CSCE 531 Compiler Construction Lecture 17 Control Flow

  • Slides: 30
Download presentation
CSCE 531 Compiler Construction Lecture 17 Control Flow Topics n Debugging n Review: Positional

CSCE 531 Compiler Construction Lecture 17 Control Flow Topics n Debugging n Review: Positional Encoding of Booleans No class Thursday, we will reschedule! n Readings: 5. 7, 6. 3, 7. 4 March 29, 2018

Overview Last Time n n n Pascal like declarations Hierarchical Symbol Tables Numeric Implementation

Overview Last Time n n n Pascal like declarations Hierarchical Symbol Tables Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation Last Time Didn’t Finish n Debugging YACC generated parsers Today’s Lecture n n n – 2– Review Semantic Attributes and actions for Boolean expressions Markers Attribute for Control Flow If statement If else (parsing first; should have done this earlier) If else semantic actions CSCE 531 Summer 2004

/class/csce 531 cocsce-l 1 d 39 -11> tree -L 1 csce 531 ├── Postfix

/class/csce 531 cocsce-l 1 d 39 -11> tree -L 1 csce 531 ├── Postfix ├── Simple. Yacc ├── Table ├── Test. Lex. Analyzer ├── Test. Programs └── Tree – 3– CSCE 531 Summer 2004

Postfix/mmmerror. c extern int yylineno; void mywhere(){ fprintf(stderr, "line %d", yylineno); } // FILE

Postfix/mmmerror. c extern int yylineno; void mywhere(){ fprintf(stderr, "line %d", yylineno); } // FILE *yyerfp = stdout; void yyerror(char *s, char *t) { extern int yynerrs; static int list = 0; if(s || ! list){ fprintf(stderr, "[error %d] ", yynerrs+1); mywhere(); – 4– Compiler Construction under UNIX Schreiner and Friedman CSCE 531 Summer 2004

tree Makefile Tree. output clean: – 5– CSCE 531 Summer 2004

tree Makefile Tree. output clean: – 5– CSCE 531 Summer 2004

Test. Lex. Analyzer cocsce-l 1 d 39 -11> tree Test. Lex. Analyzer/ ├── core

Test. Lex. Analyzer cocsce-l 1 d 39 -11> tree Test. Lex. Analyzer/ ├── core 2. tab. h ├── lex. yy. c ├── lex. yy. o ├── Makefile ├── regexp. py ├── struct. h ├── test 8 ├── test. Lex. c ├── to. Name. c –└── 6– to. Name. o CSCE 531 Summer 2004

. /test. Lex < test 8 cocsce-l 1 d 39 -11>. /test. Lex <

. /test. Lex < test 8 cocsce-l 1 d 39 -11>. /test. Lex < test 8 Token returned = 267 lexeme=program Token. Defined. Constant=PROGRAM Token returned = 274 lexeme=int Token. Defined. Constant=UNRECOGNIZED TOKEN CODE! Token returned = 278 lexeme=a Token returned = 44 lexeme=, Token returned = 278 lexeme=b Token returned = 44 lexeme=, Token returned = 278 lexeme=c Token returned = 44 lexeme=, Token returned = 278 lexeme=d Token returned = 59 lexeme=; Token returned = 268 lexeme=begin Token returned = 260 lexeme=if –Token 7– Token. Defined. Constant=ID Token. Defined. Constant=, Token. Defined. Constant=ID Token. Defined. Constant=; Token. Defined. Constant=BEGN Token. Defined. Constant=IF CSCE 531 Summer 2004

test. Lex. c #include<stdio. h> int yylex(); #include "struct. h" #include "core 2. tab.

test. Lex. c #include<stdio. h> int yylex(); #include "struct. h" #include "core 2. tab. h" YYSTYPE yylval; extern char *yytext; char *to. Token. Name(); void main(){ int token. Code = 0; while((token. Code = yylex()) != 0){ printf("Token returned = %dtlexeme=%st Token. Defined. Constant=%sn", token. Code, yytext, to. Token. Name(token. Code)); } – 8– } CSCE 531 Summer 2004

