Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Table-driven syntax-directed.

Slides:



Advertisements
Similar presentations
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Advertisements

Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and.
Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
Joey Paquet, 2000, 2002, Lecture 3 Syntactic Analysis Part I.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Copyright Joey Paquet, Lecture 9 Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Introduction to code generation.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
Bernd Fischer RW713: Compiler and Software Language Engineering.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntactic analysis: Part.
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Joey Paquet, 2000, 2002, 2008, Lecture 8 Syntax-Directed Translation.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Joey Paquet, 2000, Lecture 8 Syntax-Directed Translation.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Bernd Fischer RW713: Compiler and Software Language Engineering.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Error recovery in top-down.
Compiler design Syntax-Directed Translation
A Simple Syntax-Directed Translator
Compiler design Bottom-up parsing Concepts
Compiler design Introduction to code generation
Compiler design Bottom-up parsing: Canonical LR and LALR
CS416 Compiler Design lec00-outline September 19, 2018
CS 3304 Comparative Languages
Syntax-Directed Translation
BOTTOM UP PARSING Lecture 16.
Bottom Up Parsing.
Syntax-Directed Translation Part II
CS416 Compiler Design lec00-outline February 23, 2019
Syntax-Directed Translation Part II
Table-Driven Syntax-Directed Translation (Top-Down and Bottom-Up)
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler design Table-driven syntax-directed translation
COMP 442/6421 – Compiler Design
Compiler design Bottom-up parsing: Canonical LR and LALR
Compiler design Review COMP 442/6421 – Compiler Design
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Table-driven syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Top-down table-driven syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Augment the parser algorithm to implement attribute migration. Introduce additional symbols in the grammar’s right hand sides for semantic actions that process semantic attributes. The grammar becomes an attribute grammar. When such a symbol is on top of the stack, execute the semantic action. Problem: the attributes have to be pushed and popped at a different pace compared to symbols on the parsing stack. Solution: use an additional stack (the semantic stack) to store the attributes. The semantic actions typically pop semantic records from the semantic stack, do some processing, then push a semantic record on the stack. Top-down table-driven syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Top-down table-driven syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design Attribute Grammar r1: E  TAE’B r2: E’ 1  +TCE’ 2 D r3: E’  E r4: T  FFT’G r5: T’ 1  *FHT’ 2 I r6: T’  J r7: F  idK r8: F  (E)L Semantic Actions A:A:{E’i = Ts} B:B:{Es = E’s} C:C:{E’ 2 i = E’ 1 i+Ts} D:D:{E’ 1 s = E’ 2 s} E:E:{E’s = E’i} F:F:{T’i = Fs} G:G:{Ts = T’s} H:H:{T’ 2 i = T’ 1 i*Fs} I:I:{T’ 1 s = T’ 2 s} J:J:{T’s = T’i} K:K:{Fs = id.val} L:L:{Fs = Es} id()+*$ Er1 E’r3r2r3 Tr4 T’r6 r5r6 Fr7r8

