delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/25/16:55:18

From: "Christopher Nelson" <paradox AT gye DOT satnet DOT net>
To: <djgpp AT delorie DOT com>
Subject: Re: Bison and particular expressions.
Date: Thu, 24 Jun 1999 11:54:59 -0600
Message-ID: <01bebe6a$ad62d9a0$LocalHost@thendren>
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.71.1712.3
X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3
Reply-To: djgpp AT delorie DOT com

.. sorry, i didn't see this mesage at first.  :-)



>int a,b,c;
>int main(int a, b c, d e);
>
>Each line is syntactically correct by itself (assuming 'b' & 'd' are
>defined types for line #2).  The problem is, my bison program looks for
>"type id, id," and when it hits the main line, it gets a parse error
>because 'c' is not a comma.  If you really want to look at the whole
>code, ask and I'll post it.  But, I think this small program illustrates
>the problem.
>
>%%
>file:
>| file star
>| file and
>;
>
>starB: '*';
>
>andB: '&';
>
>equalB: '=';
>
>commaB: ',';
>
>star: starB
>| starB and
>| starB andB commaB
>;


^^^^ this is what i was talking about in my other e-mail.  the longest
should go first, unless you're using recursion.

>and: andB commaB
>;

the proper way to have a list of this type is to do the following:

listitem: item
|    listitem ',' item
;

listofitems: listitem
;

in addition, it is partially a lexical problem.  c is a very complex
language to parse specifically because of typedef's and some special-case
problems.  one part of what you should do is have the lexical-analyzer
understand id contexts.  that way you can write the recursive list like
this:

parameteritem:  typedefid   id
|    class id                                    /* class=int,float,short,
etc. */
|    id
;

parameterlist:    parameteritem
|    parameterlist ',' parameteritem
;

functiondec:    typedefid    id    '(' parameterlist ')'
|                    class          id    '(' paramterlist ')'
;


the lexical analyzer in this case would do the following:

{ID}    res=findid(yytext);    if (is_typedef(res)) return(typedefid); else
return(id);

you can extend this to include different types of variables, structures,
etc.  this is the way that most clean compilers implement the problem of
context.  there is an excellent c-grammar that has very few reduce-reduce
and shift-reduce problems in it, and it would probably be good to look at
it.  unfortunately, i've forgotten it's name.  it's something like Roseburg
or Rosen or something.  :-)  in any case, this is how he chose to resolve
the problem of type-def's.

    -={C}=-

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019