help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: for loops in C style


From: Bob Rossi
Subject: Re: for loops in C style
Date: Thu, 28 Feb 2008 14:09:53 -0500
User-agent: Mutt/1.5.15+20070412 (2007-04-11)

On Thu, Feb 28, 2008 at 03:16:40PM +0100, Ilyes Gouta wrote:
> Hi!
> 
> I'm writing a small grammar for a very simplified C language. My goal
> is to produce an AST once a script file is parsed by the flex/bison
> tools. I got almost everything working nicely, except for the C style
> for loops.
> 
> Let's assume that we have a rule like this:
> 
> unary:
>     const
> |   id
> ;
> 
> expr:
>     unary
> |   expr + expr
> |   expr - expr
> |   expr * expr
> |   expr / expr
> ;
> 
> assignment:
>     id '=' expr ';'
> ;
> 
> for_stmt:
>     for (assignment; expr; assignment) block
> ;
> 
> My problem is that I'd like to support loops with empty assignment and
> expr parts (i.e, always true loops and the likes) such as:
> 
> int a, b, c;
> 
> for (;;) {
>     c = a + b;
> }
> 
> but I couldn't find a way to identify an empty assignment (or
> expression) and to define the action associated (to push/construct a
> new node within my AST). How one would modify the grammar to take into
> account such a scenario?
> 
> Any ideas?

Add a full expression, fexpr, that can either be nothing, or an
expression.

Same with the assignment. However, an assignment is an expression, so I
think you shouldn't separate them out.

Bob Rossi




reply via email to

[Prev in Thread] Current Thread [Next in Thread]