...
- Create a new Visual Studio project
- Add these project references from the folder C:\ANTLR\antlr-3.1.3\runtime\CSharp\Libraries
- Antlr3.Runtime.dlldll (Antlr runtime 3.1.3)
- antlr.runtime.dll (Antlr runtime 2.7.7 for StringTemplate)
- StringTemplate.dll
- Antlr3.Utility.dll
- Start ANTLRWorks with a double click on the jar-file (antlrworks-1.2.3.jar)
- Copy the following test grammar into the new file. Note the language property in the options section of the grammar is what specifies C# targeted output.
grammar Test;
options
{language = 'CSharp';
output=AST;
}
expr : mexpr (PLUS^ mexpr)* SEMI!
;
mexpr
: atom (STAR^ atom)*
;
atom: INT
;
//class csharpTestLexer extends Lexer;
WS : (' '
| '\t'
| '\n'
| '\r')
{ $channel = HIDDEN; }
;
LPAREN: '('
;
RPAREN: ')'
;
STAR: '*'
;
PLUS: '+'
;
SEMI: ';'
;
protected
DIGIT
: '0'..'9'
;
INT : (DIGIT)+
; - Save this file as Test.g (the name of the file has to equal the name of the grammar) in your VS project directory.
- Add existing item Test.g (the file you just saved) to your project.
- Now go back to ANTLRWorks. Do Ctrl+R (check grammar): You should get a success message.
- Open the menu File->Preferences, choose tab General and set the output path to your Visual Studio Project directory. (In case you experience any Java related problems: check tab Compiler to set the path to javac). Click APPLY and close Preferences Window.
- Now try Ctrl+Shift+G (Generate Code). You should get a success message.
- Add the generated .cs files to your VS project.
- Back in Visual Studio you should be able to build your solution and work with the parser.
- Whenever you need to make changes to your grammar, you can do it in ANTLRWorks now and lexer and parser will be updated automatically in your project.
...