Please note:The SCons wiki is in read-only mode due to ongoing spam/DoS issues. Also, new account creation is currently disabled. We are looking into alternative wiki hosts.

Note: the following is for ANTLR v2, not for ANTLR v3. It also assumes that you don't use the 'exportVocab' keyword in the 'options' block for your Lexer. It also can not be used if you want to create a shared library containing your lexer/parser, as the list of sources returned by the tool includes header files and text files, which G++ will refuse to package in a shared library. Tested Oct 2007 with 'cantlr' deb package on Ubuntu 7.04.

For a given antlr grammar (t.g):

   1 header {
   2 
   3 #include <iostream>
   4 
   5 ANTLR_USING_NAMESPACE(std)
   6 ANTLR_USING_NAMESPACE(antlr)
   7 
   8 }
   9 
  10 options {
  11   language="Cpp";
  12 }
  13 
  14 class IntAndIDLexer extends Lexer;
  15 
  16 INT        : ('0'..'9')+ ;
  17 ID             : ('a'..'z')+ ;
  18 COMMA      : ',' ;
  19 NEWLINE    : '\n' ;
  20 
  21 class SeriesParser extends Parser;
  22 
  23 series     : element (COMMA! element)* NEWLINE;
  24 element    : INT | ID;
  25 
  26 //class SeriesTreeParser extends TreeParser;
  27 //series: (INT | ID)+;
  28 

The following builder will build a static library for the parser for this grammar and link it with a main.cpp. Note that I am assuming C++ output here.

   1 import os
   2 import re
   3 
   4 antlr_res={};
   5 
   6 def make_re(classtype):
   7         return re.compile('^class\\s+(\\S+)\\s+extends\\s+'+classtype,re.MULTILINE);
   8 
   9 def append_re(res,classtype,fn):
  10         res[classtype]=[make_re(classtype),fn];
  11 
  12 def lexer_append(target,classname):
  13         target_append(target,classname);
  14         target.append(classname+'TokenTypes'+'.hpp');
  15         target.append(classname+'TokenTypes'+'.txt');
  16 
  17 def target_append(target,classname):
  18         target.append(classname+'.hpp');
  19         target.append(classname+'.cpp');        
  20 
  21 append_re(antlr_res,'Lexer',lexer_append);
  22 append_re(antlr_res,'Parser',target_append);
  23 append_re(antlr_res,'TreeParser',target_append);
  24 
  25 def antlr_emitter(target,source,env):
  26         target=[];
  27         for src in source:
  28                 contents = src.get_contents();
  29                 for type_re in antlr_res:
  30                         found = antlr_res[type_re][0].findall(contents);
  31                         for classname in found:
  32                                 antlr_res[type_re][1](target,classname);
  33         return (target,source);
  34 
  35 b = Builder(action="java antlr.Tool $SOURCE",src_suffix='.g',emitter=antlr_emitter)
  36 env = Environment();
  37 env.Append(BUILDERS={'antlr':b}); # NEED TO APPEND!
  38 env.Append(ENV={'CLASSPATH':os.environ['CLASSPATH']});
  39 antlr_sources=env.antlr(source='t.g')
  40 
  41 lantlr=env.StaticLibrary(target='tparser',source=antlr_sources);
  42 prog=env.Program(source=['main.cpp'],LIBS=['tparser','antlr'],LIBPATH='.');

And the output:

antlr $ scons -Q
java antlr.Tool t.g
ANTLR Parser Generator   Version 2.7.4   1989-2004 jGuru.com
g++ -c -o IntAndIDLexer.o IntAndIDLexer.cpp
g++ -c -o SeriesParser.o SeriesParser.cpp
ar r libtparser.a IntAndIDLexer.hpp IntAndIDLexer.o IntAndIDLexerTokenTypes.hpp IntAndIDLexerTokenTypes.txt SeriesParser.hpp SeriesParser.o
ranlib libtparser.a
ar: creating libtparser.a
g++ -c -o main.o main.cpp
g++ -o main main.o -L. -ltparser -lantlr

AntlrBuilder (last edited 2008-03-12 02:47:00 by localhost)