Re: Simple Expression Recursion vs Expression/Term/Factor

Vinay Kakade <kvinay@ip.eth.net>
6 Aug 2001 04:00:36 -0400

          From comp.compilers

Related articles
Simple Expression Recursion vs Expression/Term/Factor stoggers@uniquest.demon.co.uk (Mike Stogden) (2001-08-02)
Re: Simple Expression Recursion vs Expression/Term/Factor kvinay@ip.eth.net (Vinay Kakade) (2001-08-06)
| List of all articles for this month |
From: Vinay Kakade <kvinay@ip.eth.net>
Newsgroups: comp.compilers
Date: 6 Aug 2001 04:00:36 -0400
Organization: Compilers Central
References: 01-08-016
Keywords: parse
Posted-Date: 06 Aug 2001 04:00:36 EDT

Hi,


The grammar
expr -> expr + expr | expr * expr | id | num
is an ambiguous one. For example, for the string "id + id * id", it generates
two different parse trees.


On the other hand, the grammar involving term, factor is an umambiguous one. It
generates a unique parse tree for the above string.


By the way, if you are using yacc, you can resolve the ambiguity in first case
by giving precedences to "+" and "-" using %prec.


-Vinay Kakade.


Mike Stogden wrote:


> Please could someone offer an explanation as to the relative merits of
> describing expressions as productions based on two comparable
> specifications?
>
> I have seen expressions for the same language described as...
>
> expr -> expr + expr
> expr -> expr * expr
> etc...
>
> alternatively...
>
> expr -> expr + term
> term -> factor | term * factor
> factor -> number | identifier
> etc...


Post a followup to this message

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