Вы находитесь на странице: 1из 2

You are going to write a translation schema to emit intermediate codes of the

programs of the following toy programming language. The grammar of this toy
programming language is:

program decls compoundstmt


decls decl ; decls |
decl int ID | real ID
stmt ifstmt | whilestmt | assgstmt | compoundstmt
compoundstmt { stmts }
stmts stmt stmts |
ifstmt if ( boolexpr ) then stmt else stmt
whilestmt while ( boolexpr ) stmt
assgstmt ID = arithexpr ;
boolexp arithexpr boolop arithexpr
boolop < | > | <= | >= | ==
arithexpr multexpr arithexprprime
arithexprprime + multexpr arithexprprime | - multexpr arithexprprime |
multexpr simpleexpr multexprprime
multexprprime * simpleexpr multexprprime | / simpleexpr multexprprime |
simpleexpr ID | INTUM | REALNUM | ( arithexpr )

In this grammar, program is the start symbol. You may assume that each token is
separated with at least one white space character (for simple reading). ID,
INTNUM and REALNUM are token types. ID is an identifier (an identifier is a
lowercase letter such as a b c ... ), INTNUM is a positive integer number (starts
with a digit and continues with digits). REALNUM is a positive real number
(REALNUM is INTNUM.INTNUM) .

Your translation schema should read a program and print the intermediate codes
corresponding to that program. Your intermediate codes should have location
information. If the program is incorrect, it should print an error message and quit
(there is no need for error handling in this assignment).

For example, if your translation schema reads the following program:

int a ; int b ; real c ;


{
a = 3 ; b = 2 ; c = 3.0 ;
a = a * a + 1 ;
b = b * a ;
if ( a < b ) then c = c / 2 ; else c = c / 4 ;
while ( a > b ) b = b + 1;
}
Your translation schema should give an output similar to the following output:
1: MOVI #3,,a
2: MOVI #2,,b
3: MOVR #3.0,,c
4: MULTI a,a,T1
5: ADDI T1,#1,T2
6: MOVI T2,,a
7: MULTI b,a,T3
8: MOVI T3,,b
9: LTI a,b,T4
10: JMPF ,,15
11: CONVR #2,,T5
12: DIVR c,T5,T6
13: MOVR T6,,c
14: JMP 18
15: CONVR #4,,T7
16: DIVR c,T7,T8
17: MOVR T8,,c
18: GTI a,b,T9
19: JMPF ,,23
20: ADDI b,#1,T10
21: MOVI T10,,b
22: JMP 18
23:

Test your translation schema with correct and incorrect programs.

Вам также может понравиться