antlr

Antlr Left-Factoring Grammar

耗尽温柔 提交于 2019-12-11 12:38:41
问题 I am new to Antlr. I am defining a grammar for my company using Antlr 3. Below is my grammar: grammar Grammar; @header { package com.grammar; } true : 'true'; false : 'false'; null : 'null'; value : true | false | null | STRING | NUMBER; query : (STATEMENT+) | STATEMENT?; INSERT : 'INSERT INTO' TABLE 'VALUES' '('ELEMENTS')'';'; STATEMENT : INSERT; STRING : ('a'..'z'|'A'..'Z')+; INTEGER : '0'..'9'+; ELEMENTS : value | value ',' ELEMENTS; When I try to generate the code using ANTLRWorks, I get

Issues of Error handling with ANTLR3

独自空忆成欢 提交于 2019-12-11 12:04:34
问题 I tried error reporting in following manner. @members{ public String getErrorMessage(RecognitionException e,String[] tokenNames) { List stack=getRuleInvocationStack(e,this.getClass().getName()); String msg=null; if(e instanceof NoViableAltException){ <some code> } else{ msg=super.getErrorMessage(e,tokenNames); } String[] inputLines = e.input.toString().split("\r\n"); String line = ""; if(e.token.getCharPositionInLine()==0) line = "at \"" + inputLines[e.token.getLine() - 2]; else if(e.token

antlr is there any way to generate the visitor code(generic visitor)

南楼画角 提交于 2019-12-11 11:47:01
问题 We would like to have the CommonTree have a visit(OurVisitor visitor) method but CommonTree is not a generated class. Right now, we have this code ANTLRStringStream stream = new ANTLRStringStream(sql); NoSqlLexer lexer = new NoSqlLexer(stream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); NoSqlParser parser = new NoSqlParser(tokenStream); CommonTree tree = (CommonTree) parser.statement().getTree(); I can always externalize walking the tree, but it is nice to just call tree

Antlr: how to match everything between the other recognized tokens?

ぃ、小莉子 提交于 2019-12-11 10:39:39
问题 How do I match all of the leftover text between the other tokens in my lexer? Here's my code: grammar UserQuery; expr: expr AND expr | expr OR expr | NOT expr | TEXT+ | '(' expr ')' ; OR : 'OR'; AND : 'AND'; NOT : 'NOT'; LPAREN : '('; RPAREN : ')'; TEXT: .+?; When I run the lexer on "xx AND yy", I get these tokens: x type:TEXT x type:TEXT type:TEXT AND type:'AND' type:TEXT y type:TEXT y type:TEXT This sort-of works, except that I don't want each character to be a token. I'd like to

How to parse JavaScript function expression calls with ANTLR?

大城市里の小女人 提交于 2019-12-11 10:35:37
问题 I am building a JavaScript instrumentor with ANTLR, using the Patrick Hulsmeijer EcmaScript 3 grammar. I'm having a problem parsing this line of code: function(){}(); that is a direct call of a function expression. The parser recognizes the statement as a function declaration and then fails when it finds the parentheses after the function body. The reason is that function declarations are recognized with most precedence to avoid the ambiguity with function expressions. This is how the grammar

ANTLR on a noisy data stream Part 2

别说谁变了你拦得住时间么 提交于 2019-12-11 10:18:10
问题 Following a very interesing discussion with Bart Kiers on parsing a noisy datastream with ANTLR, I'm ending up with another problem... The aim is still the same : only extracting useful information with the following grammar, VERB : 'SLEEPING' | 'WALKING'; SUBJECT : 'CAT'|'DOG'|'BIRD'; INDIRECT_OBJECT : 'CAR'| 'SOFA'; ANY : . {skip();}; parse : sentenceParts+ EOF ; sentenceParts : SUBJECT VERB INDIRECT_OBJECT ; a sentence like it's 10PM and the Lazy CAT is currently SLEEPING heavily on the

Drawing parse tree in ANTLR4 using Java

谁说胖子不能爱 提交于 2019-12-11 10:12:26
问题 I am new to ANTLR4, when I was first trying it out in command line I was using the grun with gui parameter. Now I am developing a Java application and I want to display the same dialog while executing my Java program. I generated the ParseTree successfully, and I can navigate through it. But I want to display it as well. I think it has something to do with TreeViewer class but I couldn't figure out how to use it. Thanks 回答1: TreeViewer is a Swing Component so you should be able to add it to

Antlr 4 parsing large c file takes forever

a 夏天 提交于 2019-12-11 09:39:44
问题 I have a large c-code file (>9000 LoC) and attempt to parse it using this grammar: https://github.com/antlr/grammars-v4/blob/master/c/C.g4 I waited for over an hour before aborting. The machine is a Core 2 Duo L9400 with 4GB of ram. Maximum java vm-heap-size is set to 2GB. It does not produce any parse errors, but it simply doesn't finish. After doing some research, I set the prediction mode to SLL, which produces a "no viable alternative at input" within seconds. Next, I set the prediction

Internal EBCDIC support for ANTLR 3.1.3?

余生颓废 提交于 2019-12-11 09:38:18
问题 I'm trying to use ANTLR 3.1.3 on a system with local EBCDIC char set Even a simple grammar like this: lexer grammar test; GENERIC_ID : (LETTER)* ; fragment LETTER : 'a' .. 'z' ; results in these errors during the initial compile (java org.antlr.Tool test.g): error(10): internal error: problem parsing group <unknown>: line 1:1: unexpected char: 0x7 : line 1:1: unexpected char: 0x7 org.antlr.stringtemplate.language.GroupLexer.nextToken(GroupLexer.java:233) antlr.TokenBuffer.fill(TokenBuffer

Antlr Parser operator priority

只愿长相守 提交于 2019-12-11 09:26:00
问题 Consider the following grammar. I have issues with the operator priority, for instance: res=2*a+b has a similar parse tree as res=2*(a+b). I know where the problem is, but no "beautiful" solution without mutual left recursion comes to my mind. Can you please help me out a little. The grammar is used with a custom visitor. grammar Math; expression: expression add=('+'|'-') expression # expressionAddExpression | expression mult='*' expression # expressionMultExpression |'(' expression ')' #