Compiler Construction

Similar documents
Compiler Construction

Compiler Construction

Semantics and Verification of Software

A programming language requires two major definitions A simple one pass compiler

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Introduction to Syntax Analysis. The Second Phase of Front-End

Compiler Construction

Compiler Construction

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Types of parsing. CMSC 430 Lecture 4, Page 1

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Compiler Construction

Introduction to Syntax Analysis

CS 321 Programming Languages and Compilers. VI. Parsing

Compiler Construction

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CMSC 330: Organization of Programming Languages

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Compiler Construction

Finite State Automata are Limited. Let us use (context-free) grammars!

Ambiguous Grammars and Compactification

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

4. Lexical and Syntax Analysis

Monday, September 13, Parsers

Lexical and Syntax Analysis. Top-Down Parsing

4. Lexical and Syntax Analysis

Chapter 3 (part 3) Describing Syntax and Semantics

COP4020 Spring 2011 Midterm Exam

Parsing Part II (Top-down parsing, left-recursion removal)

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Principles of Programming Languages

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

Recursive Descent Parsers

Computer Science 160 Translation of Programming Languages

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

Lexical and Syntax Analysis

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Chapter 4. Lexical and Syntax Analysis

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Syntax-Directed Translation Part I

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Building Compilers with Phoenix

Syntax-Directed Translation

Chapter 4: LR Parsing

Solving systems of regular expression equations

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

CS 403: Scanning and Parsing

3. Parsing. Oscar Nierstrasz

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

CS 314 Principles of Programming Languages

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Context-free grammars

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Context-Free Languages and Parse Trees

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Static Program Analysis

CSE302: Compiler Design

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Compiler Construction: Parsing

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.


Semantic Analysis with Attribute Grammars Part 1

Syntactic Analysis. Top-Down Parsing

Syntax Analysis, III Comp 412

Dr. D.M. Akbar Hussain

Chapter 2 The Language PCF

Abstract Syntax Trees & Top-Down Parsing

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

CS 406/534 Compiler Construction Parsing Part I

Question Bank. 10CS63:Compiler Design

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

LL parsing Nullable, FIRST, and FOLLOW

Syntax Analysis Part I

Semantic Analysis Attribute Grammars

Compiler construction lecture 3

Transcription:

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/

Recap: Circularity of Attribute Grammars Circularity of Attribute Grammars Goal: unique solvability of equation system = avoid cyclic dependencies Definition (Circularity) An attribute grammara = G, E, V AG is called circular if there exists a syntax tree t such that the attribute equation system E t is recursive (i.e., some attribute variable of t depends on itself). Otherwise it is called noncircular. Remark: because of the division of Var π into In π and Out π, cyclic dependencies cannot occur at production level. 3 of 22 Compiler Construction

Recap: Circularity of Attribute Grammars Attribute Dependency Graphs and Circularity I Observation: a cycle in the dependency graph D t of a given syntax tree t is caused by the occurrence of a cover productionπ = A 0 w 0 A 1 w 1...A r w r P in a node k 0 of t such that the dependencies in E k0 yield the upper end of the cycle and for at least one i [r], some attributes in syn(a i ) depend on attributes in inh(a i ). Example on the board To identify such critical situations we need to determine for each i [r] the possible ways in which attributes insyn(a i ) can depend on attributes ininh(a i ). 4 of 22 Compiler Construction

Recap: Circularity of Attribute Grammars Attribute Dependency Graphs and Circularity II Definition (Attribute dependence) LetA = G, E, V AG with G = N,Σ, P, S. If t is a syntax tree with root label A N and root node k, α syn(a), and β inh(a) such α.k, then α is dependent on β below A in t (notation: β A α). that β.k + t For every syntax tree t with root label A N, is(a, t) := {(β,α) inh(a) syn(a) β A α in t}. For every A N, IS(A) := {is(a, t) t syntax tree with root label A} 2 Inh Syn. Remark: it is important that IS(A) is a system of attribute dependence sets, not a union (otherwise: strong noncircularity see exercises). Example on the board 5 of 22 Compiler Construction

