ANTLR v3 C # Namespaces

Hope this is very fast :) I wrote the lexer / parser specification in ANTLR3 and aimed at the CSharp2 target. The generated code works correctly, but I can't get ANTLR to put C # output in a namespace.

The corresponding section of the grammar file is as follows:

grammar MyGrammar;

options
{
    language = CSharp2;
    output = AST;
    ASTLabelType = CommonTree;
}

To create the correct namespace, I tried:

@namespace { MyNamespace }

and

@lexer::namespace { MyNamespace }
@parser::namespace { MyNamespace }

but both of these generate errors, claiming that the file has no rules.

Any help is appreciated.

+3
source share
3 answers

I use this for a combined lexer and parser (and it generates a namespace correctly):

grammar Test;

options
{
    language=CSharp2;
}

@lexer::namespace {
    My.Name.Space
}

@parser::namespace {
    My.Name.Space
}


DIGIT   :   '0'..'9';

simple  :    DIGIT EOF;

So I wonder why your version didn't work - maybe you want to try this simple example and see if it works for you.

+5

, @namespace {}. ...

+4

language = 'CSharp3'; ( CSharp2) :

@lexer::namespace {
    My.Name.Space
}

@parser::namespace {
    My.Name.Space
}

:

} // namespace 
 My.Name.Space <-- compile error here

. :

@lexer::namespace {My.Name.Space}

@parser::namespace {My.Name.Space}

It works fine and generates:

} // namespace My.Name.Space <-- within the line comment, no error of course
+2
source

All Articles