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'