ITEC 380 - Programming Assignment 4

Due Date: 11:59:59 Monday 12/06/2010

File name: translate

(Notice that there is no extension on the file)

Submit command : submit itec380-01 translate


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.

Sample Program

-- 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

Stack Machine Language

Stack machine commands are given below. Make sure that you use EXACTLY these commands with no deviation so that your code can be executed by the stack machine interpreter. An interpreter for the stack machine is available here. (Thanks Lee for this program.)

Metalanguage

In the language definition below, nonterminals are shown in italics and terminals are shown in a 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.

Lexical Structure

Grammar

    Program -> StatementList

    StatementList -> Statement {Statement}

    Statement -> SimpleStmt | IfStmt | WhileStmt

    SimpleStmt -> Variable := Expr ;

    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

Semantics

Scanner

You can find a scanner module and a sample client program here. To use the module, simply put it in the directory where your program is and include the line import scanner in your program.
The scanner has functions initialize(filename), currentLexeme(), and advanceToken(). , and hasMoreTokens. The functions currentLexeme() and currentToken() return strings. The list of tokens returned can be found in the python program.
  • Warning: Since this uses the python scanner, you may have to have good indentation in your input program.
  • Other points:

    1. You can assume that your input program is valid.

    2. It is not valid to use a Variable that is identical to a ReservedWord.

    3. There is no (practical) limit to the number of Identifiers or Statements.

    4. There is no unary minus (ie x := -3 is not valid; use x := 0-3 instead).

    5. It is not valid for the input file to not exist

    6. It is not valid for the program to be empty or the body of loop or the then or else part of an if statement to be empty.

    7. You will have to factor IfStmt into IfHead (which has if condition then statment) and ifRest (which has else statement end if;). See the example in the notes.

    8. You should generate label numbers as needed as you execute your program.

    9. Make sure you follow good style.

    10. Call your program translate. The file should have no extension.

    11. Submit all files needed for execution. As usual, your program should be submitted from rucs2 or one of its clients.

    12. An approximate grading plan is show in this sequence.

    Last modified on