Your assignment is to write a Python program that uses a recursive descent parser to translate the language defined below into code for the stack machine defined below. When submitted, your translator should run as a script (ie contain the required #! statement as the first line). Your translator is to read from the file which specified as the command line argument when your translator is invoked and it is to write to standard output.
-- Input a value n, sum from 1 to the absolute value of n -- Output sum and whether sum is even or odd i:=1; sum := 0; get n; if n < 0 then n := 0 - n; end if; while i <= n loop sum := sum + i; i:=i + 1; end loop; print sum; newLine; if 2 * (sum / 2) = sum then print "Even"; else print "Odd"; end if;If the program above were in a file called
sample1
then a
sample run would be very similar to this (the blank lines are added for readability;
you may choose whether to generate blank lines, but I recommend it):
>translate sample1 push 1 store i push 0 store sum get store n push n push 0 lt branchIfZero AfterThen1 push 0 push n minus store n branchAlways AfterElse1 LABEL AfterThen1 LABEL AfterElse1 LABEL loopTop2 push i push n le branchIfZero LoopEnd2 push sum push i plus store sum push i push 1 plus store i branchAlways loopTop2 LABEL loopEnd2 push sum print newLine push 2 push sum push 2 divide times push sum eq branchIfZero AfterThen3 push "Even" print branchAlways AfterElse3 LABEL AfterThen3 push "Odd" print LABEL AfterElse3
push
operand: Puts the value of operand on the stack.
operand is a variable, an integer literal, or a string literal.
plus, minus, times, divide
: replace top two elements
with the result of the operation.lt, le, gt, ge, eq, ne
:
replace top two integer elements with boolean result
(1 for true, 0 for false)store
operand: store top value into variable
operand and pop stack print
: sends the string or value on top of stack to
standard output and then pops the stack newLine
: sends a newline character to standard outputbranchAlways
location: transfers control to the specified locationbranchIfZero
location: transfers control to the specified location if top of
stack is 0 and pops stackLABEL
location: marks the specified location in the stack
machine programfileName
using the command interp fileName
fixed width
font. The symbols "{" and
"}" denote 0
or more occurrences and the symbols "[" and "]" represent 0 or 1 occurrences.
The symbol "|" represents a choice.
0 1 2 3 4 5 6 7 8 9
a..z
and A..Z
.
The language is case sensitive. ( ) := ;
+ -
* /
< <= > >= = /=
get print newLine if then else end while loop
Program -> StatementList
StatementList -> Statement {Statement}
Statement -> SimpleStmt | IfStmt | WhileStmt
SimpleStmt -> Variable :=
Expr ;
| print
Expr ;
| print
String ;
| get
Variable ;
| newLine
;
Variable -> Identifier
IfStmt -> if
BoolExpr then
StatementList [else
StatementList] end if ;
WhileStmt -> while
BoolExpr loop
StatementList end loop ;
Expr -> Term {AddOp Term}
Term -> Factor {MulOp Factor}
Factor -> Number | Variable | (
Expr
)
BoolExpr -> Expr RelOp Expr
if
and while
statements.
if
and while
statements have the usual
semantics.:=
Expr
assigns the value of Expr to Variable
print
Expr sends to standard output
the value of Expr.print
String sends String to standard output.
newLine
sends a newline character to standard output. import scanner
in your program.
initialize(filename)
,
currentLexeme()
, and
advanceToken()
.
hasMoreTokens
. currentLexeme()
and currentToken()
return strings.
The list of tokens returned can be found in the python program.
if
condition then
statment)
and ifRest (which has else statement end if;
). See the example in the notes.translate
.
The file should have no extension.