ANTLR NoViableAltException with JAVA

In my grammar with antlrworks, I can get a noviablealtexception for rules like if, while they need matching right and left brackets. However, in java, I cannot get a novationalaltexception.

loop_statement: (WHILE LPAREN expr RPAREN statement) | (DO statement WHILE LPAREN expr RPAREN); condition_statement : IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)? 

In the instruction rule, I have a block rule, which is,

 statement_blocks : (LBRACE statement* RBRACE) ; 

And the statement rule below,

 statement : var_dec | statement_blocks | condition_statement | loop_statement | expr_statement ; 

Before posting this, I checked a few examples. I think I need to add an EOF at the end of each rule. When I add EOF for these rules, I get different errors. For instance,

 loop_statement: ((WHILE LPAREN expr RPAREN statement) | (DO statement WHILE LPAREN expr RPAREN)) EOF; condition_statement : ( (IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)? )EOF 

This is what I get for the following inputs;

 if(s==d){ d=s; if(a=n){ s=h; } a=g; } 

line 6: 0 missing EOF on 'a'
When I remove the first left bracket from the first "if"

 if(s==d) d=s; if(a=n){ s=h; } a=g; } 

testcases / new line of the file 3: 0 missing EOF in 'if',
testcases / new line of the file 6: 0 missing EOF in 'a'

  while(s==d){ d=s; while(a=n){ s=h; } a=g; } 

line 6: 0 missing EOF on 'a'
When I remove the first left parenthesis from the first "while"

  while(s==d) d=s; while(a=n){ s=h; } a=g; } 

testcases / new line of the file 3: 0 missing EOF in 'while'
testcases / new line of the file 6: 0 missing EOF in 'a'

+4
source share
1 answer

No, you need to place the EOF at the end of your "main" parser rule, and not after several statements. In doing so, the parser expects the file to finish after such statements (which, of course, is incorrect).

My guess is that your entry point does not contain EOF , causing the parser to stop prematurely rather than throwing an error / exception when it encounters invalid input.

Here's a demo (note the EOF after the parse rule):

Tg

 grammar T; parse : statement+ EOF ; statement : var_dec | statement_blocks | c=condition_statement {System.out.println("parsed :: " + $c.text);} ; var_dec : ID '=' ID ';' ; statement_blocks : LBRACE statement* RBRACE ; condition_statement : IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)? ; expr : ID '==' ID ; IF : 'if'; ELSE : 'else'; ID : 'a'..'z'+; LBRACE : '{'; RBRACE : '}'; LPAREN : '('; RPAREN : ')'; SPACE : (' ' | '\t' | '\r' | '\n')+ {skip();}; 

which can be tested using the class:

Main.java

 import org.antlr.runtime.*; public class Main { public static void main(String[] args) throws Exception { TLexer lexer = new TLexer(new ANTLRFileStream("in.txt")); TParser parser = new TParser(new CommonTokenStream(lexer)); parser.parse(); } } 

Testing everything

If you now in.txt input file ( in.txt ):

 if(s==d) { d=s; if(a==n){ s=h; } a=g; } 

no problem, as you can see:

 java -cp antlr-3.3.jar org.antlr.Tool Tg javac -cp antlr-3.3.jar *.java java -cp .:antlr-3.3.jar Main parsed :: if(a==n){s=h;} parsed :: if(s==d){d=s;if(a==n){s=h;}a=g;} 

And if you delete ( or ) from the in.txt file, you will get the following (similar) error:

 in.txt line 1:8 missing RPAREN at '{' 
+2
source

All Articles