The input code: -- turn on tokens and semantic stack -- examine the existing "writeln(x);" and the new -- phase 2 writlne with strings and expressions %%t %%ss x:=2; writeln(x); writeln(" ""hi"" there"," x = ",x); -------------------------------------- Running SIMPLE with command line -i (AST) -s (SymbolTable dump) -v (var addrs) in simple.c grammar: stmtlist -> lambda Token : Id grammar: identifier->Id After : PushId Semstack is: IdEntry (x) <+ SemTop StLstEntry grammar: memref -> identifier After : IdToAddr Semstack is: AddrEntry <+ SemTop StLstEntry Token : Assignop Token : Iconst grammar: integer_const-> Iconst After : PushInt Semstack is: IconstEntry <+ SemTop AddrEntry StLstEntry grammar: primary -> integer_const After : IconstReduce Semstack is: ExprEntry <+ SemTop AddrEntry StLstEntry grammar: fact -> primary grammar: term -> fact Token : Termsym grammar: expr -> term grammar: assignstmt->memeref Assignop expr After : Assign Semstack is: StmtEntry <+ SemTop StLstEntry grammar: stmt -> assignstmt Termsym grammar: stmtlist -> stmtlist stmt After : MergeStmts Semstack is: StLstEntry <+ SemTop Token : Writeln Token : Lparen Token : Id grammar: identifier->Id After : PushId Semstack is: IdEntry (x) <+ SemTop StLstEntry grammar: memref -> identifier After : IdToAddr Semstack is: AddrEntry <+ SemTop StLstEntry grammar: primary -> memref After : AddrToPrimary Semstack is: ExprEntry <+ SemTop StLstEntry grammar: fact -> primary grammar: term -> fact Token : Rparen grammar: expr -> term grammar: actparam -> expr After : CreateActParam Semstack is: ParmEntry <+ SemTop StLstEntry grammar: actparamlist->actparam grammar: writestmt-> Writeln Lparen actparamlist Rparen After : Call Semstack is: StmtEntry <+ SemTop StLstEntry Token : Termsym grammar: stmt -> writestmt Termsym grammar: stmtlist -> stmtlist stmt After : MergeStmts Semstack is: StLstEntry <+ SemTop Token : Writeln Token : Lparen Token : StrConst grammar: actparam -> StrConst After : PushString Semstack is: StrEntry ( \"hi\" there) <+ SemTop StLstEntry After : CreateActParam Semstack is: ParmEntry <+ SemTop StLstEntry grammar: actparamlist->actparam Token : Commasym Token : StrConst grammar: actparam -> StrConst After : PushString Semstack is: StrEntry ( x = ) <+ SemTop ParmEntry StLstEntry After : CreateActParam Semstack is: ParmEntry <+ SemTop ParmEntry StLstEntry grammar: actparamlist->actparamlist Commasym actparam After : MergeActParam Semstack is: ParmEntry <+ SemTop StLstEntry Token : Commasym Token : Id grammar: identifier->Id After : PushId Semstack is: IdEntry (x) <+ SemTop ParmEntry StLstEntry grammar: memref -> identifier After : IdToAddr Semstack is: AddrEntry <+ SemTop ParmEntry StLstEntry grammar: primary -> memref After : AddrToPrimary Semstack is: ExprEntry <+ SemTop ParmEntry StLstEntry grammar: fact -> primary grammar: term -> fact Token : Rparen grammar: expr -> term grammar: actparam -> expr After : CreateActParam Semstack is: ParmEntry <+ SemTop ParmEntry StLstEntry grammar: actparamlist->actparamlist Commasym actparam After : MergeActParam Semstack is: ParmEntry <+ SemTop StLstEntry grammar: writestmt-> Writeln Lparen actparamlist Rparen After : Call Semstack is: StmtEntry <+ SemTop StLstEntry Token : Termsym grammar: stmt -> writestmt Termsym grammar: stmtlist -> stmtlist stmt After : MergeStmts Semstack is: StLstEntry <+ SemTop grammar: program -> stmtlist After : Done Semstack is: EMPTY Symbol table dump: x Intermediate Representation - the Abstract Syntax Tree Statements Assign x gets Immediate Value 2 Writeln parameters x Writeln parameters \"hi\" these x = x From codegen.c - DisplayRegs() marks used registers Register: 0 1 2 3 4 5 6 7 8 9 - - - - - - - - - - Used = x: Variable dump from GenStorage ID: x Offset: 0 Proc. Level: 0 COMPILATION COMPLETE out simple.c ------------------------------------- The listing file produced by simple -- turn on tokens and semantic stack -- examine the existing "writeln(x);" and the new -- phase 2 writlne with strings and expressions %%t %%ss x:=2; writeln(x); writeln(" ""hi"" there"," x = ",x); ------------------------------------- The assembler file produced by simple # Register Usage: # $s0 for global variables # .text .globl main main: la $t9, CONST la $s0, GVARS # # Start Code # # Generate Assignment Statement li $t0,2 sw $t0,0($s0) # # Generate Writeln statement li $v0, 1 lw $a0,0($s0) syscall la $a0, S0 li $v0, 4 syscall # # Generate Writeln statement la $a0, S2 li $v0, 4 syscall la $a0, S1 li $v0, 4 syscall li $v0, 1 lw $a0,0($s0) syscall la $a0, S0 li $v0, 4 syscall # # Halt execution li $v0 10 syscall # # Finish up by writing out constants .word 0 CONST: #Constant storage area .data S0: .asciiz "\n" .data S1: .asciiz " x = " .data S2: .asciiz " \"hi\" there" # # Reserve space for global variables .word 0 GVARS: # space for Global Variables .data _x: .word 0 # Offset at 0 .data Temp_Wr: .word 0 #just for alignment of write(exprtree) ------------------------------------- execute the code in spim: SPIM Version 6.2 of January 11, 1999 Copyright 1990-1998 by James R. Larus (larus@cs.wisc.edu). All Rights Reserved. See the file README for a full copyright notice. Loaded: /opt/spim/bin/trap.handler 2 "hi" there x = 2