How to get a standard C compiler with preprocessor through ANTLR?

263 views
Skip to first unread message

song...@gmail.com

unread,
Mar 20, 2017, 11:39:54 PM3/20/17
to antlr-discussion

Thanks for the answers of my last question.
But I still confuse about how to get a standard C compiler by using ANTLR.
I already have the C preprocessor and C parser grammar downloaded from the
antlr3 official website. Trying to combine them to get a full c compiler.
From what I learn these days, I found two ways to do this:
1. Rewrite the C parser grammar based on C preprocessor output file.
    (I found someone get this done by writing a C parser based on the .i file created 
     by gcc using gcc -E command.) 
2. "Combine the preprocessing engine with a lexer."
      This way seems to separate the preprocessor and parser in logic, but combine
      together in ANTLR.

I am planing to try both for learning. Can someone give me more details or advices?
For the first way I am stuck on how to get C preprocessor output as the output file created by antlr
just can recognize the input.
Should I add some ANTLR action or write a tree grammar in ANTLR to create an output file, And then
 the rewrite the C parser grammar to parser the output file?
 
For the second way seem I need to write a Lexer grammar which combine the C preprocessor, and then write a parser
for it. That means I should rewrite all the grammar, it will be a huge work to me.

Mike Lischke

unread,
Mar 21, 2017, 4:57:31 AM3/21/17
to antlr-di...@googlegroups.com
Thanks for the answers of my last question.
But I still confuse about how to get a standard C compiler by using ANTLR.
I already have the C preprocessor and C parser grammar downloaded from the
antlr3 official website. Trying to combine them to get a full c compiler.
From what I learn these days, I found two ways to do this:
1. Rewrite the C parser grammar based on C preprocessor output file.
    (I found someone get this done by writing a C parser based on the .i file created 
     by gcc using gcc -E command.) 
2. "Combine the preprocessing engine with a lexer."

OK, then let me throw in again my solution: http://www.soft-gems.net/index.php/java/windows-resource-file-parser-and-converter, which is per se made for .rc files, but .rc and .h files are very similar wrt to preprocessing (e.g. can include other files, have to support preprocessor syntax etc.). It's an older solution (for ANTLR 2.7) but you might find the approach interesting. I used a custom  reader to handle preprocessor stuff which then delivers only the active text parts to the lexer.


song...@gmail.com

unread,
Mar 21, 2017, 11:39:01 PM3/21/17
to antlr-discussion
Thanks Mike!
Reply all
Reply to author
Forward
0 new messages