program ----> { stmt }
stmt ----> "{" { stmt } "}" | ifstmt | whilestmt |
assign ";" | readstmt ";" | writestmt ";" | writecstmt ";"
ifstmt ----> "IF" "(" expr ")" stmt [ "ELSE" stmt ]
whilestmt ----> "WHILE" "(" expr ")" stmt
assign ----> id "=" expr
readstmt ----> "READ" "(" id ")"
writestmt ----> "WRITE" "(" expr ")"
writecstmt ----> "WRITEC" "(" intconst ")"
expr ----> mulexpr { ( "+"|"-" ) mulexpr }
mulexpr ----> unaryexpr { ( "*"|"/"|"%" ) unaryexpr }
unaryexpr ----> const | id | "(" expr ")"
const ----> intconst
(Supplied by scanner: Everything in "", id, intconst)
(Metasymbols: | means "or", { xx } means "zero or more occurrences of xx,
[ xx ] means "optional item xx", that is, zero or one occurrences of xx,
( x | y ) means "either x or y", where () is used for grouping.
----> has precedence over | which has precedence over everything else.)
Implementation Notes:
I implemented these additions to the language in two stages.
As described in class, if you have source like the following:
then output the following quadruples (here nextQuad is the next quadruple to be generated):
Store value of nextQuad in local variable whilestart
Quads to evaluate expr, with result in temporary T1
Store value of nextQuad in local variable jeqquad
(JEQ, T1, , __ ) (Location __ must be backpatched.)
Quads for stmt
(JMP, , , whilestart)
Backpatch quad number jeqquad to put value nextQuad into 4th position
You could use the following simple source to check the new WHILE loop:
source.
Again as described in class, if you have source like the following:
then output the following quadruples:
Quads to evaluate expr, with result in temporary T1
Store value of nextQuad in local variable (for backpatching)
(JEQ, T1, , __ ) (Location ___ must be backpatched.)
Quads to evaluate stmt1
If there is no ELSE, then backpatch value of nextQuad into earlier JEQ.
If there is an ELSE, then
Store value of nextQuad in local variable (for backpatching)
(JMP, , , __ ) Location __ must be backpatched
Backpatch value of nextQuad into ealier JEQ
Quads for stmt2
Backpatch value of nextQuad into earlier JMP.
I used following simple source to check the new IF-ELSE loop:
Source.
Source to use Newton's method to calculate the square root of 2..
Here is simple source to try out the Project IV work:
Source to generate Fibonacci numbers and identify them as even (E) or odd(O).
Here is very complicated source for the Project IV work:
Source to generate Fibonacci numbers, to factor them, and print the results.