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

Terry Arnold terryarno at yahoo.com
Tue Apr 6 07:30:18 PDT 2010


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,


      


More information about the antlr-interest mailing list