我将 VS2012 与 ANTLR 语言支持和 ANTLR C# 文件版本 3.5.0.2 一起使用来为词法分析器和解析器生成 C# 代码。
我的语法包含以下内容(此处仅给出部分)
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?)
您在解决此问题方面的帮助将不胜感激。