Compiler Construction

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Decision Structures – The Syntax Tree Lecture 22 Fri, Apr 8, 2005.
Backpatching: The syntax directed definition we discussed before can be implemented in two or more passes (we have both synthesized attributes and inheritent.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Compiler Construction Sohail Aslam Lecture Boolean Expressions E → E 1 and M E 2 {backpatch(E 1.truelist, M.quad); E.truelist = E 2.truelist; E.falselist.
Compiler Designs and Constructions
Three Address Code Generation Backpatching-I Prepared By: Siddharth Tiwary 04CS3010.
Backpatching דוגמא : switch. דוגמא switch : הדקדוק.
Lecture 09 – IR (Backpatching) Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6
Lecture 15 Control Flow Topics Review Positional Encoding of Booleans Short Circuit Evaluation Control Flow Statements Readings: 8.4, 8.6 March 13, 2006.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
Test Yourself #2 Test Yourself #3 Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Syntax Specification and BNF © Allan C. Milne Abertay University v
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Backpatching Lecture 24 Fri, Apr 16, Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java.
Languages and Compilers
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
Boolean expressions 1 productionsemantic action E  E1 or E2E1.trueLabel = E.trueLabel; E1.falseLabel = freshLabel(); E2.trueLabel = E.trueLabel; E2.falseLabel.
Chap. 4, Intermediate Code Generation
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Three Address Code Generation of Control Statements continued..
Imperative Programming Statements and invariants.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Review: Chapter 5: Syntax directed translation
Compiler Lecture 1 CS510.
Intermediate Code Generation
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax-Directed Translation Part II
FLOW OF CONTROL.
Intermediate Code Generation Part II
Three Address Code Generations for Boolean Functions
Three Address Code Generation Control Statement
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part II
Three Address Code Generation - Control Statements
Syntax-Directed Translation Part II
Compiler Construction
Intermediate Code Generation Part II
Intermediate code generation
Compiler Construction
Syntax-Directed Translation Part II
Compiler Construction
Three Address Code Generation – Backpatching
Compiler Construction
Intermediate Code Generation Part II
Compiler Construction
Backpatching Lecture 23 Wed, Apr 13, 2005.
Syntax-Directed Translation Part II
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Intermediate Code Generation Part II
Compiler Construction
Compiler Construction
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 41 compiler: Bottom-up Parsing

Flow-of-Control Statements S → if E then S | if E then S else S | while E do S | begin L end | A L → L ; S | S S denotes a statement This is a note L is a statementlist A is assignment compiler: Bottom-up Parsing

Semantic Actions S → if E then M1 S1 N else M2 S2 { backpatch(E.truelist, M1.quad); backpatch(E.falselist, M2.quad); S.nextlist = merge(S1.nextlist, merge( N.nextlist,S2.nextlist)); } This is a note If E is true, jump to M1.quad which is start of code for S1 N is marker non-terminal to introduce jump over code for S2 compiler: Bottom-up Parsing

Semantic Actions N → e { N.nextlist = makelist(nextQuad()); emit(‘goto_’); } This is a note The attribute N.nextlist records the quad number of the goto it generates compiler: Bottom-up Parsing

Semantic Actions M → e { M.quad = nextQuad(); } This is a note compiler: Bottom-up Parsing

Semantic Actions S → if E then M S1 { backpatch(E.truelist, M.quad); S.nextlist = merge( E.falselist,S1.nextlist); } This is a note If E is true, jump to M.quad which is start of code for S1 compiler: Bottom-up Parsing

Semantic Actions S → while M1 E do M2 S1 { backpatch(S1.nextlist, M1.quad); backpatch(E.truelist, M2.quad); S.nextlist = E.falselist; emit( ‘goto’ M1.quad); } This is a note compiler: Bottom-up Parsing

Semantic Actions S → begin L end { S.nextlist = L.nextlist; } This is a note compiler: Bottom-up Parsing

Semantic Actions S → A { S.nextlist = nil; } This is a note initializes S.nextlist to an empty list compiler: Bottom-up Parsing

Semantic Actions L → L1 ; M S { backpatch(L1.nextlist, M.quad); L.nextlist = S.nextlist; } This is a note Statement following L1 in order of execution is beginning of S compiler: Bottom-up Parsing

Semantic Actions L → S { L.nextlist = S.nextlist; } This is a note compiler: Bottom-up Parsing

Example if a<b or c<d and e<f then x=y+z else x=y-z if E1 or M E2 then x=y+z else x=y-z This is a note compiler: Bottom-up Parsing

if E1 or M E2 then x=y+z else x=y-z if E then x=y+z else x=y-z { E.truelist=[100,104] E.falselist=[103,105] } This is a note compiler: Bottom-up Parsing

100 if a < b goto _ 101 goto 102 102 if c < d goto 104 103 if e < f goto _ 105 goto_ 106 107 108 109 This is a note compiler: Bottom-up Parsing

if E then M1 x=y+z else x=y-z M1 →  { M1.quad = 106 } if E then M1 A else x=y-z A → x=y+z { emit(‘x=y+z’) } if E then M1 S1 else A S1 → A { S1.nextlist = nil} if E then M1 S1 N else x=y-z N →  { N.nextlist = [107] emit(‘goto _’ } This is a note compiler: Bottom-up Parsing

100 if a < b goto _ 101 goto 102 102 if c < d goto 104 103 if e < f goto _ 105 106 x=y+z 107 108 109 This is a note compiler: Bottom-up Parsing

if E then M1 S1 N else M2 x=y-z M2 →  { M2.quad = 108 } if E then M1 S1 N else M2 A A → x=y-z { emit(‘x=y-z’) } if E then M1 S1 N else M2 S2 S2 → A { S2.nextlist = nil } S { backpatch([100,104],106) backpatch([103,105],108) S.nextlist=[107]} This is a note compiler: Bottom-up Parsing

100 if a < b goto _ 101 goto 102 102 if c < d goto 104 103 if e < f goto _ 105 106 x=y+z 107 108 x=y-z 109 106 108 106 108 This is a note compiler: Bottom-up Parsing

100 if a < b goto 106 101 goto 102 102 if c < d goto 104 103 if e < f goto 106 105 106 x=y+z 107 goto _ 108 x=y-z 109 This is a note compiler: Bottom-up Parsing

Semantic Actions in YACC The syntax-directed translation statements can be conveniently specified in YACC The %union will require more fields because the attributes vary This is a note compiler: Bottom-up Parsing

Semantic Actions in YACC The actual mechanics will be covered in the handout for the syntax-directed translation phase of the course project This is a note compiler: Bottom-up Parsing