In my grammar with antlrworks, I can get noviablealtexception for rules like if, while which need corresponding right and left brackets. However, in java, i cannot get noviablealtexception.
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 statement rule I have a block rule which is,
statement_blocks
: (LBRACE statement* RBRACE)
;
And statement rule is below,
statement
: var_dec
| statement_blocks
| condition_statement
| loop_statement
| expr_statement
;
Before posting this I’ve checked some examples. I think i need to add EOF at the end of each rule. When I add EOF for those rules, I get different errors. For example,
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
These are what I get for the following inputs;
if(s==d){
d=s;
if(a=n){
s=h;
}
a=g;
}
line 6:0 missing EOF at ‘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 file line 3:0 missing EOF at ‘if’,
testcases/new file line 6:0 missing EOF at ‘a’
while(s==d){
d=s;
while(a=n){
s=h;
}
a=g;
}
line 6:0 missing EOF at ‘a’
When I remove the first left bracket from the first “while”
while(s==d)
d=s;
while(a=n){
s=h;
}
a=g;
}
testcases/new file line 3:0 missing EOF at ‘while’
testcases/new file line 6:0 missing EOF at ‘a’
No, you need to place
EOFat the end of your “main” parser rule, not after more than one statement. By doing so, the parser expects the end of the file after such statements (which is not correct, of course).My guess is that your entry point does not contain
EOFcausing the parser to stop prematurely instead of throwing an error/exception when it stumbles upon invalid input.Here’s a demo (note the
EOFafter theparserule):T.g
which can be tested with the class:
Main.java
Testing it all
If you now parse the input file (
in.txt):there’s no problem, as you can see:
And if you remove a
(or)from the filein.txt, you will get the following (similar) error: