Lecture 16 Boolean Expressions and Control Flow

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Intermediate Code Generation
Programming Languages and Paradigms The C Programming Language.
Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
1 Compiler Construction Intermediate Code Generation.
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
Lecture 10 YACC – Yet Another Compiler Compiler Introduction to YACC and Bison Topics Yacc/Bison IntroductionReadings: February 13, 2006 CSCE 531 Compiler.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
CSE 303 Lecture 13a Debugging C programs
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Lecture 14 Boolean Expressions Topics Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation If Then else (incorrect.
C Slides and captured lecture (video and sound) are available at:
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
LEX and YACC work as a team
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Introduction to YACC Panfeng Xue
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Code Generation CPSC 388 Ellen Walker Hiram College.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
Three Address Code Generation of Control Statements continued..
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
CSCE 531 Compiler Construction
DEBUG.
Language processing: introduction to compiler construction
Lecture 9 Symbol Table and Attributed Grammars
Tutorial On Lex & Yacc.
CSE 374 Programming Concepts & Tools
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages and Paradigms
CSCE 212Honors Computer Organization
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Context-free Languages
Programming Paradigms
gdb gdb is the GNU debugger on our CS machines.
Bison: Parser Generator
CMPE 152: Compiler Design December 5 Class Meeting
Programmazione I a.a. 2017/2018.
Intermediate Code Generation
Lecture 16 Boolean Expressions and Control Flow
Lecture 9 SLR Parse Table Construction
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Chapter 6 Intermediate-Code Generation
CSCE 531 Compiler Construction
Lecture 14 Semantic Actions II
When your program crashes
Lecture 14 Semantic Actions II
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Lecture 16 Boolean Expressions and Control Flow
Compiler Structures 7. Yacc Objectives , Semester 2,
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
Programming Languages and Paradigms
Compiler Construction
CSCE 212Honors Computer Organization
Lecture 16 Boolean Expressions and Control Flow
CSCE 531 Compiler Construction
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Lecture 16 Boolean Expressions and Control Flow
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

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

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:

Pascal like declarations What if the language gets it wrong? Pascal x,y,z : integer; Grammar?

Hierarchical Symbol Tables From last time figure 5.12 page 246

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

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

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

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

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 …

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

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

Functions for Boolean Attributes int nextquad variable – Makelist (quad) Merge(l1, l2) Backpatch(List, target)

/class/csce531-001/web/Examples/Booleans Don’t use this make or lex file as example. Focus on boolean.y

%{ #include <ctype.h> #include <stdio.h> #define YYDEBUG 1 #define ADDOP 401 #define MULTOP 402 #define GOTO 407 /* $Header: gram,v 1.1 84/12/07 12:01:01 matthews Exp $ */ extern char *yytext; char *strsave(); char *newtemp(); typedef struct node{ int quadnum; struct node *link; } *LIST, LISTNODE;

#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; }

B: ID RELOP ID { gen($2, $1, $3, VOID); gen(GOTO, VOID, VOID, VOID); $$.true = makelist(nextquad -2); $$.false = makelist(nextquad - 1); } ;

| 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);

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);

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

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

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

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

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

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 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

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/csce531-001/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>

Backtrace(bt) To see the Activation Stack (gdb) run < t1 Starting program: /class/csce531-001/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 0xff305798 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

Printing values (gdb) up #1 0xff305798 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 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 = 2.122050236999444e-314, s = 0x1 <Address 0x1 out of bounds>}} (gdb)

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 email, but no promises! No class Thursday, we will reschedule!

Test 2