CSCE 531 Compiler Construction

Slides:



Advertisements
Similar presentations
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Advertisements

Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
1 Compiler Construction Intermediate Code Generation.
Lecture 20 Types / Offsets Revisited Topics Procedural Abstraction Activation records Readings: 8.2 March 29, 2006 CSCE 531 Compiler Construction.
Lecture 19 Nested Scopes Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
Compiler Designs and Constructions
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.
Language processing: introduction to compiler construction Andy D. Pimentel Computer Systems Architecture group
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:
Parser construction tools: YACC
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
Saumya Debray The University of Arizona Tucson, AZ 85721
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.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to YACC Panfeng Xue
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
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.
Introduction to YACC CS 540 George Mason University.
Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
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..
Project Part 2: Parser. Command line: bison –d translate.y Command line: flex filename.l gcc y.tab.c lex.yy.c -lfl Bison.
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.
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
DEBUG.
Language processing: introduction to compiler construction
Lecture 9 Symbol Table and Attributed Grammars
Constructing Precedence Table
CSE 374 Programming Concepts & Tools
Review: Chapter 5: Syntax directed translation
Compiler Construction
CSCE 212Honors Computer Organization
Chapter 4 Syntax Analysis.
CS 153: Concepts of Compiler Design December 5 Class Meeting
Parser and Scanner Generation: An Introduction
CMPE 152: Compiler Design December 5 Class Meeting
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lecture 16 Boolean Expressions and Control Flow
CS 3304 Comparative Languages
Compiler Design 4. Language Grammars
Bison Marcin Zubrowski.
Lecture 9 SLR Parse Table Construction
CSCE 531 Compiler Construction
Subject: Language Processor
Lecture 16 Boolean Expressions and Control Flow
Lecture 16 Boolean Expressions and Control Flow
Compiler Lecture Note, Miscellaneous
Yacc Yacc.
Compiler Structures 7. Yacc Objectives , Semester 2,
Appendix B.2 Yacc Appendix B.2 -- Yacc.
Saumya Debray The University of Arizona Tucson, AZ 85721
Compiler Construction
CSCE 212Honors Computer Organization
Lecture 16 Boolean Expressions and Control Flow
CSCE 531 Compiler Construction
Lecture 16 Boolean Expressions and Control Flow
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

CSCE 531 Compiler Construction Lecture 16 Control Flow Topics Control Flow Statements Introduction to Procedures and functions Readings: 8.4, 8.6 March 15, 2006

Overview Last Time Today’s Lecture References: Sections 8.4, 8.6 Review of Booleans Numeric Implementation of Booleans Positional Encoding of Booleans Short Circuit Evaluation If-then-else semantic actions almost Today’s Lecture Control Flow using numeric implementation of Booleans References: Sections 8.4, 8.6 Homework:

Positional Implmentation of Booleans Attributes E.true - … E.false Functions makelist( quadnum ) – merge( list1, list2 ) – backpatch (list, quadnum)

Intermediate Code Generation void gen(int op, struct nlist *p1, struct nlist *p2, struct nlist *r, int t) { opcode[nextquad] = op; op1[nextquad] = p1; op2[nextquad] = p2; result[nextquad] = r; branchTarget[nextquad] = t; nextquad = nextquad + 1; }

Semantic Actions for S  if B then M S else M S ***Almost S: IF B THEN M S N ELSE M S { backpatch($2.true, $4); backpatch($2.false, $8); tmplist = merge($5, $6); $$ = merge(tmplist, $9); } ; Why almost? N  ε { N.next = nextquad; gen(goto, _, _, _, void); }

Semantic Actions for Assignments S: ID ASSIGNOP expr { gen(ASSIGNOP, $3, NULL, $1, VOID); $$ = NULL; } |

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 bison specification (C definitions section %{ ..%} extern int yydebug; in lex 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

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

Run GDB

Attributes for Booleans and Control Flow* *Assuming positional encoding. B (for boolean expression) B.true – A list of …… that need to … B.false Functions for Boolean Attributes int nextquad variable – Makelist (quad) Merge(l1, l2) Backpatch(List, target)

Semantic Actions Control Flow S  LHS ASSIGNOP E ‘;’ | if-statement | if-else-statement | while-statement | for-statement | BEGIN L END | function-calls ??? /* this is not really what we mean by control flow we will do this later */ ; L L S | S ; Now what attributes do we need for these nonterminals? (S, L)

Semantic Actions Statement Lists L L S | S ; As an example consider while (B-expr){ S1 S2 S3 } Parse Tree S while ‘(‘ B ‘)’ DO S END Now what attributes do we need for these nonterminals? (S, L)

Semantic Actions for S  if B then M S N else M S backpatch($2.true, $4); backpatch($2.false, $8); tmplist = merge($5, $6); $$ = merge(tmplist, $9); } ; N  ε { N.next = nextquad; gen(goto, _, _, _, void); }

Semantic Actions for S  if B then M S backpatch($2.true, $4); $$ = merge($2.false, $5); } ;

While Statement

C For statement

Project 4 – Generating Postfix Code for Expressions and Control Flow Booleans If B then assign else assign Undeclared variables print error message including line number Write up in the email soon

Procedure Calls

Activation Records