[antlr-interest] How can Antlr Parser actions know the file names and line numbers from a C preprocessed file?

Jim Idle jimi at temporal-wave.com
Tue Apr 6 08:44:20 PDT 2010


Lots of ways but:

Derive your own token from CommonToken and add file number field to it;
Get the lexer to produce those tokens and the parser to accept them;
Build a file table in the lexer and refer to it in error messages;
Keep track of current file in the lexer in case you need error messages from the lexer;
Keep track of current line number in the inferred file by setting it in your PPLINE rule then incrementing it in your NEWLINE rule;
Set the line and file number at the end of each rule (or override the nextToken stuff to set this automatically);


Start down that path and you will see the best way for your requirements.

You don't say what target language you use but in the C target there are user fields for storing such additional information so you can do the same thing as deriving a token.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Terry Arnold
> Sent: Tuesday, April 06, 2010 7:30 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How can Antlr Parser actions know the file
> names and line numbers from a C preprocessed file?
> 
> I’m writing a lint program for an in-house language whose source files
> are preprocessed just like C/C++. Can someone point me in the direction
> of figuring out how my parser actions can know what file and line
> number the current token(s) are really from?
> 
> 
> The lexer knows all about it from the preprocessor “#line” entries (for
> example):
> 
> #line 34 “includeFile.h”
> 
> in the preprocessed file (“sourceFile.i”). But how can that information
> be given to the parser such that while parsing ‘sourceFile.i”, line
> 332, a parser action can report that, say,
> 
> includeFile.h(40) : warning : Local variable ‘temp’ declared but not
> used
> 
> How can the lexer tell the parser that the file and line number the
> current token is from is actually “includeFile.h”, line 40, and not
> “sourceFile.i”, line 332?
> 
> My lexer:
> 
> PPLINE  // preprocessor #line. update the parser with the file name and
> line number
>     :   '#line' WS+ INT WS+
> {input.setLine(Integer.parseInt($INT.text));}
>             STRING  {currentFile = $STRING.text;}
>         {  $channel=HIDDEN; }
>     ;
> 
> input.setLine() compiles but I don't know yet if that is the right way
> to do it. If it is then it seems all I need is to update the parser
> with the file name (STRING).
> 
> Thanks for your help,
> 
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list