antlr - 解析器没有使用所有标记,这是一个错误吗?
问题描述
环境:antlr 4.7.1
语法是:
grammar Whilelang;
program : seqStatement;
seqStatement: statement (';' statement)* ;
statement: ID ':=' expression # attrib
| 'print' Text # print
| '{' seqStatement '}' # block
;
expression: INT # int
| ID # id
| expression ('+'|'-') expression # binOp
| '(' expression ')' # expParen
;
bool: ('true'|'false') # boolean
| expression '=' expression # relOp
| expression '<=' expression # relOp
| 'not' bool # not
| bool 'and' bool # and
| '(' bool ')' # boolParen
;
INT: ('0'..'9')+ ;
ID: ('a'..'z')+;
Text: '"' .*? '"';
Space: [ \t\n\r] -> skip;
输入语言代码为:
a := 1
b := 2
根据语法,Antlr4 应该会输出错误 --" expect ';' 在第 1 行“为上述输入语言代码。但实际上。没有错误输出,似乎语法只接受部分输入,并且没有消耗所有输入标记。是antlr4的bug吗?
$ grun Whilelang program -trace
a := 1
b := 2
^d
enter program, LT(1)=a
enter seqStatement, LT(1)=a
enter statement, LT(1)=a
consume [@0,0:0='a',<17>,1:0] rule statement
consume [@1,2:3=':=',<2>,1:2] rule statement
enter expression, LT(1)=1
consume [@2,5:5='1',<16>,1:5] rule expression
exit expression, LT(1)=b
exit statement, LT(1)=b
exit seqStatement, LT(1)=b
exit program, LT(1)=b
解决方案
Not a bug. ANTLR is doing exactly what it was asked to do.
Given the rules
program : seqStatement;
seqStatement: statement (';' statement)* ;
the program
rule is then entirely complete when at least one statement
has been matched. Since the parser cannot validly match another statement
-- optional per the grammar-- it stops.
Changing to
program : seqStatement EOF;
requires the program
rule to match statements
until it can also match an EOF
token (the lexer automatically adds an EOF
at the end of the source text). This likely the behavior you are looking for.
推荐阅读
- javascript - 如何使用 AWS SDK v3 为经过身份验证的用户请求临时 STS 凭证?
- excel - 是否可以使用动态过滤器的结果填充 Excel 工作表?
- amazon-ec2 - 是否可以在不同的 EC2 机器上创建一个 Kafka 集群?
- firebase - VSCode 不再自动完成/建议 firebase 方法?
- mysql - MySQL 查询未按预期运行
- batch-file - 使用“rundll 键盘,禁用”运行批处理文件是否需要恢复出厂设置才能修复?
- esp8266 - ESP8266-12F WiFi 软 AP config.authmode 失败
- r - 包足球动画
- php - 带有文本框自定义验证的 Wordpress Ninja 表单
- pine-script - 从图表参数中删除空值