Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ANTLR not parse the entire input?

I am quite new to ANTLR, so this is likely a simple question.
I have defined a simple grammar which is supposed to include arithmetic expressions with numbers and identifiers (strings that start with a letter and continue with one or more letters or numbers.)

The grammar looks as follows:

grammar while;

@lexer::header {
  package ConFreeG;
}  

@header {
  package ConFreeG;
  
  import ConFreeG.IR.*;
}

@parser::members {
}

arith:
    term
    | '(' arith ( '-' | '+' | '*' ) arith ')'  
    ;
    
term  returns [AExpr a]:    
    NUM
    {
        int n = Integer.parseInt($NUM.text);
        a = new Num(n);
    }
    | IDENT
    {
        a = new Var($IDENT.text);
    }
    ;

fragment LOWER : ('a'..'z');
fragment UPPER : ('A'..'Z');
fragment NONNULL : ('1'..'9');
fragment NUMBER : ('0' | NONNULL);
IDENT  : ( LOWER | UPPER ) ( LOWER | UPPER | NUMBER )*;
NUM    : '0' | NONNULL NUMBER*;

fragment NEWLINE:'\r'? '\n';
WHITESPACE  :   ( ' ' | '\t' | NEWLINE )+ { $channel=HIDDEN; };

I am using ANTLR v3 with the ANTLR IDE Eclipse plugin. When I parse the expression (8 + a45) using the interpreter, only part of the parse tree is generated:

alt text

Why does the second term (a45) not get parsed? The same happens if both terms are numbers.

like image 867
Martin Wiboe Avatar asked Oct 16 '25 14:10

Martin Wiboe


1 Answers

You'll want to create a parser rule that has an EOF (end of file) token in it so that the parser will be forced to go through the entire token stream.

Add this rule to your grammar:

parse
  :  arith EOF
  ;

and let the interpreter start at that rule instead of the arith rule:

alt text

like image 109
Bart Kiers Avatar answered Oct 18 '25 10:10

Bart Kiers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!