Question

Hopefully this is a really quick one ;) I have written a lexer / parser specification in ANTLR3, and am targeting the CSharp2 target. The generated code works correctly, but I can't get ANTLR to put the C# output into a namespace.

The relevant section of the Grammar file is as follows:

grammar MyGrammar;

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

To generate the correct namespace, I have 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.

Was it helpful?

Solution

I use this for a combined lexer and parser (and it generates the 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.

OTHER TIPS

It seems that the @namespace directive needs to be placed AFTER the tokens{} block. All good now...

With language = 'CSharp3'; (and CSharp2 as well), you can do:

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

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

which generates:

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

at the end of lexer and parser code. If I write:

@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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top