使用ANTLR容忍格式错误的语句(例如,用于代码完成) [英] Tolerating malformed statements with ANTLR (e.g., for code-completion)

查看:60
本文介绍了使用ANTLR容忍格式错误的语句(例如,用于代码完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于简单DSL的ANTLR语法,在没有语法错误的情况下,所有内容都能正常运行.但是,现在,我需要支持一种自动补全机制,在这种机制下,我需要从我的树语法中获取可能的补全,从而对属性,函数等进行基本的类型检查.

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.

问题是,ANTLR不在本地statement级别报告语法错误,而是在解析树的更远处(例如,在programfunction级别)报告语法错误.因此,而不是看起来像

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

我在树的顶部得到了垃圾节点,因为未能匹配statement规则会冒泡"并阻止function规则匹配.

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.

是否可以编写一条包含"catch-all"子句的规则以吃掉意外令牌的方法?

我在想类似的东西:

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中可以有任意数量的垃圾节点,但是垃圾之前(最好是垃圾之后)的所有内容都应该是合理的.

There may be any number of garbage nodes in the AST, but everything before (and preferably after) the garbage should be sane.

我将不胜感激任何提示/建议/指针/等.我使用的是Java目标ANTLR v3.

I'd appreciate any hints/suggestions/pointers/etc. I'm using ANTLR v3, Java target.

推荐答案

看看 http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery

顺便说一句:如果您要定位日食,则应查看xtext( http://www.eclipse .org/Xtext/)-它基于ANTLR 3,并生成一个具有语法提示和代码辅助功能的漂亮编辑器.

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.

这篇关于使用ANTLR容忍格式错误的语句(例如,用于代码完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