Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!rutgers!sri-spam!ames!ucbcad!ucbvax!decvax!ima!johnl
From: johnl@ima.UUCP (Compilers mailing list)
Newsgroups: mod.compilers
Subject: Re: advice needed re: parsing C decl syntax
Message-ID: <290@ima.UUCP>
Date: Wed, 10-Dec-86 17:23:48 EST
Article-I.D.: ima.290
Posted: Wed Dec 10 17:23:48 1986
Date-Received: Sat, 13-Dec-86 21:48:41 EST
References: <284@ima.UUCP>
Reply-To: Pedz Thing 
Organization: Digital Lynx; Dallas TX
Lines: 39
Approved: 

I recently wrote a C interpreter (which of course must parse also).  I
found the particular problem mentioned to not be that bad.  The
declaration of a function (or a variable) and the actual definition of
a function was handled by the same code.  Roughly I had a routine
which I called declaration.  This called something which picked up the
declarative part (like the int i or the char *p(arg)).  I put the list
of arguments in a place to be found later by `declaration'.  Then I
would look to see if I found a function and if I was not looking at a
semi-colon.  If that was the case, I assumed I had a function
definition and parsed it.

The part I found to be tricky was the code which picked up the `int i'
or the `char *p[4](arg)'.  It turns out that this is not very
recursive.  I did this in a loop which continued until I did not see
anything I liked.  In that look I picked up the *'s, followed by a
name, followed by the []'s or ()'s for functions.  The ()'s used for
grouping was the reason for the loop.  If you want more details, I
could send you part of the code.

However, I would like to end with a statement about yacc.  As was
pointed out, the problem with yacc is not the speed nor the size.  It
is rather difficult to write a good grammar for C which yacc will
accept and still provides useful screening of illegal input.  I have
written one which correctly knows about lvalue's.  It knows the
difference between functions and pointers to functions so that the
items inside a struct (for example) are always pointers to functions
and not functions.  It also has 0 shift/reduce conflicts (as reported
out of yacc).  All the other grammars I have looked at fail miserably
in these reguards.  The code produced (tables and driver) is 8K I
think.  (The reason I did not use it was because of legal problems
with ATT which I still have not heard a definate answer on.)
-- 
Perry Smith
{convex!ctvax,{ti-csl,infotel}!pollux}!bobkat!pedz
-- 
Send compilers mail to ima!compilers or, in a pinch to Levine@YALE.EDU
Plausible paths are { ihnp4 | decvax | cbosgd | harvard | yale | bbncca}!ima
Please send responses to the originator of the message -- I cannot forward
mail accidentally sent back to compilers.  Meta-mail to ima!compilers-request