Re: Simple parser for C/C++



On Sep 28, 8:59 pm, Esa Heikkinen <esa.heikki...@xxxxxx> wrote:
Michael already directed you to the page on the wiki which is probably
the best starting point for parsing C.

Ok .. i understand, but i think it would be exist simpler way to detect
only things i want. I don't want to parse all code. Maybe i do not need
parser at all ?

Unfortunately C is not "partially parsable", because the same token
from the lexer can have wildly different syntactic roles depending on
the surrounding tokens.

So, what to do if you don't want to use and augment an existing full-
fledged C/C++ parser ?

Two radically different approaches:

1) Obstinacy: compile it with a true C compiler, with debugging info
enabled, and then analyze the resulting object files to extract line
number information for beginning and end of functions. In unix I'd do
this with gcc and objdump.

2) Pragmatism: give style rules to your developers, like in doxygen et
al., so that the interesting parts of the code become subject to
simple grepping (or regexp, I suddenly remember we are on c.l.t ;-).
Notice that the style rules may be much simpler than doxygen's comment-
typing, e.g by playing only with indentation:

/* uninstrumented beginning */
#include <stdlib.h>
...

/* ============ HERE_BEGINS_INSTRUMENTED_PART == */

int
foo
(char *a,void *b,float c)
{
/* indented body */
}

The code above is just slightly disturbed by the constraints of your
goal. So it is 99% as readable for humans (some would say 150% ;-),
and 500% for Tcl...
(Now of course if you have read-only access to the code, this is not
an option.)

-Alex

.



Relevant Pages

  • Re: SPCI Parser
    ... Let's say it won't be a SPCI parser... ... This looks like a really small subset of SPCI. ... Lex/Yacc couldn't compile and fit in an ATmega 16? ...
    (comp.arch.embedded)
  • Re: Parser
    ... >> In comp.programming I asked for advice for writing a parser. ... >> someone showed me wouldn't compile. ... >> But that's my opinion. ... > type FILE. ...
    (comp.lang.c)
  • Re: Parse library
    ... sure, but if you aren't trying to compile to MSIL or anything, it'd be ... xsd.exe way and generate a config class that loads and writes out the config ... or more interestingly a parser generator based ... Its the parser generator that mono uses. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Other notes
    ... >>can affect the parser's compile time behavior, ... But the parser executes before the compiled program runs, ... before the operator definition has been executed? ...
    (comp.lang.python)