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

查看:28
本文介绍了使用 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 级别报告语法错误,而是在解析树的更上层,例如,在 program功能级.因此,而不是看起来像

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.

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

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天全站免登陆