Re: [Newbie] LL(1) and if-then-else

"Thomas W. Christopher" <tc@charlie.cns.iit.edu>
3 Apr 1997 14:11:16 -0500

          From comp.compilers

Related articles
[Newbie] LL(1) and if-then-else morin@ufr-info-p7.ibp.fr (1997-03-31)
Re: [Newbie] LL(1) and if-then-else salomon@nickel.cs.umanitoba.ca (1997-04-02)
Re: [Newbie] LL(1) and if-then-else tc@charlie.cns.iit.edu (Thomas W. Christopher) (1997-04-03)
| List of all articles for this month |
From: "Thomas W. Christopher" <tc@charlie.cns.iit.edu>
Newsgroups: comp.compilers
Date: 3 Apr 1997 14:11:16 -0500
Organization: Illinois Institute of Technology
References: 97-03-177
Keywords: parse, LL(1)

MORIN Bruno wrote:
> Currently studying compilers, we need for a project to resolve the 'if
> - then - else' ambiguity. We are programming a mini-compiler based on
> a LL(1) grammar and are unable to resolve the ambiguity raised by an
> 'else' statement.


It can't be removed, but you can still use LL(1) parsing by "cheating".


s -> if e then s else s | if e then s | ....
is rewritten into
s-> if e then e elseOpt | ....
elseOpt -> else s | empty


Now for elseOpt, there is a conflict between the first set of "else s"
and the follow set of s (for the empty alternative).


Have the parser generator (or you, if you're doing this by hand)
prefer a right hand side selected by the first set over than selected by
the follow set. In this case, let else select "else s" rather than the
empty RHS.


I'm sorry, this is a little brief. I've discussed it more fully in the
documentation on TCLL1, an LL(1) parser generator. You can get the
document and the parser generator (written in and targeting Icon
currently) from
http://www.iit.edu/~tc/toolsfor.htm


--
-Thomas W. Christopher http://www.iit.edu/~tc
                                                tc@charlie.cns.iit.edu
--


Post a followup to this message

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