Concordia University Department of Computer Science and Software Engineering Attribute migration Joey Paquet, COMP 442/6421 – Compiler Design id1+id2*id3$ E T T T' * F id T'  F id E'  T'  F id E' + K:{Fs = id.val} F:{T'i = Fs} J:{T's = T'i} G:{Ts = T's} A:{E'i = Ts} B:{Es = E's} D:{E's = E's} C:{E'i = E'i+Ts} E:{E's = E'i} I:{T's = T's} J:{T's = T'i} K:{Fs = id.val} H:{T'i = T'i * Fs} F:{T'i = Fs} K:{Fs = id.val} G:{Ts = T's}

Concordia University Department of Computer Science and Software Engineering Attribute migration Joey Paquet, COMP 442/6421 – Compiler Design id1*id2+id3$ E T T T' * F id F E'  T'  F id E' + K:{Fs = id.val} F:{T'i = Fs} G:{Ts = T's} A:{E'i = Ts} B:{Es = E's} D:{E's = E's} C:{E'i = E'i+Ts} E:{E's = E'i} I:{T's = T's} J:{T's = T'i} K:{Fs = id.val} H:{T'i = T'i * Fs} F:{T'i = Fs} K:{Fs = id.val} G:{Ts = T's} T'  J:{T's = T'i}

Concordia University Department of Computer Science and Software Engineering Parsing example using semantic stack for attribute migration Joey Paquet, COMP 442/6421 – Compiler Design parsing stackinputactionsemantic stack 1$Eid 1 *id 2 +id 3 $R1 2$BE'ATid 1 *id 2 +id 3 $R4 3$BE'AGT'FFid 1 *id 2 +id 3 $R7 4$BE'AGT'FKidid 1 *id 2 +id 3 $ 5$BE'AGT'FK*id 2 +id 3 $KF 1 s[id 1.val] 6$BE'AGT'F*id 2 +id 3 $FT' 1 i[id 1.val] 7$BE'AGT'*id 2 +id 3 $R5T' 1 i[id 1.val] 8$BE'AGIT'HF**id 2 +id 3 $T' 1 i[id 1.val] 9$BE'AGIT'HFid 2 +id 3 $R7T' 1 i[id 1.val] 10$BE'AGIT'HKidid 2 +id 3 $T' 1 i[id 1.val] 11$BE'AGIT'HK+id 3 $KT' 1 i[id 1.val] F 2 s[id 2.val] 12$BE'AGIT'H+id 3 $HT' 2 i[id 1.val * id 2.val] 13$BE'AGIT'+id 3 $R6T' 2 i[id 1.val * id 2.val] 14$BE'AGIJ+id 3 $JT' 2 s[id 1.val * id 2.val] 15$BE'AGI+id 3 $IT' 1 s[id 1.val * id 2.val] 16$BE'AG+id 3 $GT 1 s[id 1.val * id 2.val] 17$BE'A+id 3 $AE' 1 i[id 1.val * id 2.val]

Concordia University Department of Computer Science and Software Engineering Parsing example using semantic stack for attribute migration Joey Paquet, COMP 442/6421 – Compiler Design parsing stackinputactionsemantic stack 18$BE'+id 3 $R2E' 1 i[id 1.val * id 2.val] 19$BDE'CT++id 3 $E' 1 i[id 1.val * id 2.val] 20$BDE'CTid 3 $R4E' 1 i[id 1.val * id 2.val] 21$BDE'CGT'FFid 3 $R7E' 1 i[id 1.val * id 2.val] 22$BDE'CGT'FKidid 3 $E' 1 i[id 1.val * id 2.val] 23$BDE'CGT'FK$KE' 1 i[id 1.val * id 2.val] F 3 s[id 3.val] 24$BDE'CGT'F$FE' 1 i[id 1.val * id 2.val] T' 3 i[id 3.val] 25$BDE'CGT'$R6E' 1 i[id 1.val * id 2.val] T' 3 i[id 3.val] 26$BDE'CGJ$JE' 1 i[id 1.val * id 2.val] T' 3 s[id 3.val] 27$BDE'CG$GE' 1 i[id 1.val * id 2.val] T 2 s[id 3.val] 28$BDE'C$CE' 2 i[id 1.val * id 2.val + id 3.val] 29$BDE'$R3E' 2 i[id 1.val * id 2.val + id 3.val] 30$BDE$EE' 2 s[id 1.val * id 2.val + id 3.val] 31$BD$DE' 1 s[id 1.val * id 2.val + id 3.val] 32$B$B$BE 1 s[id 1.val * id 2.val + id 3.val] 33$$acceptE 1 s[id 1.val * id 2.val + id 3.val]

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Top-down syntax-directed translation grammar transformation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Problem: Left recursion is not allowed in predictive top-down parsing. We must transform the grammar. We saw how to transform a context-free grammar to remove left recursions. How is an attribute grammar transformed? Top-down syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design E T1T1 T2T2 T’ 2 * F1F1 id (v a : ) T’ 1  F2F2 id (v b : ) E’ 2  T’ 3  F3F3 id (v c : ) E’ 1 + a+b*c E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  id

Concordia University Department of Computer Science and Software Engineering With left recursions Joey Paquet, COMP 442/6421 – Compiler Design E E F id (v a : ) T F id (v b : ) + {Fs = lookup(a)} {Ts = Fs} {Es = Ts} T T {Fs = lookup(b)} * F id (v c : ) {Fs = lookup(c)} {Ts = Ts * Fs} {Es = Es + Ts} E  E+T{Es = Es+Ts} E  T{Es = Ts} T  T*F{Ts = Ts*Fs} T  F{Ts = Fs} F  id{Fs = lookup(id)} a+b*c

Concordia University Department of Computer Science and Software Engineering Without left recursions Joey Paquet, COMP 442/6421 – Compiler Design E T T T’ * F id (v a : ) T’  F id (v b : ) E’  T’  F id (v c : ) E’ + E  T{E’i=Ts}E’{Es=E’s} E’  +T{E’i=Ts}E’{E’s=E’i+E’s} E’  {E’s=E’i} T  F{T’i=Fs}T’{Ts=T’s} T’  F{T’i=Fs}T’ {T’s=Ti*T’s} T’  {T’s=T’i} F  id{Fs=lookup(id)} {Fs = lookup(a)} {Fs = lookup(b)} {Fs = lookup(c)} {E’i = Ts} {T’i = Fs} {T’s = Ti} {Ts = T’s} {T’i = Fs} {T’s = T’i} {T’i = Fs} {T’s = Ti*T’s} {Ts = T’s} {E’i = Ts} {E’s = E’i} {E’s = E’i+E’s} {Es = E’s} a+b*c

Concordia University Department of Computer Science and Software Engineering Top-down syntax-directed translation: attribute grammar transformation Joey Paquet, COMP 442/6421 – Compiler Design Solution: The grammar is transformed as we saw before to eliminate left recursions in a context-free grammar. But when we introduce attributes, the transformation spreads some attributes over multiple rules, thus introducing the need for attribute inheritance. The following transformation should be applied: A 1  A 2  A 1  Q 1 A 3  Q 2  Q 3 Q 4   A 1  A 2  {A 1 s=f(A 2 s, )} A 1  {Q 1 i=g()}Q 1 {A 1 s=Q 1 s} A 3   {A 3 s=g()}Q 2  {Q 3 i=f(Q 2 i, )}Q 3 {Q 2 s=Q 3 s} Q 4  {Q 4 s=Q 4 i}

Concordia University Department of Computer Science and Software Engineering Top-down syntax-directed translation: attribute grammar transformation Joey Paquet, COMP 442/6421 – Compiler Design E 1  E 2 +T 1 {E 1 s=E 2 s+T 1 s} E 1  T 2 {E 1 ’i=T 2 s}E 1 ’{E 1 s=E 1 ’s} E 3  T 2 {E 3 s=T 2 s}E 2 ’  +T 1 {E 3 ’i=E 2 ’i+T 1 s}E 3 ’{E 2 ’s=E 3 ’s} E 4 ’  {E 4 ’s=E 4 ’i} E 1  E 2 +T 1 E 1  T 2 E 1 ’ E 3  T 2 E 2 ’  +T 1 E 3 ’ E 4 ’   A 1  A 2  A 1  Q 1 A 3  Q 2  Q 3 Q 4   A 1  A 2  {A 1 s=f(A 2 s,)} A 1  {Q 1 i=g()}Q 1 {A 1 s=Q 1 s} A 3  {A 3 s=g()}Q 2  {Q 3 i=f(Q 2 i,)}Q 3 {Q 2 s=Q 3 s} Q 4  {Q 4 s=Q 4 i} where: A 1,2,3  E 1,2,3 Q 1,2,3,4  E’ 1,2,3,4   +T 1   T 2 f(A 2 s, ) E 2 s+T 1 s g()  T 2 s

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Bottom-up syntax-directed translation Building an abstract syntax tree using syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Syntax-directed translation is much easier to implement bottom-up than top-down. Synthetized attributes are propagated from the bottom-up, so we have this propagation mechanism for free in a bottom-up parse The presence of inherited attributes generally comes from the elimination of left-recursions and ambiguities. As these are not a problem in bottom- up parsing, we seldom need to process inherited attributes in bottom-up translation In bottom-up translation, the parse and semantic stacks move in synchronism, so there is no need for an additional semantic stack. Semantic rules are triggered as handles are popped from the stack Bottom-up syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering The tree is built by grafting subtrees (handles) to the nodes. The semantic actions build the subtrees by grafting generally through simple pointer manipulations. Generally, all nodes (internal or leaves) are of the same type. Differences can be managed by using a variant record structure: Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, COMP 442/6421 – Compiler Design nodeKind = (internal,leaf) treeNode = record token : tokenType case kind : nodeKind of internal : (left,right : *treeNode) leaf : (location : integer) end

Concordia University Department of Computer Science and Software Engineering We need a makeTree function that will be used to create subtrees and a makeLeaf function that will be used to create tree leaves: Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, COMP 442/6421 – Compiler Design nodePtr makeTree(op tokenType, rightSon,leftSon nodePtr){ rootPtr = new(nodePtr) rootPtr.kind = internal rootPtr.token = op rootPtr.left = leftSon rootPtr.right = rightSon return rootPtr } nodePtr makeLeaf(tok tokenType, location integer){ leafPtr = new(nodePtr) leafPtr.kind = leaf leafPtr.token = tok leafPtr.location = location return leafPtr }

Concordia University Department of Computer Science and Software Engineering Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, COMP 442/6421 – Compiler Design r1: S  id = EA r2: E 1  E 2 + E 3 B r3: E 1  E 2 * E 3 C r4: E 1  (E 2 )D r5: E  idE A:A:{S.root = makeTree(‘=‘, id.location, E.root)} B:B:{E 1.root = makeTree(‘+’, E 2.root, E 3.root)} C:C:{E 1.root = makeTree(‘*’, E 2.root, E 3.root)} D:D:{E 1.root = E 2.root} E:E:{E 1.root = makeLeaf(id, id.location)}

Concordia University Department of Computer Science and Software Engineering Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, COMP 442/6421 – Compiler Design parsing stackinputaction 1$id = (id+id)*id$ 2$id= (id+id)*id$ 3$id=(id+id)*id$ 4$id=(id+id)*id$ 5$id=(id+id)*id$r5 E 6$id=(E 1 +id)*id$ 7$id=(E 1 +id)*id$ 8$id=(E 1 + id)*id$r5 E 9$id=(E 1 + E 2 )*id$ 10$id=(E 1 + E 2 )*id$r2 B 11$id=(E 3 )*id$r4 D 12$id=E 4 *id$ 13$id=E 4 *id$ 14$id=E 4 * id$r5 E 15$id=E 4 * E 5 $r3 C 16$id=E 6 $r1 A 17$S$S$accept

Concordia University Department of Computer Science and Software Engineering Bottom-up syntax-directed translation: building an abstract syntax tree Joey Paquet, COMP 442/6421 – Compiler Design E1E1 id 1 E2E2 id 2 E3E3 E1E1 E2E2 + E3E3 + id 1 id 2 E4E4 + id 1 id 2 E5E5 id 3 E6E6 E4E4 E5E5 * + id 1 id 2 E6E6 * id 3 + id 1 id 2 * id 3 = S id 4 A B C DE E E

Concordia University Department of Computer Science and Software Engineering We can use a similar process to build other kinds of intermediate representations. A similar process can also be used to generate target code directly, but that diminishes the possibilities of high-level code optimization. A similar technique can also be adopted by top-down parsing to build syntax trees as an intermediate representation. Bottom-up syntax-directed translation Joey Paquet, COMP 442/6421 – Compiler Design

Concordia University Department of Computer Science and Software Engineering Fischer, Cytron, Leblanc. Crafting a Compiler, Chapter 7. Addison- Wesley References Joey Paquet, COMP 442/6421 – Compiler Design