Yacc and a calculator

Dan Weinlader <weinlade@fugue.stanford.edu>
13 Feb 1996 19:19:55 -0500

          From comp.compilers

Related articles
Yacc and a calculator weinlade@fugue.stanford.edu (Dan Weinlader) (1996-02-13)
Re: Yacc and a calculator nr@cs.purdue.edu (1996-02-14)
Re: Yacc and a calculator foggia@amalfi.dis.unina.it (1996-02-16)
| List of all articles for this month |

From: Dan Weinlader <weinlade@fugue.stanford.edu>
Newsgroups: comp.compilers
Date: 13 Feb 1996 19:19:55 -0500
Organization: Compilers Central
Keywords: yacc, parse, question

I have been reading the lex & yacc book by Levine, Mason and Brown and have
a question on how to extend the calculator example. Suppose I wish to allow
the user to enter 8 5 and have it parsed as 8*5, how do I do that?

I have tried:

expression: ...
                | ...
                | expression expression { ... }

but that produces shift/reduce conflicts. The problem is not precedence,
but associativity, right? So how do I tell Yacc (Bison to be exact) what
the assocativity is for this expression? I tried %prec as shown in the book
using UMINUS be that only reduced the shift/reduce conflicts from something
like 16 to 8.

In case you wondering, this is not a HW problem. I am actually writing a
parser for a full text search query language and want an implied AND between
words. Since my problem mapped so well to the calculator, I thought that
would be a clearer way to ask the question.


Dan Weinlader


Post a followup to this message

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