[antlr-interest] parsing preprocessed C file

Stephen Siegel siegel at udel.edu
Thu Apr 19 07:00:23 PDT 2012


I want to parse a C file that has already gone through the C preprocessor.  My question is, how do I get my parser to associate the correct files and line numbers to each token?

I found this question and an answer in one of the FAQs (http://www.antlr.org/wiki/display/ANTLR3/FAQ+-+C+Target):

------------------------------------- begin quote -------------------------------------

One way is to follow this path:

	• 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 thelexer;
	• 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.

In the C target there are user fields for storing such additional information so you can do the same thing as deriving a token.

-------------------------------------- end quote --------------------------------------

I was wondering if anyone has actually done this, and can share the result?   Or, are there any examples around that do any of the things above that might help me figure out how to do this?

I'm using the Java target.
Thanks.



More information about the antlr-interest mailing list