1

I made a grammar which constructs comparison between expressions as follows.

grammar EtlExpression;

/** The start rule; begin parsing here. */
prog   : comp ; 

comp   : expr ('='|'<='|'>='|'<'|'>') expr ;

expr   : expr ('*'|'/') expr   
       | expr ('+'|'-') expr   
       | '(' expr ')'         
       | func
       | ID                    
       | STR                    
       | NUM    
       ;

func   : ID '(' expr (',' expr)* ')' ;    // function 

STR    : '"' ('\\"'|.)*? '"' ;                       // match identifiers 
ID     : LETTER (LETTER|DIGIT)* ;                    // match identifiers 
NUM    : '-'? ('.' DIGIT+ | DIGIT+ ('.' DIGIT*)? ) ; // match number
WS     : [ \t\r\n]+ -> skip ;                        // toss out whitespace

fragment
LETTER : [a-zA-Z] ;
fragment
DIGIT  : [0-9] ;

then I ran testRig after compile. The result is as follwoging.

java -cp .;C:\App\Antlr4\antlr-4.7.1-complete.jar org.antlr.v4.gui.TestRig test.antlr.EtlExpression prog -tree
a < b = c
^Z
(prog (comp (expr a) < (expr b)))

The comp rule specifies only one comparison operand and I think this test input string should emit some kind error like "line 1:6 token recognition error at: '=' ", but it just ignores "= c" part.

Could you help me What's wrong with the grammar or how i can get the right message? Thankyou in advance.

류상욱
  • 11
  • 1

1 Answers1

1

The parser simply stops when it cannot match = and beyond it. If you force the parser to consume the entire input, an error will appear on your stdout. You can do that by adding EOF to your prog rule:

prog : comp EOF; 
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288