开发者

Why does ANTLR not parse the entire input?

开发者 https://www.devze.com 2022-12-25 22:13 出处:网络
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

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  : 开发者_Python百科  ( ' ' | '\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:

Why does ANTLR not parse the entire input?

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


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:

Why does ANTLR not parse the entire input?

0

精彩评论

暂无评论...
验证码 换一张
取 消