Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Create a new Visual Studio project
  2. 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
  3. Start ANTLRWorks with a double click on the jar-file (antlrworks-1.2.3.jar)
  4. 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)+
    ;

  5. Save this file as Test.g (the name of the file has to equal the name of the grammar) in your VS project directory.
  6. Add existing item Test.g (the file you just saved) to your project.
  7. Now go back to ANTLRWorks. Do Ctrl+R (check grammar): You should get a success message.
  8. 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.
  9. Now try Ctrl+Shift+G (Generate Code). You should get a success message.
  10. Add the generated .cs files to your VS project.
  11. Back in Visual Studio you should be able to build your solution and work with the parser.
  12. 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.

...