1

我在 ANTLR 的 v2 中有以下语法,我需要帮助将其转换为 v3

expression
: ( simpleLookup
  | lookup
  )
  ( x:LSQRBRACKET^ {#x.setType(ATTRIBUTES);} attributesExpr RSQRBRACKET! )?
;enter code here

实际上我在下面尝试过,但不确定它是否相同,并且在尝试构建解析器时也会出现以下错误

expression
: ( simpleLookup
  | lookup
  )
  (x=LSQRBRACKET b=attributesExpr RSQRBRACKET )?) -> ^(ATTRIBUTES[$x] $a $b)?
;

并低于错误

expecting SEMI, found '->'
unexpected token: $
unexpected token: $
unexpected token: )

如何转换"!" in v3 from v2?

请用你的专业知识帮助我......

我还有一个问题是如何在 v3 中在语法级别编写树解析器,就像在 v2 中我们以前以以下格式编写的一样

class CustomTreeParser extends TreeParser;
4

1 回答 1

0

尝试这样的事情:

grammar YourGrammarName;

options {
  output=AST;
}

tokens {
  ATTRIBUTES;
}

// ...

expression
 : ( simpleLookup
   | lookup
   )
   ( x=LSQRBRACKET^ {$x.setType(ATTRIBUTES);} attributesExpr RSQRBRACKET! )?
 ;

// ...

如何转换“!” 在 v2 的 v3 中?

!从 AST 中排除某些规则/令牌的内联运算符在 v3 中没有变化。

我还有一个问题是如何在 v3 中在语法级别编写树解析器,就像在 v2 中一样......

像这样:

options {
  output=AST;
}
于 2014-08-13T07:57:17.453 回答