问题描述
这应该很简单.我正在使用ANTLR进行词法分析器语法,并希望将变量标识符的最大长度限制为30个字符.我试图用这一行来做到这一点(遵循普通的正则表达式-除了"thing-语法"):
This should be fairly simple.I'm working on a lexer grammar using ANTLR, and want to limit the maximum length of variable identifiers to 30 characters. I attempted to accomplish this with this line(following normal regex - except for the '' thing - syntax):
ID : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'){0,29} {System.out.println("IDENTIFIER FOUND.");}
;
代码生成没有错误,但是由于生成的代码中的一行很简单,所以编译失败:
No errors in code generation, but compilation failed due to a line in the generated code that was simply:
0.29
很明显,antlr正在将括号内的文本部分与打印行一起放置在接受状态区域中.我搜索了ANTLR网站,但没有找到示例或对等效表达式的引用.该表达式的语法应该是什么?
Obviously antlr is taking the section of text between the brackets and placing it in the accept state area along with the print line. I searched the ANTLR site, and I found no example or reference to an equivalent expression.What should the syntax of this expression be?
推荐答案
ANTLR不支持{m,n}
量词语法. ANTLR看到了量词的{}
,无法将它们与包围动作的{}
分开.
ANTLR does not support the {m,n}
quantifier syntax. ANTLR sees the {}
of your quantifier and can't tell them apart from the {}
that surround your actions.
解决方法:
- 从语义上加强限制.让它收集不限大小的ID,然后将其作为操作代码的一部分或以后在编译器中进行投诉/截断.
- 手动创建定量规则.
这是一个手动规则的示例,该规则将ID限制为8.
This is an example of a manual rule that limits IDs to 8.
SUBID : ('a'..'z'|'A'..'Z'|'0'..'9'|'_')
;
ID : ('a'..'z'|'A'..'Z')
(SUBID (SUBID (SUBID (SUBID (SUBID (SUBID SUBID?)?)?)?)?)?)?
;
就个人而言,我将使用语义解决方案(#1).如今,很少有理由限制一种语言中的标识符,而更少的理由是在违反这种规则时导致语法错误(早期中止编译).
Personally, I'd go with the semantic solution (#1). There is very little reason these days to limit the identifiers in a language, and even less reason to cause a syntax error (early abort of the compile) when such a rule is violated.
这篇关于ANTLR Regex中的范围限定符语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!