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

shmuel siegel antlr at shmuelhome.mine.nu
Thu Jul 14 01:47:27 PDT 2005


>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
>
>  
>
Unfortunately, at least in my case it is even worse than that. If antlr 
tries to determine the correct path with a switch failure occurs by    
        default:
        {
            throw new NoViableAltException(LT(1), getFilename());
        }
   This means that I have to either manage to set parsers filename, 
(remember the parser never sees the #line directive) or override 
getFilename. The latter sounds easier but then I have to find it. It is 
saved in ParserSharedInputState as a protected member. So given your 
answer, it looks like I will also have to save it locally in the 
generated lexer in a way that the parser can reach it.


-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.8.14/48 - Release Date: 7/13/2005



More information about the antlr-interest mailing list