简体   繁体   English

ANTLR 3.x - 如何格式化重写规则

[英]ANTLR 3.x - How to format rewrite rules

I'm finding myself challenged on how to properly format rewrite rules when certain conditions occur in the original rule. 我发现自己在如何在原始规则中出现某些条件时正确格式化重写规则时遇到了挑战。

What is the appropriate way to rewrite this: 什么是重写这个的适当方法:

unaryExpression: op=('!' | '-') t=term
  -> ^(UNARY_EXPR $op $t)

Antlr doesn't seem to like me branding anything in parenthesis with a label and "op=" fails. Antlr似乎不喜欢我用标签在括号中标记任何东西而“op =”失败。 Also, I've tried: 另外,我试过:

unaryExpression: ('!' | '-') t=term
  -> ^(UNARY_EXPR ('!' | '-') $t)

Antlr doesn't like the or '|' Antlr不喜欢或者'|' and throws a grammar error. 并抛出语法错误。

Replacing the character class with a token name does solve this problem, however it creates a quagmire of other issues with my grammar. 用令牌名称替换字符类确实可以解决这个问题,但是它会在语法上产生其他问题的泥潭。

--- edit ---- ---编辑----

A second problem has been added. 第二个问题已被添加。 Please help me format this rule with tree grammar: 请帮我用树语法格式化这个规则:

multExpression : unaryExpression (MULT_OP unaryExpression)* ;

Pretty simple: My expectation is to enclose every matched token in a parent (imaginary) token MULT so that I end up with something like: 非常简单:我的期望是将每个匹配的令牌包含在父(虚构)令牌MULT这样我最终得到的结果如下:

 MULT
  o
  |
  o---o---o---o---o
  |   |   |   |   |
 '3' '*' '6' '%'  2
unaryExpression
    :    (op='!' | op='-') term
         -> ^(UNARY_EXPR[$op] $op term)
    ;

I used the UNARY_EXPR[$op] so the root node gets some useful line/column information instead of defaulting to -1. 我使用了UNARY_EXPR[$op]因此根节点获取了一些有用的行/列信息,而不是默认为-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM