[antlr-interest] Slightly different C-Parser

Jim Idle jimi at temporal-wave.com
Tue Feb 5 08:25:31 PST 2008


Starting with the GNU C grammars isn’t a bad idea if you don’t feel up
to writing one from scratch. There is the basis of a C parser for ANTLR3
in the downloadable examples tar (see the downloads page). However some
of the situations you pose here (such as multiple ambiguous macro
definitions) might make the job impossible. You need a pre-processor for
C or would need to just parse the macro references without expanding
them and take them as read. A lot of semantic checking would just be
impossible. 

 

However it should be perfectly feasible to write a parser that will skip
includes if they are not there and just parse the structure of C,
assuming that the macros were written correctly and expect ‘;’ after
them (some people write macros with code in that contains the ‘;’). Then
ANTLR3 will recover automatically from a lot of common errors such as a
missing ‘;’ and so on., so you should get a long way without too much
refinement. 



It can be a useful technique to program the structure of syntax errors
that are common, where you can do so without creating lots of
ambiguities. This will allow very specific messages to be produced.
Generally you want your parser to accept just about any old junk that
seems like it might be syntactically valid, then your semantic phase can
be quite specific about the problems it finds. So, when you see a
language spec that says something like ‘generics can’t be used here’,
just use a common rule that allows them anyway and avoids ambiguity,
then your semantic check will pick up the illegal use and flag it. When
you are trying to do a partial parse like this, the semantics will be
more difficult and you may be given code that would not be in error if a
macro was defined. 

 

If possible, you should look to see if you cannot just get full source,
but I assume you have valid reasons for this partial parse. Assuming
that there is some specific purpose for this tool, then you might
consider writing a lexer and grammar that is much more specific to that
purpose, perhaps only pulling out the things you are inspecting from the
lexer (if possible) or perhaps you can write a pre-processor that fills
in missing elements (or takes partial definitions away or something).
Whether or not a good place to start for something like this is the
existing grammar depends on what you are trying to do I think.

 

 

Jim

 

From: Swapna R (GS-EC/EDG5 - RBIN/EMT2) [mailto:R.Swapna at de.bosch.com] 
Sent: Tuesday, February 05, 2008 7:53 AM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Slightly different C-Parser

 

Dear All,

 

 I think my question was not clear. 

 

 I downloaded the GNU C grammars but it is not supported in antlrworks
1.1.6. Can we find these grammars for this version ?

 

 The parser we need has to work in a environment where some of the
includes are missing and also for one single macro there could be
multiple definitions. 

 

 So for these to satisfy we would require to take certain deviations
from the GNU C parser. IS this a good idea ? Do you suggest that we
should write entire parser according to our needs ?

 

 Anyone of you please respond.

 

regards,

Swapna

 

  _____  

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Swapna R
(GS-EC/EDG5 - RBIN/EMT2)
Sent: Tuesday, February 05, 2008 8:33 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Slightly different C-Parser

Hello, 

 For my project work I need to parse C-files. Sometimes It can be that
not all header files are present. I have found the GNU-C grammars. 

 Is it a good idea to change these grammars for specific use-cases ? How
do you suggest me to go ahead ? 

 Incase of code not conforming to C-syntax instead of generating a
exception I want it to continue further and just creating a problem
statment for the problematic code.

Can you anyone of you help me ? This is very important as we are under
pressure to take decision about the parser. 

Regards, 
Swapna 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080205/49bf0f2a/attachment-0001.html 


More information about the antlr-interest mailing list