core 2. tab. h … enum yytokentype { INT = 258, RELOP = 259,

core 2. tab. h … enum yytokentype { INT = 258, RELOP = 259, IF = 260, THEN = 261, ELSE = 262, AND = 263, ASSIGNOP = 264, OR = 265, NOT = 266, PROGRAM = 267, BEGN = 268, END = 269, WHILE = 270, DO = 271, – 9– ENDLOOP = 272, CSCE 531 Summer 2004

to. Name. c char * to. Token. Name(int token. Code){ char ascii[2]; ascii[1]=(char)0; //

to. Name. c char * to. Token. Name(int token. Code){ char ascii[2]; ascii[1]=(char)0; // EOS (End Of String) if(token. Code < 128){ ascii[0] = (char) token. Code; return(strdup((char *) ascii)); } switch (token. Code){ case 258: return("INT"); case 259: return("RELOP"); case 260: return("IF"); case 261: return("THEN"); … case 278: return("ID"); default: return("UNRECOGNIZED TOKEN CODE!"); } – 10 – } CSCE 531 Summer 2004

Python 3 regular expressions § https: //docs. python. org/3/library/re. html § import re then

Python 3 regular expressions § https: //docs. python. org/3/library/re. html § import re then fairly standard regular expression patterns § Grouping using parentheses § m = re. search(‘[0 -9]{3}-[0 -9]{2}-([0 -9]{4})’, string) § § – 11 – re. search – search for pattern (regexpr) in string Return match in ‘m’ (a match object) Note parentheses specify grouping last 4 = m. group(1) CSCE 531 Summer 2004

import re filename="core 2. tab. h" … # compile the regular expression to do

import re filename="core 2. tab. h" … # compile the regular expression to do searches etc. dfa = re. compile("ss([A-Z]+)s*=s*([0 -9][0 -9])") # open the file "core 2. tab. h" and process each line by matching the reg expr f = open(filename) for line in f: match = dfa. search(line) if match: print("tt case "+ match. group(2) + ": return("" + match. group(1) + ""); ") – 12 – CSCE 531 Summer 2004

Generate header of the function # create the header part of the function print("char

Generate header of the function # create the header part of the function print("char *") print("to. Token. Name(int token. Code){") print("t char ascii[2]; ") print("t ascii[1]=(char)0; // EOS (End Of String) ") print("t if(token. Code < 128){") print("tt ascii[0] = (char) token. Code; ") print("tt return(strdup((char *) ascii)); ") print("t }") print("t switch(token. Code){") – 13 – CSCE 531 Summer 2004

dfa = re. compile("ss([A-Z]+)s*=s*([0 -9][0 -9])") # open the file "core 2. tab. h"

dfa = re. compile("ss([A-Z]+)s*=s*([0 -9][0 -9])") # open the file "core 2. tab. h" and process each line by matching the re f = open(filename) for line in f: match = dfa. search(line) if match: print("tt case "+ match. group(2) + ": return("" + match. group(1) + ""); ") – 14 – CSCE 531 Summer 2004

bison -- report=all State 8 1 task: task expr '; '. $default reduce using

bison -- report=all State 8 1 task: task expr '; '. $default reduce using rule 1 (task) State 9 4 expr: expr. '+' expr 4 | expr '+' expr. ['+', '; '] 5 | expr. '*' expr '*' shift, and go to state 7 $default reduce using rule 4 (expr) Conflict between rule 4 and token '+' resolved as reduce (%left '+'). Conflict between rule 4 and token '*' resolved as shift ('+' < '*'). – 15 – CSCE 531 Summer 2004

Debugging Parsers written with Yacc 1. Debug the grammar 1. 2. Rewrite grammar to

Debugging Parsers written with Yacc 1. Debug the grammar 1. 2. Rewrite grammar to eliminate reduce/reduce and as many shift/reduce as you can. Tracing parses using l –t option to bison or yacc l -DYYDEBUG compile option l int yydebug=1; in flex specification (C definitions section %{. . %} l extern int yydebug; in yacc specification 2. Debug the semantic actions n n – 16 – 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 Summer 2004

Parsing Traces with yydebug=1 Setting up for traces l –t option to bison or

Parsing Traces with yydebug=1 Setting up for traces l –t option to bison or yacc l -DYYDEBUG compile option l int yydebug=1; in lex specification n C definitions section %{ … %} l extern int yydebug; in yacc specification l Generate. output file and print out so you can follow along – 17 – CSCE 531 Summer 2004

Stack now 0 1 5 6 9 7 Entering state 10 Stack now 0

Stack now 0 1 5 6 9 7 Entering state 10 Stack now 0 1 5 6 Entering state 9 Reading a token: Next token is token '; ' () Reducing stack by rule 4 (line 55): $1 = nterm expr () $2 = token '+' () $3 = nterm expr () -> $$ = nterm expr () Stack now 0 1 Entering state 5 Next token is token '; ' () Shifting token '; ' () Entering state 8 Reducing stack by rule 1 (line 46): $1 = nterm task () $2 = nterm expr () – 18$3 – = token '; ' () . /tree Dumping tree + ID x * ID y ID z CSCE 531 Summer 2004

Common Mistakes l Segmentation fault - This means you have referenced a memory location

Common Mistakes l Segmentation fault - This means you have referenced a memory location that is outside of the memory segment of your program. n n n – 19 – 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 CSCE 531 Summer 2004

GDB - Essential Commands gdb program [core] - debug program [using coredump core] b

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 – 20 – CSCE 531 Summer 2004

Example using gdb deneb> make bison -d decaf. y contains 51 shift/reduce conflicts. gcc

Example using gdb deneb> make bison -d 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 < t 1 Keyword int Segmentation Fault (core dumped) !!! – 21 – CSCE 531 Summer 2004

Example using gdb deneb> make Note the use of the –g option (CFLAGS=-g in

Example using gdb deneb> make Note the use of the –g option (CFLAGS=-g in Makefile bison -d 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 < t 1 Keyword int Segmentation Fault (core dumped) – 22 – CSCE 531 Summer 2004

Getting in and out of GDB deneb> gdb decaf GNU gdb 4. 18 Copyright

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 < t 1 Starting program: /class/csce 531 -001/submissions/huang 27/review 01/decaf < t 1 before treeprint demo=Program info=X demo= info= Program received signal SIGSEGV, Segmentation fault. 0 xff 2 b 3474 in strlen () from /usr/libc. so. 1 (gdb) quit The program is running. Exit anyway? (y or n) y deneb> – 23 – CSCE 531 Summer 2004

Backtrace(bt) To see the Activation Stack (gdb) run < t 1 Starting program: /class/csce

Backtrace(bt) To see the Activation Stack (gdb) run < t 1 Starting program: /class/csce 531 -001/submissions/huang 27/review 01/decaf < t 1 before treeprint demo=Program info=X demo= info= Program received signal SIGSEGV, Segmentation fault. 0 xff 2 b 3474 in strlen () from /usr/libc. so. 1 (gdb) bt #0 #1 #2 #3 #4 #5 #6 #7 0 xff 2 b 3474 in strlen () from /usr/libc. so. 1 0 xff 305798 in _doprnt () from /usr/libc. so. 1 0 xff 3072 cc in printf () from /usr/libc. so. 1 0 x 199 e 8 in treeprint (p=0 x 32718) at tree. c: 9 0 x 19 a 0 c in treeprint (p=0 x 32738) at tree. c: 11 0 x 19 a 0 c in treeprint (p=0 x 32748) at tree. c: 11 0 x 11414 in yyparse () at decaf. y: 91 0 xff 3804 e 0 in main () from /usr/liby. so. 1 – 24 – CSCE 531 Summer 2004

Printing values (gdb) up #1 0 xff 305798 in _doprnt () from /usr/libc. so.

Printing values (gdb) up #1 0 xff 305798 in _doprnt () from /usr/libc. so. 1 (gdb) up #2 0 xff 3072 cc in printf () from /usr/libc. so. 1 (gdb) up #3 0 x 199 e 8 in treeprint (p=0 x 32718) at tree. c: 9 9 printf("demo=%s info=%sn", p->demo, p->info); (gdb) print *p $1 = {tag = VDECL, child = 0 x 326 e 8, next = 0 x 1, info = 0 x 1 adf 8 "", tagname = 0 x 7 <Address 0 x 7 out of bounds>, demo = 0 x 0, value = {i = 1, d = 2. 122050236999444 e-314, s = 0 x 1 <Address 0 x 1 out of bounds>}} (gdb) – 25 – CSCE 531 Summer 2004

Markers – Grammar symbol that only derive ε which are used perform some semantic

Markers – Grammar symbol that only derive ε which are used perform some semantic action and usually to save some attribute value Example S A B M C D n In this case we want to perform some action when the production M ε is reduced, like saving the starting place of the code the evaluates C Markers for handling Booleans using the positional encoding n n – 26 – M ε N ε (M. quad – marks the start of a piece of code) (N. next – a list consisting of a single quad that needs to have its target field filled in later. ) CSCE 531 Summer 2004

Semantic Actions for B B or M B B … | B OR M

Semantic Actions for B B or M B B … | B OR M B { backpatch($1. false, $3); $$. false = $4. false; $$. true = merge($1. true, $4. true); } M: {$$ = nextquad; } /* ε production */ ; – 27 – CSCE 531 Summer 2004

S: ID ASSIGNOP expr { gen(ASSIGNOP, $<place>3, VOID, $1); $$ = NULL; } |

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); $$ = makelist(nextquad - 1); } – 28 – CSCE 531 Summer 2004

Project Two and Three Due Tonight – dropbox project two n n Test cases

Project Two and Three Due Tonight – dropbox project two n n Test cases using yourlogin name followed by a numeral as the names, e. g. , matthews 1, matthews 2, matthews 3 Your grammar file without necessarily having any semantic actions, except for expressions and undefined variables. Your Lex, Makefile (not necessarily any routines for semantic actions) We will create a Test. Dir with all of these files that you can get to. Note I will not vouch for the validity of any test program. Project Three 1. Add in Boolean to grammar: && (AND), || (OR) and !(not) 2. Quad Array 2. List manipulating functions: makelist, merge, and backpatch 3. Add semantic actions for control-flow statements – 4. 29 – Due Sunday April 7! CSCE 531 Summer 2004

Test 2 April 19 th !! (last day I can give it. ) Exam

Test 2 April 19 th !! (last day I can give it. ) Exam May 8 – 30 – CSCE 531 Summer 2004