[antlr-interest] Creating AST with third party grammar

John Pool j.pool at ision.nl
Wed Feb 10 22:49:29 PST 2010


I strongly advise you to purchase 'The Definitive ANTLR Reference'. C#
example below.

Cheers -- John Pool

  MyLexer lexer = new MyLexer (new ANTLRStringStream ("my code to parse")); 
  // ... or ANTLRFileStream ("my file to parse")
  CommonTokenStream cts = new CommonTokenStream (lexer);
  MyParser parser = new MyParser (cts);
  MyParser.member_return returnValue = parser.member(); // or whatever
'root' rule you want to parse
  tree = (CommonTree)returnValue.Tree;
  string treeAsString = tree.ToStringTree ();

  AntlrExceptions.CheckForErrors (); // my own error handling class ...
  // ... contains overridden versions of ReportError() for the parser, lexer
and treeparser

  // depending on whether you need a treeparser (AST parser):
  CommonTreeNodeStream treeNodeStream = new CommonTreeNodeStream (tree);
  JclTreeParser treeParser = new JclTreeParser (treeNodeStream);
  treeNodeStream.TokenStream = cts;

  AntlrExceptions.CheckForErrors ();



-- 
View this message in context: http://n2.nabble.com/Creating-AST-with-third-party-grammar-tp4524691p4553093.html
Sent from the ANTLR mailing list archive at Nabble.com.


More information about the antlr-interest mailing list