[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Parsing if, while, for, etc
From: |
James Buchanan |
Subject: |
Parsing if, while, for, etc |
Date: |
Sat, 2 Dec 2000 22:09:55 +1100 |
Hello Bison users,
I have a book called Lex & Yacc by O'Rielly, however its coverage
of parsing conditionals and loops is pitiful to say the least. It is
excellent in all other areas, but it's a total failure in conditionals and
loops.
I am wondering what other people do when needing to
parse loops and conditionals. Let's suppose we are generating
pseudo assembler as a kind of intermediate representation, and
come across something like this in a source file:
while x = True and y = False and z > 10 do
... statements ...
end while
... statements that come after while loop ...
Would I generate a label, say endwhileX (where X is the x-th while
loop seen in the source code) and perhaps build a linked list of all
the while conditions, say while_conditions_X->cond,
while_conditions_X->next, etc and walk through them one by one
testing them for truth, and if so go through the while loop code, but
if none of the conditions are true, would I generate say a jump to
the endwhileX label? Say (since they are all supposed to be true to
continue in the while loop code above):
move register1, cond1
jumpzero endwhileX
move register1, cond2
jumpzero endwhileX
move register1, cond3
jumpzero endwhileX
alltrueX:
.... code to execute in while loop ....
.... fall through to endwhileX when done ....
endwhileX:
.... code to execute after while loop ....
Can someone tell me, if they have ever written a grammar involving
conditionals and loops, what you do when you need to generate
code for them or what you do when you execute statements
straight away as in an interpreted script?
Thanks for any pointers/tips/examples you can possibly give me!
Regards,
James Buchanan
address@hidden
- Parsing if, while, for, etc,
James Buchanan <=
- Re: Parsing if, while, for, etc, Akim Demaille, 2000/12/07
- Re: Parsing if, while, for, etc, Hans Aberg, 2000/12/07