Download presentation
Presentation is loading. Please wait.
Published byΤισιφόνη Κοντολέων Modified over 6 years ago
1
Lecture 16 Boolean Expressions and Control Flow
CSCE 531 Compiler Construction Lecture 16 Boolean Expressions and Control Flow Topics Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation Debugging No class Thursday, we will reschedule! Readings: 5.7, 6.3, 7.4 March 27, 2018
2
Overview Last Time Last Time Didn’t Finish Today’s Lecture
Attribute Grammars Type Systems ILOC Symbol Table Last Time Didn’t Finish Pascal like declarations Hierarchical Symbol Tables Today’s Lecture Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation Debugging YACC generated parsers References: Sections 7.4 Homework:
3
Pascal like declarations
What if the language gets it wrong? Pascal x,y,z : integer; Grammar?
4
Hierarchical Symbol Tables
From last time figure 5.12 page 246
5
Boolean Expression Grammar
BoolExpr not OrTerm | OrTerm OrTerm OrTerm OR AndTerm | AndTerm AndTerm AndTerm AND Bool | Bool Bool RelExpr | true | false RelExpr Expr RelOp Expr Expr Expr + Expr | Expr * Expr | ( Expr ) | ID | NUM
6
Numeric Encoding True = non-zero False = zero Example B OR C AND NOT D
Quadruples NOT rD _ rt1 AND rC rt1 rt2 OR rB rt2 rt3
7
Numeric Encod. Extended to Comparisons
Comparison operations in Hardware IA32 - CC register set as result of arithmetic operations Add, Subtract, … CMP = subtract without saving result Jumps then test certain bits in the CC register JLT, JLE, JEQ, JGE, JGT, JNEQ So encoding includes a lot of Jumps (x < y) AND (y < z) OR (y=x) L0 cmp x y _ _ JLT L2 L1 LoadI 0 T1 _ JMP L3 L2 LoadI 1 _ T1 L3 NOP
8
Example: (x < y) AND (y < z)
Label Op A1 A2 Target CMP X Y _ JLT L2 LOADI T1 JMP L3 1 Z L4 T2 L5 AND T3
9
Positional Encoding In Positional Encoding we represent the value of an expression by where (the position) you end up in the code Example while(k < 20) { sum = sum + k*k; k = k + 1; } Note in the code on the right the value of the boolean expr k<20 is never explicitly represented other than in the Condition Code Register (CC) The value is represented by whether you end up at quad 7 or quad 3 Quad Op S1 S2 T CMPI k 20 _ JGE ?7 MULT k k T1 ADD sum T1 sum ADDI k 1 k JMP 1 …
10
Attributes for Booleans
Consider the example on the next slide As we generate the code we don’t know what the target branches should be. We need to build lists of quads whose target fields need to be filled in later E.True E.false
11
Example: (x < y) AND (y < z)
QuadNum Op A1 A2 Target 1 CMP X Y _ 2 3 4 5 6 7 8 9 10 11
12
Functions for Boolean Attributes
int nextquad variable – Makelist (quad) Merge(l1, l2) Backpatch(List, target)
13
/class/csce531-001/web/Examples/Booleans
Don’t use this make or lex file as example. Focus on boolean.y
14
%{ #include <ctype.h> #include <stdio.h> #define YYDEBUG 1 #define ADDOP 401 #define MULTOP 402 #define GOTO /* $Header: gram,v /12/07 12:01:01 matthews Exp $ */ extern char *yytext; char *strsave(); char *newtemp(); typedef struct node{ int quadnum; struct node *link; } *LIST, LISTNODE;
15
#define CODESIZE 1000 int opcode[CODESIZE]; char *op1[CODESIZE], *op2[CODESIZE], *target[CODESIZE]; char *VOID = "VOID"; LIST tmplist; int nextquad = 0; %} %union{ char *place; struct { LIST *true; LIST *false; } list; int quad; int type; LIST next; }
16
B: ID RELOP ID { gen($2, $1, $3, VOID); gen(GOTO, VOID, VOID, VOID); $$.true = makelist(nextquad -2); $$.false = makelist(nextquad - 1); } ;
17
| B AND M B { backpatch($1.true,$3); $$.true = $4.true; $$.false = merge($1.false, $4.false); } | B OR M B { backpatch($1.false,$3); $$.false = $4.false; $$.true = merge($1.true, $4.true);
18
S: ID ASSIGNOP expr { gen(ASSIGNOP, $<place>3, VOID, $1); $$ = NULL; } | IF B THEN M S N ELSE M S { backpatch($2.true, $4); backpatch($2.false, $8); tmplist = merge($5, $6); $$ = merge(tmplist, $9); ; M: {$$ = nextquad;} N: {gen(GOTO, VOID, VOID, VOID); $$ = makelist(nextquad - 1);
20
Debugging Parsers written with Yacc
Debug the grammar Rewrite grammar to eliminate reduce/reduce and as many shift/reduce as you can. Tracing parses using –t option to bison or yacc -DYYDEBUG compile option Int yydebug=1; in lex specification (C definitions section %{ ..%} Extern int yydebug; in yacc specification 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
21
Parsing Traces with yydebug=1
Setting up for traces –t option to bison or yacc -DYYDEBUG compile option int yydebug=1; in lex specification C definitions section %{ … %} extern int yydebug; in yacc specification Generate .output file and print out so you can follow along
22
deneb> ./expr Starting parse Entering state 0 Reading a token: 23+34*3; Next token is 262 (INT) Shifting token 262 (INT), Entering state 2 Reducing via rule 6 (line 28), INT -> expr INT=23 state stack now 0 Entering state 4 Reading a token: Next token is 260 (PLUS) Shifting token 260 (PLUS), Entering state 8 Reading a token: Next token is 262 (INT) INT=34 state stack now 0 4 8 Entering state 13 Reading a token: Next token is 259 (TIMES) Shifting token 259 (TIMES), Entering state 7
23
Common Mistakes Segmentation fault - This means you have referenced a memory location that is outside of the memory segment of your program. You have a pointer that has a bad value! First make sure everytime you copy a string value you use strdup. Several people have had errors with strcat(s,t) where they did not allocate space for the string “s”. Use gdb and bt (backtrace) to trace down the pointer with the bad value
24
GDB - Essential Commands
gdb program [core] - debug program [using coredump core] b [file:] function set breakpoint at function [in file] run [arglist] start your program [with arglist] bt backtrace: display program stack p expr display the value of an expression c continue running your program n next line, stepping over function calls s next line, stepping into function calls
25
Example using gdb deneb> make bison -d decaf.y
decaf.y contains 51 shift/reduce conflicts. gcc -c -g decaf.tab.c flex decaf.l gcc -DYYDEBUG -g -c lex.yy.c gcc -c -g tree.c gcc -DYYDEBUG -g decaf.tab.o lex.yy.o tree.o -ly -o decaf deneb> ./decaf < t1 Keyword int Segmentation Fault (core dumped) !!!
26
Note the use of the –g option (CFLAGS=-g in Makefile
Example using gdb deneb> make bison -d decaf.y decaf.y contains 51 shift/reduce conflicts. gcc -c -g decaf.tab.c flex decaf.l gcc -DYYDEBUG -g -c lex.yy.c gcc -c -g tree.c gcc -DYYDEBUG -g decaf.tab.o lex.yy.o tree.o -ly -o decaf deneb> ./decaf < t1 Keyword int Segmentation Fault (core dumped) Note the use of the –g option (CFLAGS=-g in Makefile
27
Getting in and out of GDB
deneb> gdb decaf GNU gdb 4.18 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, … (gdb) run < t1 Starting program: /class/csce /submissions/huang27/review01/decaf < t1 before treeprint demo=Program info=X demo= info= Program received signal SIGSEGV, Segmentation fault. 0xff2b3474 in strlen () from /usr/lib/libc.so.1 (gdb) quit The program is running. Exit anyway? (y or n) y deneb>
28
Backtrace(bt) To see the Activation Stack
(gdb) run < t1 Starting program: /class/csce /submissions/huang27/review01/decaf < t1 before treeprint demo=Program info=X demo= info= Program received signal SIGSEGV, Segmentation fault. 0xff2b3474 in strlen () from /usr/lib/libc.so.1 (gdb) bt #0 0xff2b3474 in strlen () from /usr/lib/libc.so.1 #1 0xff in _doprnt () from /usr/lib/libc.so.1 #2 0xff3072cc in printf () from /usr/lib/libc.so.1 #3 0x199e8 in treeprint (p=0x32718) at tree.c:9 #4 0x19a0c in treeprint (p=0x32738) at tree.c:11 #5 0x19a0c in treeprint (p=0x32748) at tree.c:11 #6 0x11414 in yyparse () at decaf.y:91 #7 0xff3804e0 in main () from /usr/lib/liby.so.1
29
Printing values (gdb) up
#1 0xff in _doprnt () from /usr/lib/libc.so.1 #2 0xff3072cc in printf () from /usr/lib/libc.so.1 #3 0x199e8 in treeprint (p=0x32718) at tree.c:9 printf("demo=%s info=%s\n", p->demo, p->info); (gdb) print *p $1 = {tag = VDECL, child = 0x326e8, next = 0x1, info = 0x1adf8 "", tagname = 0x7 <Address 0x7 out of bounds>, demo = 0x0, value = {i = 1, d = e-314, s = 0x1 <Address 0x1 out of bounds>}} (gdb)
30
Project Two and Three Due Tonight – dropbox project two
Test cases using yourlogin name followed by a numeral as the names, e.g., matthews1, matthews2, matthews3 Your grammar file without necessarily having any semantic actions Your Lex, Makefile (not necessarily any routines for semantic actions) We will create a TestDir with all of these files that you can get to. Note I will not vouch for the validity of any test program. Project Three (the old project two) Due Sunday Night Note I will be at an ABET meeting the rest of the week! I will try to read my , but no promises! No class Thursday, we will reschedule!
31
Test 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.