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

Re: grammar questions




Hi! Thanks for the help.

I'll look into using filter for parse time transformations, until now I
did it later using tree processing and alternative rules.

"Etienne M. Gagnon" wrote:

> > My second problem is greater and more difficult. I solved the dangling
> > if/else problem as it was done in java1.1 and thought that's it.
> > But there's more and i couldn't manage to make it work.
> >
> > The language has besides the 'if' and 'else' terminals also an 'elseif'
> > statement. So statements like that are valid:
> >
> > if (a) x(); elseif (b) y(); elseif (c) z(); else w();
> >
> > Now I got this working, correct I hope. The next thing with this
> > language is support for second style if statements that use colon:
>
> If I understand well, the ":" always follows the ")" of an "if" or
> "elseif" or follows an "else" .
>
> > Both kind of 'if' statements are allowed and by idea you could mix them.
> > How to do this?
>
> I see the following quick solution.
>
> if_stmt =
>   {if}            if lpar expr rpar colon? stmt
> | {else}          if lpar expr rpar colon? noif_stmt else colon? stmt
> | {elseif}        if lpar expr rpar colon? noif_stmt elseif_stmt
> ;
>
> elseif_stmt =
>   {if}            elseif lpar expr rpar colon? stmt
> | {else}          elseif lpar expr rpar colon? noif_stmt else colon?
> stmt
> | {elseif}        elseif lpar expr rpar colon? noif_stmt elseif_stmt
> ;
>
> etc.
>
> In other words, you use introduce an optinal colon in your elseif
> solution.  Again, I have not tried it.  Please report your findings back
> to us.

Unfortunately it's not that easy. The two kind of 'if' statements are
different statements, adding an optional colon won't do much good.

You can't mix colon and noncolon keywords. And what more, the
colon is something like '{', it allows you to group more statements
together. And you forgot the 'endif'.

So something like that would be equivalent and valid:

---with colon---

if (a):
  x = y;
  z = d;
endif;

---without colon---

if (a) {
  x = y;
  z = d;
}


---now a real nut to crack - mixing (valid theoreticly, tho
       not in current php language implementation using bison):---

if (a):
  if (c) x = y;
elseif (b):
  z = d;
endif;

In this last code please note that the second if(c) x = y; has
elseif following it. Which way to go? There's colon for one,
but how to make it work for the parser?

Regards,
Indrek