ANTLR 4 解析器语法 [英] ANTLR 4 Parser Grammar

查看:39
本文介绍了ANTLR 4 解析器语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何改进我的解析器语法,而不是为我的测试代码创建包含几个 decFunc 规则的 AST.它将只创建一个,sum 成为第二个根.我尝试使用多种不同的方式解决这个问题,但我总是得到一个左递归错误.这是我的测试代码:

How can I improve my parser grammar so that instead of creating an AST that contains couple of decFunc rules for my testing code. It will create only one and sum becomes the second root. I tried to solve this problem using multiple different ways but I always get a left recursive error. This is my testing code :

f :: [Int] -> [Int] -> [Int]
f x y = zipWith (sum) x y
sum :: [Int] -> [Int]
sum a = foldr(+) a

这是我的语法:这是此链接中有两个 decFunc 的图像http://postimg.org/image/w5goph9b7/

This is my grammar: This is the image that has two decFuncin this link http://postimg.org/image/w5goph9b7/

prog        : stat+;

stat        : decFunc  | impFunc ;


decFunc     : ID '::' formalType ( ARROW formalType )* NL impFunc
            ;

anotherFunc : ID+;


formalType  : 'Int' | '[' formalType ']' ;


impFunc     : ID+ '=' hr NL

            ;


hr          : 'map' '(' ID* ')'  ID*
                | 'zipWith' '(' ('*' |'/' |'+' |'-') ')' ID+ | 'zipWith' '(' anotherFunc ')' ID+
                | 'foldr'   '(' ('*' |'/' |'+' |'-') ')' ID+
                | hr  op=('*'| '/' | '.&.' | 'xor' ) hr | DIGIT
                | 'shiftL' hr hr | 'shiftR' hr hr
                | hr  op=('+'| '-') hr | DIGIT
                | '(' hr ')'
                | ID '(' ID* ')'
                | ID
                ;

推荐答案

您的测试输入包含两个与 decFunc 规则匹配的内容实例.生成的 parse-tree 正好表明:两个子树,每个子树都有一个 deFunc 作为根.

Your test input contains two instances of content that will match the decFunc rule. The generated parse-tree shows exactly that: two sub-trees, each having a deFunc as the root.

Antlr v4 不会产生真正的 AST,其中 fsum 是单独子树的根.

Antlr v4 will not produce a true AST where f and sum are the roots of separate sub-trees.

我可以用语法做些什么来使 fsum 成为根 – Jonny Magnam

Is there any thing can I do with the grammar to make both f and sum roots – Jonny Magnam

不直接在 Antlr v4 语法中.你可以:

Not directly in an Antlr v4 grammar. You could:

  1. 切换到 Antlr v3 或其他解析器工具,并根据需要定义生成的 AST.
  2. 遍历 Antlr v4 解析树并创建所需形式的单独 AST.
  3. 直接使用解析树,并意识到它在信息上等同于经典定义的 AST,并且该实现提供了许多实际好处.

具体来说,标准学术 AST 是可变的,这意味着每个(或除第一个之外的所有)访问者都是自定义的,而不是生成的,并且基础语法或 AST 的临时结构的任何更改都需要重新考虑和可能的更改到每个后续访问者及其实现的逻辑.

Specifically, the standard academic AST is mutable, meaning that every (or all but the first) visitor is custom, not generated, and that any change in the underlying grammar or an interim structure of the AST will require reconsideration and likely changes to every subsequent visitor and their implemented logic.

Antlr v4 解析树本质上是不可变的,允许在不损失关系完整性的情况下针对树节点累积装饰.访问者都使用一个共同的基础结构,大大减少了由于语法变化和先前执行的访问者的影响而造成的脆弱性.实际上,除非有明确要求,否则 tree-walk 很容易构建、快速且相互独立.它们可以在设计中实现更大的关注点分离,并在实践中实现更轻松的代码维护.

The Antlr v4 parse-tree is essentially immutable, allowing decorations to be accumulated against tree nodes without loss of relational integrity. Visitors all use a common base structure, greatly reducing brittleness due to grammar changes and effects of prior executed visitors. As a practical matter, tree-walks are easily constructed, fast, and mutually independent except where expressly desired. They can achieve a greater separation of concerns in design and easier code maintenance in practice.

为整个工作选择合适的工具,无论您如何定义它.

Choose the right tool for the whole job, in whatever way you define it.

这篇关于ANTLR 4 解析器语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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