The Circularity Check The Circularity Check I In the circularity check, the dependency systems IS(A) are iteratively computed. The following notation is employed: Definition 14.1 Givenπ = A w 0 A 1 w 1...A r w r P and is i inh(a i ) syn(a i ) for each i [r], is defined by is[π; is 1,...,is r ] inh(a) syn(a) { is[π; is 1,...,is r ] := (β,α) (β.0,α.0) ( π r i=1 {(β.p i,α.p i ) (β,α ) is i }) +} where p i := i j=1 w j 1 +i. Example 14.2 on the board 7 of 22 Compiler Construction

The Circularity Check The Circularity Check II Algorithm 14.3 (Circularity check for attribute grammars) Input: A = G, E, V AG with G = N,Σ, P, S Procedure: 1. for every A N, iteratively construct IS(A) as follows: i. ifπ = A w P, then is[π] IS(A) ii. ifπ = A w 0 A 1 w 1...A r w r P and is i IS(A i ) for every i [r], then is[π; is 1,...,is r ] IS(A) 2. test whetherais circular by checking if there exist π = A w 0 A 1 w 1...A r w r P and is i IS(A i ) for every i [r] such that the following relation is cyclic: π r i=1 {(β.p i,α.p i ) (β,α) is i } (where p i := i j=1 w j 1 +i) Output: yes or no 8 of 22 Compiler Construction

The Circularity Check The Circularity Check III Example 14.4 D S AB : S α D A B : β A α 1 α 2 D A c : β A α 1 α 2 β α 1 α 2 β α 1 α 2 A B β B α 1 α 2 c D B AB : β B α 1 α 2 D A a : β A α 1 α 2 D B b : β B α 1 α 2 β α 1 α 2 β α 1 α 2 A Application of Algorithm 14.3: on the board B a b 9 of 22 Compiler Construction

Correctness and Complexity of the Circularity Check Correctness and Complexity of Circularity Check Theorem 14.5 (Correctness of circularity check) An attribute grammar is circular iff Algorithm 14.3 yields the answer yes Proof. by induction on the syntax tree t with cyclic D t Lemma 14.6 The time complexity of the circularity check is exponential in the size of the attribute grammar (= maximal length of right-hand sides of productions). Proof. by reduction of the word problem of alternating Turing machines (see M. Jazayeri: A Simpler Construction for Showing the Intrinsically Exponential Complexity of the Circularity Problem for Attribute Grammars, Comm. ACM 28(4), 1981, pp. 715 720) 11 of 22 Compiler Construction

Attribute Evaluation Attribute Evaluation Methods Given: noncircular attribute grammar A = G, E, V AG syntax tree t of G valuation v : Syn Σ V for Syn Σ := {α.k k labelled by a Σ,α syn(a)} Var t Goal: extend v to (partial) solution v : Var t V Methods: 1. Topological sorting of D t (later): i. start with variables which depend at most on Syn Σ ii. proceed by successive substitution 2. Strongly noncircular AGs: recursive functions (details omitted) i. for every A N andα syn(a), define evaluation function g A,α with the following parameters: the node of t whereαhas to be evaluated and all inherited attributes of A on whichα(potentially) depends ii. for everyα syn(s), evaluate g S,α (k 0 ) where k 0 denotes the root of t 3. L-attributed grammars: integration with top-down parsing (later) 4. S-attributed grammars (i.e., Inh = ): yacc 13 of 22 Compiler Construction

Attribute Evaluation by Topological Sorting Attribute Evaluation by Topological Sorting Algorithm 14.7 (Evaluation by topological sorting) Input: noncirculara = G, E, V AG, syntax tree t of G, v : Syn Σ V Procedure: 1. let Var := Var t \ Syn Σ (* attributes to be evaluated *) 2. while Var do i. let x Var such that{y Var y t x} = ii. let x = f(x 1,...,x n ) E t iii. let v(x) := f(v(x 1 ),...,v(x n )) iv. let Var := Var \{x} Output: solution v : Var t V Remark: noncircularity guarantees that in step 2.i at least one such x is available Example 14.8 see Examples 12.1 and 12.2 (Knuth s binary numbers) 15 of 22 Compiler Construction

