使用 ANTLR 容忍格式错误的语句(例如,用于代码完成)
我有一个简单 DSL 的 ANTLR 语法,如果没有语法错误,一切都会顺利进行。然而,现在我需要支持自动完成机制,我需要从对属性、函数等执行基本类型检查的树语法中获得可能的完成。
问题是,ANTLR 没有报告语法错误本地语句
级别,但在解析树的更上方,例如程序
或函数
级别。因此,我并不是
program
|
function
/ | \
/ | \
stat hosed stat
在树的顶部得到垃圾节点,而是因为与 statement
规则匹配失败而“冒泡”并阻止了 function
匹配规则。
有没有办法编写一个具有“包罗万象”子句的规则来吃掉意外的令牌?
我正在考虑这样的事情:
statement
: var_declaration
| if_statement
| for_loop
| garbage
;
garbage
: /* Match unexpected tokens, etc. (not actual statements, or closing
parens, braces, etc.). Maybe just consume one input token and let
the parser try again? */
;
AST中可能有任意数量的垃圾节点,但是垃圾之前(最好是之后)的一切都应该是理智的。
我将不胜感激任何提示/建议/指示/等。我正在使用 ANTLR v3,Java 目标。
I have an ANTLR grammar for a simple DSL, and everything works swimmingly when there are no syntax errors. Now, however, I need to support an auto-completion mechanism, where I need to get possible completions from my tree grammars that perform basic type-checking on attributes, functions, etc.
The problem is, ANTLR isn't reporting syntax errors at the local statement
level, but farther up the parse tree, e.g., at the program
or function
level. Hence, instead of an AST that looks like
program
|
function
/ | \
/ | \
stat hosed stat
I get garbage nodes across the top of the tree, as a failure to match the statement
rule "bubbles up" and prevents the function
rule from matching.
Is there a way to write a rule that has a "catch-all" clause to eat unexpected tokens?
I'm thinking of something like:
statement
: var_declaration
| if_statement
| for_loop
| garbage
;
garbage
: /* Match unexpected tokens, etc. (not actual statements, or closing
parens, braces, etc.). Maybe just consume one input token and let
the parser try again? */
;
There may be any number of garbage nodes in the AST, but everything before (and preferably after) the garbage should be sane.
I'd appreciate any hints/suggestions/pointers/etc. I'm using ANTLR v3, Java target.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看 http://www.antlr.org/ wiki/display/ANTLR3/Error+reporting+and+recovery
顺便说一句:如果你的目标是 eclipse,你应该看看 xtext (http://www.eclipse.org/Xtext/) - 它基于 ANTLR 3,并生成一个带有语法高亮和代码辅助的漂亮编辑器。
Take a look at http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery
BTW: If you're targeting eclipse, you should look at xtext (http://www.eclipse.org/Xtext/) - it's based on ANTLR 3 and generates a nice editor with syntax hilighting and code assist.