ANTLR“子树的意外结束”
嘿。我是 ANTLR 的新手。 ANTLRWorks 向导为我编写了以下代码:
grammar test;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
INT : '0'..'9'+
;
FLOAT
: ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT
;
COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
STRING
: '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
;
CHAR: '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
调试时,它抛出以下错误:
[22:45:49] error(100): C:\Documents and Settings\user\Desktop\test.g:0:0: syntax error: codegen: <AST>:0:0: unexpected end of subtree
有人可以解释一下错误是什么,错误在哪里以及如何修复它?
谢谢。
Hey. I'm new to ANTLR. ANTLRWorks wizard wrrited for me the following code:
grammar test;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
INT : '0'..'9'+
;
FLOAT
: ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
| '.' ('0'..'9')+ EXPONENT?
| ('0'..'9')+ EXPONENT
;
COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
STRING
: '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
;
CHAR: '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
;
fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
When debugging it, it throws the following error:
[22:45:49] error(100): C:\Documents and Settings\user\Desktop\test.g:0:0: syntax error: codegen: <AST>:0:0: unexpected end of subtree
Can someone explain me what is the error, where is it and how can I fix it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 ANTLR 中,每个以大写字母开头的规则都是词法分析器规则。以小写开头的是解析器规则。正如您所看到的,您只有词法分析器规则:这就是您的问题。您必须至少有一个解析器规则。如果添加以下规则:
为词法分析器/解析器生成源文件时,错误将消失。
In ANTLR, every rule that starts with an uppercase is a lexer rule. The ones that start with a lower case are parser rules. As you can see, you only have lexer rules: and there's your problem. You have to have at least one parser rule. If you add the following rule:
the error will disappear when generating source files for your lexer/parser.
免责声明:我对 ANTLR 向导一无所知。
谷歌搜索出现了这句话:
如果您的文件应该指定语法而不仅仅是词法分析的规则,这对我来说是有意义的。文件的第一行是“语法测试”,所以大概这是一个语法。
语法可让您将一系列终结符简化为单个非终结符。例如,表示完全括号表达式的非常简单的语法如下所示:
这里,P 是根,因为所有句子最终都会简化为 P。如果一个句子不能简化为 P,则它与此语法不匹配。因此,您需要找到语法的根,并且所有其他产生式只能在根的上下文中出现(即通过直接或间接推导)。
Disclaimer: I don't know anything about ANTLR wizard.
A google search turns up this quote:
This makes sense to me IF your file is supposed to specify a grammar and not just rules for lexical analysis. The first line of your file is "grammar test" so presumably this is a grammar.
A grammar lets you reduce a series of terminal symbols to a single nonterminal symbol. So for example, a very simple grammar representing fully parenthesized expressions would look like this:
Here, P is the root because all sentences eventually reduce to a P. If a sentence cannot reduce to a P, it does not match this grammar. So, you need to find a root for your grammar, and all other productions should come up only in the context of the root (ie via a direct or indirect derivation).