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

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

问题描述

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

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

这是我的语法:
这是在此链接中有两个 decFunc 的映像
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 。生成的分析树正好显示:两个子树,每个子树以 deFunc 为根。



Antlr v4不会产生真正的AST,其中 f sum 子树。


我可以用语法做两个 f sum 根 - Jonny Magnam


不直接在Antlr v4语法。您可以:


  1. 切换到Antlr v3或其他解析器工具,并根据需要定义生成的AST。

  2. 步行Antlr v4解析树并创建一个单独的AST所需的形式。

  3. 只需直接使用parse-tree就可以实现它在信息上等同于

具体来说,标准学术AST是可变的,意味着每个但第一个)访问者是自定义的,不是生成的,并且底层语法或AST的临时结构的任何改变将需要重新考虑,并且可能对每个后续访问者及其实现的逻辑的改变。



Antlr v4分析树基本上是不可变的,允许装饰对树节点累积而不损失关系完整性。访客都使用共同的基本结构,大大降低由于语法变化和先前执行的访客的影响的脆弱性。作为实际问题,除非明确期望,否则易于构造,快速且相互独立的树木走向。他们可以在设计中实现更大的关注点,并在实践中更轻松地实现代码维护。



以任何方式为整个工作选择合适的工具。 p>

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

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
                ;

解决方案

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 will not produce a true AST where f and sum are the roots of separate sub-trees.

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

Not directly in an Antlr v4 grammar. You could:

  1. switch to Antlr v3, or another parser tool, and define the generated AST as you wish.
  2. walk the Antlr v4 parse-tree and create a separate AST of your desired form.
  3. just use the parse-tree directly with the realization that it is informationally equivalent to a classically defined AST and the implementation provides a number practical benefits.

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.

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