
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Topics Topics
Review Positional Encoding of Booleans
Short Circuit Evaluation
Control Flow Statements
Readings: 8.4, 8. Readings: 8.4, 8.
March 13, 2006
Last Time Last Time
Evaluations of Expressions
Numeric Implementation of Booleans
Positional Encoding of Booleans
Short Circuit Evaluation
If-then-else semantic actions almost
Today Today’
’s Lecture
s Lecture
Review
z
Booleans, quadruples
Control Flow using numeric implementation of Booleans
Control Flow using
References: Sections 8.4, 8. References: Sections 8.4, 8.
Homework: Homework:
id id
1 1
relop relop id
id
2 2
{ E.place
E.place =
= newtemp
newtemp();
emit(if emit(if id
id
1 1
.place .place relop.op
relop.op id
id
22
.place .place goto
goto nextquad+3)
nextquad+3)
emit(E.place emit(E.place ‘
emit(goto emit(goto nextquad
nextquad +2);
emit(E.place emit(E.place ‘
11
2 2
E.place E.place =
= newtemp
newtemp();
emit(E.place emit(E.place ‘
1 1
.place .place ‘
‘and
and’
2 2
.place); .place);
11
2 2
E.place E.place =
= newtemp
newtemp();
emit(E.place emit(E.place ‘
1 1
.place .place ‘
‘or
or’
2 2
.place); .place);
z z
Attributes Attributes
E.true E.true -
E.false E.false
z z
Functions Functions
makelist makelist(
( quadnum
quadnum )
merge( list1, list2 ) merge( list1, list2 ) –
backpatch backpatch (list,
(list, quadnum
quadnum)
void void
backpatch(QuadList backpatch(QuadList p,
p, int
int q)
q)
while (p != NULL){ while (p != NULL){
target[p target[p-
quadnum
quadnum] = (
] = (QuadListNode
QuadListNode *) q;
*) q;
p = p p = p -
-> link;
link;
CSCE 531 Spring 2006
gen($2, $1, $3, NULL, VOID); gen($2, $1, $3, NULL, VOID);
gen(GOTO gen(GOTO, NULL,NULL,NULL,VOID);
$$.true = $$.true = makelist(nextquad
makelist(nextquad -
$$.false = $$.false = makelist(nextquad
makelist(nextquad -
CSCE 531 Spring 2006
Markers are typically Markers are typically nonterminals
nonterminals that derive
that derive
ε ε
that are that are
inserted to insure an action is performed at a given inserted to insure an action is performed at a given
time. time.
A common thing need is to remember the quad number A common thing need is to remember the quad number
where something starts, so the attribute of a marker where something starts, so the attribute of a marker
is just the next quad number. is just the next quad number.
ε ε
{ M.quad
M.quad =
= nextquad
nextquad; }
So instead of So instead of
z z
if B then S else S if B then S else S
We use We use
z z
if B then M if B then M
1 1
S else M S else M
2 2
***Almost ***Almost
S: ID ASSIGNOP expr
expr
gen(ASSIGNOP gen(ASSIGNOP,
Debug the grammar Debug the grammar
Rewrite grammar to eliminate reduce/reduce and as manyshift/reduce as you can.
Tracing parses using
z
–t option to bison or yacc
z
-DYYDEBUG compile option
z
int yydebug=1; in bison specification (C definitions section %{ ..%}
z
extern int yydebug; in lex specification
Debug the semantic actions Debug the semantic actions
Compile with –g option; set CFLAGS=-g in Makefile and use
gcc $(CFLAGS) … as the compile (or rely on the builtin rules)
Use gdb (Gnu debugger) to debug the program
CSCE 531 Spring 2006
gdb gdb program [core]
program [core] -
debug program
[using [using coredump
coredump core]
core]
b [file:] function b [file:] function
set breakpoint at function [in file] set breakpoint at function [in file]
run [ run [arglist
arglist]
start your program [with start your program [with arglist
arglist]
bt bt backtrace
backtrace:
display program stack display program stack
p p expr
expr
display the value of an expression display the value of an expression
c c
continue running your program continue running your program
n n
next line, stepping over function calls next line, stepping over function calls
s s
next line, stepping into function calls next line, stepping into function calls
deneb deneb> make
make
bison bison -
-d
d decaf.y
decaf.y
decaf.y decaf.y contains 51 shift/reduce conflicts.
contains 51 shift/reduce conflicts.
gcc gcc -
-c
c -
-g
g decaf.tab.c
decaf.tab.c
flex flex decaf.l
decaf.l
gcc gcc -
-DYYDEBUG
DYYDEBUG -
-g
g -
-c
c lex.yy.c
lex.yy.c
gcc gcc -
-c
c -
-g
g tree.c
tree.c
gcc gcc -
-DYYDEBUG
DYYDEBUG -
-g
g decaf.tab.o
decaf.tab.o lex.yy.o
lex.yy.o tree.o
tree.o -
-ly
ly -
-o decaf
o decaf
deneb deneb> ./decaf < t
./decaf < t
Keyword Keyword
int int
Segmentation Fault (core dumped) Segmentation Fault (core dumped)
ÍÍ
!!! !!!