为什么 ANTLR 不解析整个输入?

发布于 2024-08-27 21:44:23 字数 1125 浏览 6 评论 0原文

我对 ANTLR 还很陌生,所以这可能是一个简单的问题。
我定义了一种简单的语法,它应该包含带有数字和标识符的算术表达式(以字母开头并以一个或多个字母或数字继续的字符串。)

语法如下所示:

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; };

我正在将 ANTLR v3 与 ANTLR IDE 一起使用Eclipse 插件。当我使用解释器解析表达式 (8 + a45) 时,仅生成解析树的一部分:

alt text

为什么第二项 (a45) 没有被解析?如果两项都是数字,也会发生同样的情况。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-09-03 21:44:24

您需要创建一个包含 EOF(文件结尾)标记的解析器规则,以便解析器将被迫遍历整个标记流。

将此规则添加到您的语法中:

parse
  :  arith EOF
  ;

并让解释器从该规则而不是 arith 规则开始:

替代文本

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文