Assignment 5 solutions
Part of
the homework for 22C:50, Summer 2004
|
12) Modify the parser shown in Figure 5.9 so it generates output in this assembly language.
procedure expression;
var more: boolean;
op: char; {--added--}
begin
op = ' '; {initially, no operator } {--added--}
repeat
if lex = '(' then begin
lex_scan;
expression;
if lex = ')' then lex_scan else error;
end else begin
value;
end;
case operator of {--added--}
' ': {no operation!}; {--added--}
'+': writeln(' ADD'); {--added--}
'-': writeln(' SUB'); {--added--}
'*': writeln(' MUL'); {--added--}
'/': writeln(' DIV'); {--added--}
end; {--added--}
more := lex in ['+','-','*','/'];
op = lex; {--added--}
if more then lex_scan;
until not more;
end {expression};
assign(i,times(plus(i,1),times(j,2)));
#define assign(var,val) var = (val) #define times(v1,v2) ((v1) * (v2)) #define add(v1,v2) ((v1) + (v2))
MOVE src,dst
The result should be identical to having written this:
W src W dstWrite a macro, using the macro notation presented in Chapter 6, that will produce this result.
MACRO MOV,src,dst W src W dst ENDMAC