[antlr-interest] AST construction in C++

Dave Bailey dave at daveb.net
Thu Feb 19 11:21:49 PST 2004


Hi,

I've been trying to get a simple parser/lexer working in C++ with
buildAST = true.  I'm missing something and I don't know what it is. 
Given the following grammar:

test.g
====================================================================
options { language = "Cpp"; }
class test_parser extends Parser;
options { exportVocab = test_; buildAST = true; }
test : (line)* ;
line : ID EQUALS Number SEMI ;
class test_lexer extends Lexer;
options { k = 2; exportVocab = test_; }
EQUALS : '=' ;
SEMI   : ';' ;
WS	   : (' '|'\t'|'\n')
{$setType(ANTLR_USE_NAMESPACE(antlr)Token::SKIP);} ;
Number : ('0'..'9')+ ;
ID     : ('a'..'z')+ ;
====================================================================

and the following C++:

main.cpp
====================================================================
#include <iostream>
#include <sstream>
#include "test_lexer.hpp"
#include "test_parser.hpp"

using namespace std;

int
main ( int argc, char **argv )
{
  ostringstream ost;

  ost << "a = 1;" << endl << "b = 2;" << endl << "c = 3;" << endl;

  istringstream  ist(ost.str());
  test_lexer     lexer(ist);
  test_parser    parser(lexer);
  parser.test();

  return 0;
}
====================================================================

I build with antlr 2.7.2 as

antlr test.g
g++ -g -Wall *.cpp -o test -lantlr

and run ./test, which dumps core.  It doesn't dump core if I turn off
buildAST.  Stack trace shows the problem is somewhere in AST code, I'm
failing to initialize something.  Can someone show me what lines of
C++ I need to add to main.cpp to get the program to build ASTs without
segfaulting?  I would really appreciate the help.  Thanks-

-dave





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list