Fwd: [antlr-interest] How to set filename in parser

Ric Klaren ric.klaren at gmail.com
Thu Jul 14 00:27:12 PDT 2005


hmmm more coffee....

---------- Forwarded message ----------
From: Ric Klaren <ric.klaren at gmail.com>
Date: Jul 14, 2005 9:26 AM
Subject: Re: [antlr-interest] How to set filename in parser
To: shmuel siegel <antlr at shmuelhome.mine.nu>


On 7/13/05, shmuel siegel <antlr at shmuelhome.mine.nu> wrote:
> My grammar parses the output of a c preprocessor. It interprets the
> #line directives so that syntax errors can be easily tracked back to the
> original file. My lexer sets the filename and linenumber parameters
> appropriately and then treats the directive like a single line comment.
> This works fine for the lexer; when an error occurs, the proper error
> message is printed. But the parser never finds out about this
> resequencing, so syntactical errors at the parser level do not reflect
> the original file information. Short of overriding the various match
> routines, is there any simple way to propagate the #line information
> from the lexer to the parser?

If I recall right working example code is available in
http://sourceforge.net/projects/antlraux/

If you set the line info correct in the lexer inputstate then tokens
should be created with the right line/column information. I don't
recall of the top of my head if CommonToken had a filename attribute.
You might have to make a custom token class and override makeToken in
the lexer to put the filename in the token as well.

After that you may have to override reportError and friends in the
parser not sure about that though.

Cheers,

Ric


More information about the antlr-interest mailing list