help-bison
[Top][All Lists]
Advanced

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

Re: Parsing a simple mathematical statement


From: Hans Aberg
Subject: Re: Parsing a simple mathematical statement
Date: Tue, 6 Mar 2007 22:36:43 +0100

On 4 Mar 2007, at 21:34, Abdelmalek Halawani wrote:

I would like to create a parser that reads the following input:

a = 1; b = 2; c = 3; d = 0;
y = a + b * c + d;

And outputs:

temp1 = b * c;
temp1 = 6;
temp2 = temp1 + d;
y = a + temp2;
y = 7;

I am not sure exactly what you want, but an operator expression can be computed by using two stacks, one for values and one for operators. Add an end-marker to the expression. When a value comes by, put it onto the values stack, when an operators comes by, check it against the operator on the stack, and compute if the precedence is higher, otherwise stack it on the operator stack. When the end-of the expression comes by, compute the rest. With this technique in hand, it is easy to convert to say RPN, if that is what you want. Your temporary registers will be the value stack.

  Hans Aberg






reply via email to

[Prev in Thread] Current Thread [Next in Thread]