[antlr-interest] passing stuff from lexer to parser

siemsen at ucar.edu siemsen at ucar.edu
Tue Jan 1 13:02:51 PST 2008


Is there an elegant way to pass extra information from the lexer  
phase to the parser phase of an ANTLR translator?

My input is a bunch of source files in a directory tree.   I want to  
output a new directory tree with the same layout as the source  
directory tree.  In the top-level source directory is a special file  
that "includes" all the other files in the source directory tree in  
the proper order.  It contains lines like

#pragma include ("qualifiers_optional.mof")
#pragma include ("Core/CIM_ManagedElement.mof")
#pragma include ("Core/CIM_ManagedSystemElement.mof")
#pragma include ("Core/CIM_SystemStatisticalInformation.mof")
#pragma include ("Database/CIM_CommonDatabaseSettingData.mof")
#pragma include ("Database/CIM_CommonDatabaseStatistics.mof")
#pragma include ("Database/CIM_DatabaseResourceStatistics.mof")

etc.

To handle the include statements, I use the mechanism described in  
the ANTLR Wiki page titled "How do I implement include files?".  It  
works great.  It does its magic during the lexer phase.  So all the  
source files are lexed first into one big token stream, then the  
parser starts.

Currently, my translator just emits output to standard out, as one  
text stream.  Now I'm ready to make it put the output into  
directories and files.  The source text is a set of things with names  
like CIM_DatabaseResourceStatistics, so I know what to name each  
output file.  I just need to know what directory to put each output  
file in.

During the lexer phase, I store the name-to-directory information in  
a HashMap.  So for example, the HashMap tells me that the output file  
named CIM_DatabaseResourceStatistics.java belongs in the output  
subdirectory named "Database".

I need to pass the HashMap from the lexer to the parser.  Is there a  
good way to do it?  Am I thinking about the problem correctly?

-- Pete

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080101/30e891d2/attachment.html 


More information about the antlr-interest mailing list