Docsity

Control Flow and Boolean Operators in CSCE 531 Compiler Construction, Assignments of Computer Science

Partial preview of the text

Download Control Flow and Boolean Operators in CSCE 531 Compiler Construction and more Assignments Computer Science in PDF only on Docsity!

Lecture 15

Control Flow

Lecture 15

Control Flow

Topics Topics

„

Review Positional Encoding of Booleans

„

Short Circuit Evaluation

„

Control Flow Statements

Readings: 8.4, 8. Readings: 8.4, 8.

March 13, 2006

CSCE 531 Compiler Construction

  • 2 –

Overview

Overview

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:

  • 4 –

Recall Numeric Rep. of Booleans

Recall Numeric Rep. of Booleans

E

E

Æ

Æ

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 ‘

  • 5 –

AND, OR in numeric implementation

AND, OR in numeric implementation

E

E

Æ

Æ

E

E

11

AND E

AND E

2 2

E.place E.place =

= newtemp

newtemp();

emit(E.place emit(E.place ‘

’ E

E

1 1

.place .place ‘

‘and

and’

’ E

E

2 2

.place); .place);

E

E

Æ

Æ

E

E

11

OR E

OR E

2 2

E.place E.place =

= newtemp

newtemp();

emit(E.place emit(E.place ‘

’ E

E

1 1

.place .place ‘

‘or

or’

’ E

E

2 2

.place); .place);

  • 7 –

Positional Implmentation of Booleans

Positional Implmentation of Booleans

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)

  • 8 –

Backpatch

Backpatch

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;

  • 10 –

CSCE 531 Spring 2006

Semantic Actions for B

Æ

ID RELOP ID

Semantic Actions for B

Æ

ID RELOP ID

B:

ID RELOP ID

B:

ID RELOP ID

gen($2, $1, $3, NULL, VOID); gen($2, $1, $3, NULL, VOID);

gen(GOTO gen(GOTO, NULL,NULL,NULL,VOID);

, NULL,NULL,NULL,VOID);

$$.true = $$.true = makelist(nextquad

makelist(nextquad -

$$.false = $$.false = makelist(nextquad

makelist(nextquad -

  • 11 –

CSCE 531 Spring 2006

Markers

Markers

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

M

Æ

Æ

ε ε

{ M.quad

M.quad =

= nextquad

nextquad; }

So instead of So instead of

z z

S

S

Æ

Æ

if B then S else S if B then S else S

We use We use

z z

S

S

Æ

Æ

if B then M if B then M

1 1

S else M S else M

2 2

S

S

***Almost ***Almost

  • 13 –

Semantic Actions for B

Æ

B OR M B

Semantic Actions for B

Æ

B OR M B

B

B

Æ

Æ

B OR M B

B OR M B

backpatch($1.false,$3);

backpatch($1.false,$3);

$$.false = $4.false;

$$.false = $4.false;

$$.true = merge($1.true, $4.true);

$$.true = merge($1.true, $4.true);

  • 14 –
  • 16 –

Semantic Actions for Assignments

Semantic Actions for Assignments

S: ID ASSIGNOP

S: ID ASSIGNOP expr

expr

gen(ASSIGNOP gen(ASSIGNOP,

$3, NULL, $1, VOID);

, NULL, $1, VOID);

$$ = NULL;

$$ = NULL;

  • 17 –

Debugging Parsers written with Yacc

Debugging Parsers written with Yacc

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

  • 19 –

CSCE 531 Spring 2006

GDB - Essential Commands

GDB - Essential Commands

gdb gdb program [core]

program [core] -

  • debug program

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

  • 20 –

Example using gdb

Example using gdb

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)

ÍÍ

!!! !!!