0
votes

I could do simple scanner and parser using bison and flex like a simple calculator for example I can give input (to the exe which was generated after compiling the lex.yy.c and y.tab.c), 3+4*5, the exe would display the result as 23. But I don't know how to do the following:

x = 0;
while (x < 3) {
    print x;
    x = x + 1;
}

using flex and bison. I want the exe generated to print x, until the while loop condition fails. How do I do that? Thanks in advance.

1
possible duplicate of Parsing a While Loop in Bison - rici

1 Answers

1
votes

It appears that you are trying to build an interpreter. What you would need is 1) to implement a symbol table and 2) define interpreter nodes derived from a common base class. Each node would have, e.g., an "execute" method.

You'd use your Bison/Yacc parser to build the node tree. Then "execute" the root of the tree.