L-Attributed Grammars L-Attributed Grammars I In an L-attributed grammar, attribute dependencies on the right-hand sides of productions are only allowed to run from left to right. Definition 14.1 (L-attributed grammar) LetA = G, E, V AG such that, for everyπ P andβ.i = f(...,α.j,...) E π withβ Inh andα Syn, j < i. Then A is called an L-attributed grammar (notation: A LAG). Remark: note that no restrictions are imposed forβ Syn (for i = 0) orα Inh (for j = 0). Thus, in an L-attributed grammar, synthesized attributes of the left-hand side can depend on any outer variable and every inner variable can depend on any inherited attribute of the left-hand side. Corollary 14.2 Every A LAG is noncircular. 17 of 22 Compiler Construction

L-Attributed Grammars L-Attributed Grammars II Example 14.3 S 6 L-attributed grammar: S AB i.1 = 0 i.2 = s.1+1 s.0 = s.2+1 A aa i.2 = i.0+1 s.0 = s.2+1 A c s.0 = i.0+1 B b s.0 = i.0+1 a 0 A 3 4 B 5 1 A 2 c b 18 of 22 Compiler Construction

L-Attributed Grammars Evaluation of L-Attributed Grammars Observation 1: the syntax tree of an L-attributed grammar can be attributed by a depth-first, left-to-right tree traversal with two visits to each node 1. top-down: evaluation of inherited attributes 2. bottom-up: evaluation of synthesized attributes Observation 2: visit sequence fits nicely with parsing 1. top-down: expansion steps 2. bottom-up: reduction steps Idea: extend LL parsing to support reduction steps, and integrate attribute evaluation = use recursive-descent parser and add variables and operations for attribute evaluation 19 of 22 Compiler Construction

L-Attributed Grammars Recursive-Descent Parsing and Attribute Evaluation I Ingredients: variable token for current token function next() for invoking the scanner procedureprint(i) for displaying the leftmost analysis (or errors) Method: to every A N we assign a procedure A(in: inh(a), out: syn(a)) which declares local variables for synthesized attributes on right-hand sides, tests token with regard to the lookahead sets of the A-productions, prints the corresponding rule number and evaluates the corresponding right-hand side as follows: for a Σ: checktoken; callnext() for A N: call A with appropriate parameters 20 of 22 Compiler Construction

L-Attributed Grammars Recursive-Descent Parsing II Example 14.4 (cf. Example 14.3) proc main(); token := next(); S() proc S(); if token in { a, c } then (* S AB *) print(1); A(); B() else print(error); stop fi proc A(); if token = a then (* A aa *) print(2); token := next(); A() elsif token = c then (* A c *) print(3); token := next() else print(error); stop fi proc B(); if token = b then (* B b *) print(4); token := next() else print(error); stop fi 21 of 22 Compiler Construction

L-Attributed Grammars Recursive-Descent Parsing and Attribute Evaluation II Example 14.5 (cf. Example 14.3) proc main(); var s; token := next(); S(s); print(s) proc S(out s0); var s1,s2; if token in { a, c } then (* S AB : i.1 = 0, i.2 = s.1+1, s.0 = s.2+1 *) print(1); A(0,s1); B(s1+1,s2); s0 := s2+1 else print(error); stop fi proc A(in i0,out s0); var s2; if token = a then (* A aa : i.2 = i.0+1, s.0 = s.2+1 *) print(2); token := next(); A(i0+1,s2); s0 := s2+1 elsif token = c then (* A c : s.0 = i.0+1 *) print(3); token := next(); s0 := i0+1 else print(error); stop fi proc B(in i0,out s0); if token = b then (* B b : s.0 = i.0+1 *) print(4); token := next(); s0 := i0+1 else print(error); stop fi 22 of 22 Compiler Construction