converting precedence to productions

mark@ora.on.ca (Mark Saaltink)
3 Oct 1996 22:58:22 -0400

          From comp.compilers

Related articles
converting precedence to productions mark@ora.on.ca (1996-10-03)
Re: converting precedence to productions brianb@microware.com (Brian Bliss) (1996-10-06)
Re: converting precedence to productions faiman@zko.dec.com (Neil Faiman) (1996-10-08)
Re: converting precedence to productions annika@cs.chalmers.se (1996-10-10)
| List of all articles for this month |

From: mark@ora.on.ca (Mark Saaltink)
Newsgroups: comp.compilers
Date: 3 Oct 1996 22:58:22 -0400
Organization: Compilers Central
Keywords: parse, question

I am trying to convert an operator precedence grammar to an
unambiguous grammar without precedence rules. This is usually easy
(e.g. expressions with normal precedence rules), but my grammar has a
low-precedence postfix operator.


The grammar looks something like this:


    S ::= n | S and S | S or S | S post | ( S )


with left associativity, and precedence decreasing to the left.


Clearly, "A and B or C" must be parsed as "(A and B) or C".


It is less clear how "A or B post and C" should be parsed; it might
reasonably be "((A or B) post) and C" or "A or ((B post) and C)".
Assuming the former is intended, how can this grammar be transformed to
an unambiguous grammar without precedence rules?


A naive approach defines precedence numbers, and results in


  S ::= S1 | S post


  S1 ::= S2 | S1 "or" S2


  S2 ::= S3 | S2 "and" S3


  S3 ::= n | ( S )


which does not work at all here; "A post or B" is rejected.
--


Post a followup to this message

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