Help - partial parsing of C

Daniel Tallis <dta@dcs.exeter.ac.uk>
Mon, 20 Jan 92 15:47:32 GMT

          From comp.compilers

Related articles
Help - partial parsing of C dta@dcs.exeter.ac.uk (Daniel Tallis) (1992-01-20)
Re: Help - partial parsing of C e-sink@uiuc.edu (1992-01-22)
Re: Help - partial parsing of C al@nmt.edu (1992-01-24)
| List of all articles for this month |
Newsgroups: comp.compilers
From: Daniel Tallis <dta@dcs.exeter.ac.uk>
Keywords: C, parse, question
Organization: Compilers Central
Date: Mon, 20 Jan 92 15:47:32 GMT

Help!


As part of my third year project I need to do partial parsing of C
programs. By partial parsing, I mean all I need to do is delimit each
individual statement and determine its type (eg. for statement, while
statement, do statement, assignment, compound statement etc.). I will
then generate a tree reflecting the structure of the program (but I'm
happy I can do that bit).


To clarify, an example:


>From the code fragment:


if (expr)
{
    stmt1;
    stmt2;
    stmt3;
}
else
    stmt4;


... I want to do sufficient parsing to be able to generate a tree like
this:


                              if-statement
                                      / \
                then-clause else-clause
                        | |
        compound-statement stmt4
              / | \
    stmt1 stmt2 stmt3


I also, at the same time, want to pull out each use of each variable and
classify the use as one of declaration, function parameter, LHS in
assignment, RHS in assignment, LHS in expression, RHS in expression.


The reason for wanting to do this is that the project is a 'smart editor'
which 'understands' the program to a certain extent. One of the
operations the user can perform on his text is 'go to next statement'
which highlights the next statement in execution order. At IF and SWITCH
statements the user makes a choice as to which option to follow etc.
Operations on variables are things like 'go to the declaration of this
variable', 'trace through in execution order the statements where this
variable is changed' etc.


At the moment I'm thinking along the lines of a recursive descent type
parser. Speed of execution is very important, as is ease of
implementation, so I want to avoid doing any more than is necessary. I
strongly suspect YACC etc. is not what I want.


Does anyone out there know if anyone has done anything like this? I would
be very interested in any references/help/suggestions you might have to
offer... Email would be great.


Thanks in advance,


Dan.


[I suspect that the easiest thing to do is to pick up one of the freely
available yacc parsers listed in the comp.compilers FAQ and use that. You
can find statement boundaries lexically pretty easily by looking for
semicolons, and matching parens and braces. If you want to identify all
the statement types distinguish between def and ref mentions of a name
you're going to have to parse 90% of the language anyway. Yacc is no
speed demon but many people find it to be fast enough. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.