Puneet
Puneet

Reputation: 472

VS2012 ANTLR Language Support generated parser does not compile

I am using VS2012 with the ANTLR Language Support and ANTLR C# files version 3.5.0.2 to generate the C# code for the lexer and parser.

My Grammar contains the following (only sections given here)

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))

When I generate the C# code, I get

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);

            }

and

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);

As a result of this, I get the following errors

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

Your help in resolving this would be most appreciated.

Upvotes: 1

Views: 521

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99869

The problem lies in your grammar:

b=(atomicExpression)

This syntax says b holds the result of matching a set, which only ever works if the value in parenthesis is a token (or set of tokens). Use this instead:

b=atomicExpression

Upvotes: 1

Related Questions