1

我将 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?) 

您在解决此问题方面的帮助将不胜感激。

4

1 回答 1

1

问题在于你的语法:

b=(atomicExpression)

此语法表示b保存匹配集合的结果,仅当括号中的值是标记(或标记集)时才有效。改用这个:

b=atomicExpression
于 2013-04-15T15:59:30.470 回答