0

我对antlr完全陌生。我正在尝试使用 antlr 进行一些语言翻译。我认为我使用了正确的语法,但我遇到了异常。

以下是语法的一部分:

primary
:   parExpression
|   'this' ('.' Identifier)* identifierSuffix?
|   'super' superSuffix
|   literal
|   'new' creator
|   Identifier ('.' Identifier)* identifierSuffix?
|   primitiveType ('[' ']')* '.' 'class'
|   'void' '.' 'class'
;

我添加了一个重写规则,例如

|   'new' creator -> 'mynew' creator

发生异常:

[11:11:48] error(100): rjava_new_rewrite.g:851:26: syntax error: antlr: NoViableAltException(58@[921:1: rewrite_alternative options {k=1; } : ({...}? => rewrite_template | {...}? => ( rewrite_element )+ -> {!stream_rewrite_element.hasNext()}? ^( ALT[LT(1),"ALT"] EPSILON["epsilon"] EOA["<end-of-alt>"] ) -> ^( ALT[LT(1),"ALT"] ( rewrite_element )+ EOA["<end-of-alt>"] ) | -> ^( ALT[LT(1),"ALT"] EPSILON["epsilon"] EOA["<end-of-alt>"] ) | {...}? ETC );])
[11:11:48] error(100): rjava_new_rewrite.g:851:34: syntax error: antlr: MissingTokenException(inserted [@-1,0:0='<missing SEMI>',<52>,851:33] at creator)
[11:11:48] error(100): rjava_new_rewrite.g:852:5: syntax error: antlr: MissingTokenException(inserted [@-1,0:0='<missing COLON>',<54>,852:4] at |)
[11:11:48] error(100): rjava_new_rewrite.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(0!=3)
[11:11:48] error(100): rjava_new_rewrite.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(3!=28)
[11:11:48] error(100): rjava_new_rewrite.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(3!=27)
[11:11:48] java.util.NoSuchElementException: can't look backwards more than one token in this stream
    at org.antlr.runtime.misc.LookaheadStream.LB(LookaheadStream.java:159)
    at org.antlr.runtime.misc.LookaheadStream.LT(LookaheadStream.java:120)
    at org.antlr.runtime.RecognitionException.extractInformationFromTreeNodeStream(RecognitionException.java:144)
    at org.antlr.runtime.RecognitionException.<init>(RecognitionException.java:111)
    at org.antlr.runtime.MismatchedTreeNodeException.<init>(MismatchedTreeNodeException.java:42)
    at org.antlr.runtime.tree.TreeParser.recoverFromMismatchedToken(TreeParser.java:135)
    at org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115)
    at org.antlr.grammar.v3.AssignTokenTypesWalker.grammar_(AssignTokenTypesWalker.java:388)
    at org.antlr.tool.CompositeGrammar.assignTokenTypes(CompositeGrammar.java:337)
    at org.antlr.tool.Grammar.setGrammarContent(Grammar.java:605)
    at org.antlr.works.grammar.antlr.ANTLRGrammarEngineImpl.createNewGrammar(ANTLRGrammarEngineImpl.java:192)
    at org.antlr.works.grammar.antlr.ANTLRGrammarEngineImpl.createParserGrammar(ANTLRGrammarEngineImpl.java:225)
    at org.antlr.works.grammar.antlr.ANTLRGrammarEngineImpl.createCombinedGrammar(ANTLRGrammarEngineImpl.java:203)
    at org.antlr.works.grammar.antlr.ANTLRGrammarEngineImpl.createGrammars(ANTLRGrammarEngineImpl.java:165)
    at org.antlr.works.grammar.engine.GrammarEngineImpl.getGrammarLanguage(GrammarEngineImpl.java:115)
    at org.antlr.works.components.GrammarWindowMenu.getEditTestRigTitle(GrammarWindowMenu.java:244)
    at org.antlr.works.components.GrammarWindowMenu.menuItemState(GrammarWindowMenu.java:529)
    at org.antlr.works.components.GrammarWindow.menuItemState(GrammarWindow.java:440)
    at org.antlr.xjlib.appkit.menu.XJMainMenuBar.refreshMenuItemState(XJMainMenuBar.java:175)
    at org.antlr.xjlib.appkit.menu.XJMainMenuBar.refreshMenuState(XJMainMenuBar.java:169)
    at org.antlr.xjlib.appkit.menu.XJMainMenuBar.refreshState(XJMainMenuBar.java:153)
    at org.antlr.xjlib.appkit.menu.XJMainMenuBar.refresh(XJMainMenuBar.java:145)
    at org.antlr.works.grammar.decisiondfa.DecisionDFAEngine.refreshMenu(DecisionDFAEngine.java:203)
    at org.antlr.works.components.GrammarWindow.afterParseOperations(GrammarWindow.java:1179)
    at org.antlr.works.components.GrammarWindow.access$200(GrammarWindow.java:96)
    at org.antlr.works.components.GrammarWindow$AfterParseOperations.threadRun(GrammarWindow.java:1553)
    at org.antlr.works.ate.syntax.misc.ATEThread.run(ATEThread.java:152)
    at java.lang.Thread.run(Thread.java:680)

任何人都可以给出任何想法吗?

4

1 回答 1

2

qinsoon 写道:

我认为我使用了正确的语法,但我遇到了异常。

你想错了。:)

您不能将解析器不匹配的内容放入重写规则中。所以在你的情况下:

|   'new' creator -> 'mynew' creator

'mynew'是错误的,因为解析器从未遇到过这样的令牌/规则。如果您想在解析器未遇到的 AST 中插入标记(它们在 ANTLR 中称为虚构标记),则需要在tokens {...}语法中定义它们,如下所示:

grammar YourGrammarName;

options {
  output=AST;
}

tokens {
  MYNEW;
}

primary
:   ...
|   'new' creator -> ^(MYNEW creator)
|   ...
;

这将插入一个类型为MYNEWinnner-text的节点"MYNEW"。如果要在节点中关联一些自定义文本,请执行以下操作:

primary
:   ...
|   'new' creator -> ^(MYNEW["mynew"] creator)
|   ...
;

从上面可以看出,我创建了一个根节点为 的 AST MYNEW。如果我做:

|   'new' creator -> MYNEW creator

ANTLR 将从该规则返回 2 个节点。如果该规则成为另一个(子)树的根,您将遇到麻烦:毕竟,您可以获得具有 2 个根的 AST!始终努力让重写规则产生单个 AST/节点:

rule
 : subrule ';' -> subrule // omit the semi-colon
 ;

或者当需要创建更多节点时,使用单个根节点制作适当的 AST:

rule
 : AToken subrule1 subrule2 ';' -> ^(AToken subrule1 subrule2) // AToken is the root
 ;
于 2012-05-22T07:26:52.610 回答