parser-generator

How to match the empty case in CUP parser grammar

你说的曾经没有我的故事 提交于 2019-12-24 06:30:00
问题 I am using CUP to generate a parser, and I want an empty file to be an acceptable program. I have tried add the empty case to my start symbol, based off the response to a similar question here. start with prog; /* The grammar rules */ prog ::= class_block:cb | class_block:cb stmts:sb | stmts:sb | // desired empty case ; Including the desired empty case gives me the following error: parser.java:516: error: incompatible types: Object cannot be converted to Symbol CUP$parser$result = parser

ANTLR: Lexer does not recognize token

半腔热情 提交于 2019-12-24 01:24:30
问题 Given the following Lexer grammar: lexer grammar CodeTableLexer; CodeTabHeader : '[code table 1.0]'; Code : 'code'; Table : 'table'; End : 'end'; Row : 'row'; Naming : 'naming'; Dfltlang : 'dfltlang'; Language : 'english' | 'german' | 'french' | 'italian' | 'spanish'; Null : 'null'; Number : Int ('.' Digit*)? ; Identifier : ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '$' | '.' | Digit)* ; String @after { setText(getText().substring(1, getText().length() - 1).replaceAll("\\\\(.)",

Create a Verilog Parser with Ruby

删除回忆录丶 提交于 2019-12-23 04:11:50
问题 I would like to create a Verilog parser written in Ruby for a university project I know there are parser generators like Bison and Yacc. Could anyone give me some advice on how to get started? 回答1: I already have a very basic verilog parser (gem) written in ruby called verilog, if you could consider contributing to that instead, or it might give an idea of how to start. I also have a gem called rubyit, which is command line utility to parse files with erb and generate the standard version of

Parsing with incomplete grammars

点点圈 提交于 2019-12-21 17:14:43
问题 Are there any common solutions how to use incomplete grammars? In my case I just want to detect methods in Delphi (Pascal)-files, that means procedures and functions . The following first attempt is working methods : ( procedure | function | . )+ ; but is that a solution at all? Are there any better solutions? Is it possible to stop parsing with an action (e. g. after detecting implementation ). Does it make sense to use a preprocessor? And when yes - how? 回答1: If you're only looking for

How can I parse code to build a compiler in Java?

送分小仙女□ 提交于 2019-12-20 23:23:05
问题 I need to write a compiler. It's homework at the univ. The teacher told us that we can use any API we want to do the parsing of the code, as long as it is a good one. That way we can focus more on the JVM we will generate. So yes, I'll write a compiler in Java to generate Java. Do you know any good API for this? Should I use regex? I normally write my own parsers by hand, though it is not advisable in this scenario. Any help would be appreciated. 回答1: Regex is good to use in a compiler, but

non-greedy matching in Scala RegexParsers

半腔热情 提交于 2019-12-19 05:21:47
问题 Suppose I'm writing a rudimentary SQL parser in Scala. I have the following: class Arith extends RegexParsers { def selectstatement: Parser[Any] = selectclause ~ fromclause def selectclause: Parser[Any] = "(?i)SELECT".r ~ tokens def fromclause: Parser[Any] = "(?i)FROM".r ~ tokens def tokens: Parser[Any] = rep(token) //how to make this non-greedy? def token: Parser[Any] = "(\\s*)\\w+(\\s*)".r } When trying to match selectstatement against SELECT foo FROM bar , how do I prevent the selectclause

Parsing latex-like language in Java

我是研究僧i 提交于 2019-12-18 18:28:06
问题 I'm trying to write a parser in Java for a simple language similar to Latex, i.e. it contains lots of unstructured text with a couple of \commands[with]{some}{parameters} in between. Escape sequences like \\ also have to be taken into account. I've tried to generate a parser for that with JavaCC, but it looks as if compiler-compilers like JavaCC were only suitable for highly structured code (typical for general-purpose programming languages), not for messy Latex-like markup. So far, it seems

Build parser from grammar at runtime

左心房为你撑大大i 提交于 2019-12-18 12:27:14
问题 Many (most) regular expression libraries for C++ allow for creating the expression from a string during runtime. Is anyone aware of any C++ parser generators that allow for feeding a grammar (preferably BNF) represented as a string into a generator at runtime? All the implementations I've found either require an explicit code generator to be run or require the grammar to be expressed via clever template meta-programming. 回答1: It should be pretty easy to build a recursive descent, backtracking

Packrat parsing vs. LALR parsing

感情迁移 提交于 2019-12-18 11:44:07
问题 A lot of websites states that packrat parsers can parse input in linear time. So at the first look they me be faster than LALR parser contructed by the tools yacc or bison. I wanted to know if the performance of packrat parsers is better/worse than the performance of LALR parser when tested with common input (like programming language source files) and not with any theoretical inputs. Does anyone can explain the main differences between the two approaches. Thanks! 回答1: I'm not an expert at

How do I parse indents and dedents with pyparsing?

人盡茶涼 提交于 2019-12-18 11:12:03
问题 Here is a subset of the Python grammar: single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE stmt: simple_stmt | compound_stmt simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE small_stmt: pass_stmt pass_stmt: 'pass' compound_stmt: if_stmt if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT (You can read the full grammar in the Python SVN repository: http://svn.python.org/.../Grammar) I am trying to use this