2013-04-15 105 views
1

我正在使用VS2012與ANTLR語言支持和ANTLR C#文件版本3.5.0.2生成詞法分析器和解析器的C#代碼。VS2012 ANTLR語言支持生成的解析器不能編譯

我的語法包含以下(這裏只給出部分)

options { 
    language=CSharp3; 
    TokenLabelType=CommonToken; 
    output=AST; 
    ASTLabelType=CommonTree; 
    backtrack=true; 
} 

fieldExpression : a=FIELDNAME b=(atomicExpression) -> ^(FieldNode $a $b); 


atomicExpression : PHRASE 
     | specialSynonym 
     | NUMBER 
     | LPARENTHESIS! notExpression RPARENTHESIS!; 


specialSynonym : 
     (CONSTITUTION OF INDIA)=>a=CONSTITUTION b=OF c=INDIA { a.Type = WORD; b.Type = WORD; c.Type = WORD; } -> ^(SpecialSynonymNode ^(SynonymNode $a $b $c) ^(SynonymNode {(object)adaptor.Create(WORD, "coi")})) 
     | (COI) => a=COI { a.Type = WORD; } -> ^(SpecialSynonymNode ^(SynonymNode {(object)adaptor.Create(WORD, "constitution")} {(object)adaptor.Create(WORD, "of")} {(object)adaptor.Create(WORD, "india")}) ^(SynonymNode $a)) 

當我生成的C#代碼,我得到

private AstParserRuleReturnScope<CommonTree, CommonToken> fieldExpression() 
    { 
     EnterRule_fieldExpression(); 
     EnterRule("fieldExpression", 9); 
     TraceIn("fieldExpression", 9); 
     AstParserRuleReturnScope<CommonTree, CommonToken> retval = new AstParserRuleReturnScope<CommonTree, CommonToken>(); 
     retval.Start = (CommonToken)input.LT(1); 

     CommonTree root_0 = default(CommonTree); 

     CommonToken a = default(CommonToken); 
     CommonToken b = default(CommonToken); 

     CommonTree a_tree = default(CommonTree); 
     CommonTree b_tree = default(CommonTree); 
     RewriteRuleITokenStream stream_FIELDNAME=new RewriteRuleITokenStream(adaptor,"token FIELDNAME"); 
     RewriteRuleSubtreeStream stream_atomicExpression=new RewriteRuleSubtreeStream(adaptor,"rule atomicExpression"); 
     try { DebugEnterRule(GrammarFileName, "fieldExpression"); 
     DebugLocation(119, 72); 
     try 
     { 
      // XmlParser\\AntlrParser\\LuceneSearchGrammar.g3:119:17: (a= FIELDNAME b= (atomicExpression) -> ^(FieldNode $a $b)) 
      DebugEnterAlt(1); 
      // XmlParser\\AntlrParser\\LuceneSearchGrammar.g3:119:19: a= FIELDNAME b= (atomicExpression) 
      { 
      DebugLocation(119, 20); 
      a=(CommonToken)Match(input,FIELDNAME,Follow._FIELDNAME_in_fieldExpression2198); if (state.failed) return retval; 
      if (state.backtracking == 0) stream_FIELDNAME.Add(a); 

      DebugLocation(119, 32); 
      // XmlParser\\AntlrParser\\LuceneSearchGrammar.g3:119:33: (atomicExpression) 
      DebugEnterAlt(1); 
      // XmlParser\\AntlrParser\\LuceneSearchGrammar.g3:119:34: atomicExpression 
      { 
      DebugLocation(119, 34); 
      PushFollow(Follow._atomicExpression_in_fieldExpression2203); 
      b=atomicExpression(); 
      PopFollow(); 
      if (state.failed) return retval; 
      if (state.backtracking == 0) stream_atomicExpression.Add(b.Tree); 

      } 

private AstParserRuleReturnScope<CommonTree, CommonToken> atomicExpression() 
{ 
    EnterRule_atomicExpression(); 
    EnterRule("atomicExpression", 10); 
    TraceIn("atomicExpression", 10); 
    AstParserRuleReturnScope<CommonTree, CommonToken> retval = new AstParserRuleReturnScope<CommonTree, CommonToken>(); 
    retval.Start = (CommonToken)input.LT(1); 

作爲的結果這,我得到以下錯誤

Error 2 Cannot implicitly convert type 'Antlr.Runtime.AstParserRuleReturnScope<Antlr.Runtime.Tree.CommonTree,Antlr.Runtime.CommonToken>' to 'Antlr.Runtime.CommonToken' 
Error 3 'Antlr.Runtime.CommonToken' does not contain a definition for 'Tree' and no extension method 'Tree' accepting a first argument of type 'Antlr.Runtime.CommonToken' could be found (are you missing a using directive or an assembly reference?) 

您的幫助解決這個問題將不勝感激。

回答

1

問題就出在你的語法:

b=(atomicExpression) 

這句法說b持有匹配一組,這永遠只能工作,如果括號內的值是一個令牌(或令牌集)的結果。改爲:

b=atomicExpression 
相關問題