Beruflich Dokumente
Kultur Dokumente
c
Chuen-Liang Chen
– generating code
(intermediate representation or final output of compiler)
id := <exp> id +
<prim> + <prim> * id
const id id
when to call?
do what? c
semantic stack
3: EXPRREC +: OPREC
LITERALEXPR PLUS
00000011
#include <assert.h>
ID #i($$)
1 2,3 4 5,6 7,8,9
EXPRREC EXPRREC OPREC EXPRREC EXPRREC
IDEXPR LITERALEXPR PLUS IDEXPR TEMPEXPR
Y 00000011 X T1
c
action-controlled semantic stack
open the interface of stack to all semantic action routines
disadvantages:
1. difficult to change
2. action routines have to manipulate the stack
QUIZ: detailed implementation
for LL parser
example (continued)
<program> top_index
$ $
#finish <program> right_index, current_index
EOP(-1,-1,0,1) <system goal> left_index
Chuen-Liang Chen, NTUCS&IE / 23
LL parser-controlled semantic stack (3/11)
(3/11)
action: pridict <program> #start begin <statement list> end
parse stack semantic stack
#start
“begin” top_index
<stmt list> "end"
"end" <stmt list>
EOP(0,1,1,3) "begin" right_index, current_index
$ $
#finish <program> left_index
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 24
LL parser-controlled semantic stack (4/11)
(4/11)
action: do #start; match begin
parse stack semantic stack
top_index
<stmt list> "end"
"end" <stmt list> current_index
EOP(0,1,1,3) "begin" right_index
$ $
#finish <program> left_index
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 25
LL parser-controlled semantic stack (5/11)
(5/11)
action: predict <statement list> <statement> <statement tail>
parse stack semantic stack
c
top_index
<statement> <stmt tail>
<stmt tail> <statement> right_index, current_index
EOP(1,3,4,6) "end"
"end" <stmt list> left_index
EOP(0,1,1,3) "begin"
$ $
#finish <program>
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 26
LL parser-controlled semantic stack (6/11)
(6/11)
action: predict <statement> <ident> := <expression>; #assign($1,$3)
parse stack semantic stack
<ident> top_index
":=" ";"
<expression> <expression>
";" ":=" c
#assign($1,$3) <ident> right_index, current_index
EOP(4,6,6,8) <stmt tail>
<stmt tail> <statement> left_index
EOP(1,3,4,6) "end"
"end" <stmt list>
EOP(0,1,1,3) "begin"
$ $
#finish <program>
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 27
LL parser-controlled semantic stack (7/11)
(7/11)
action: predict <ident> ID #process_id($$)
parse stack semantic stack
ID
#proc_id($$) top_index
EOP(6,8,8,12) ID right_index, current_index
":=" ";"
<expression> <expression>
";" ":=" c
#assign($1,$3) <ident> left_index
EOP(4,6,6,8) <stmt tail>
<stmt tail> <statement>
EOP(1,3,4,6) "end"
"end" <stmt list>
EOP(0,1,1,3) "begin"
$ $
#finish <program>
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 28
LL parser-controlled semantic stack (8/11)
(8/11)
action: match ID
parse stack semantic stack
top_index
":=" ";"
<expression> <expression>
";" ":=" c current_index
#assign($1,$3) <ident> right_index
EOP(4,6,6,8) <stmt tail>
<stmt tail> <statement> left_index
EOP(1,3,4,6) "end"
"end" <stmt list>
EOP(0,1,1,3) "begin"
$ $
#finish <program>
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 30
LL parser-controlled semantic stack (10/11)
(10/11)
action: match :=
parse stack semantic stack
top_index
";"
<expression> <expression> current_index
";" ":=" c
#assign($1,$3) <ident> right_index
EOP(4,6,6,8) <stmt tail>
<stmt tail> <statement> left_index
EOP(1,3,4,6) "end"
"end" <stmt list>
EOP(0,1,1,3) "begin"
$ $
#finish <program>
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 31
LL parser-controlled semantic stack (11/11)
(11/11)
action: predict <expression> <primary> #copy($1,$2) <primary tail> #copy($2,$$)
parse stack semantic stack
<primary> top_index
#copy($1,$2) <primary tail>
<primary tail> <primary> right_index, current_index
#copy($2,$$) ";"
EOP(6,8,10,12) <expression> left_index
";" ":=" c
#assign($1,$3) <ident>
EOP(4,6,6,8) <stmt tail> QUIZ: space complexity
<stmt tail> <statement> QUIZ: how to improve?
EOP(1,3,4,6) "end" QUIZ: comparison,
"end" <stmt list> LL v.s. LR
EOP(0,1,1,3) "begin" action v.s. parser-
$ $ controlled
#finish <program>
EOP(-1,-1,0,1) <system goal>
Chuen-Liang Chen, NTUCS&IE / 32
Intermediate code
ADDI, ADDF, SUBI, SUBF, MULTI, MULTF, DIVI, DIVF, MOD, REM,
EXPI, EXPF, AND, OR, XOR, EQ, NE, GT, GE, LT, LE
RESULT := ARG1 OP ARG2
UMINUS ARG2 := -ARG1
NOT ARG2 := not ARG1
ASSIGN ARG3 := ARG1, size is ARG2
FLOAT ARG2 := FLOAT(ARG1 c ) [ARG 1 in an integer]
ADDRESS ARG2 := the address of ARG1
RANGETEST abort execution if ARG3 < ARG1 or ARG3 > ARG2
LABEL ARG1 is used to label the next tuple
JUMP jump to tuple labeled ARG1
JUMP0 jump to ARG2 if ARG1 = 0
JUMP1 jump to ARG2 if ARG1 = 1
CASEJUMP ARG1 is case selector expression
CASELABEL ARG1 is a case statement label
CASERANGE ARG1 is lower bound of label range,
ARG2 is upper bound of range
example
begin (READI, A)
read(A,B); (READI, B)
if A > B then (GT, A, B, t1)
C := A + 5; (JUMP0, t1, L1)
else c (ADDI, A, 5, C)
C := B + 5; (JUMP, L2)
end if; (LABEL, L1)
write(2 * (C -1)); (ADDI, B, 5, C)
end (LABEL, L2)
(SUBI, C, 1, t2)
(MULTI, 2, t2, t3)
(WRITEI, t3)