Syntax-Directed Translation Part I

Similar documents
Principles of Programming Languages

Principles of Programming Languages

Syntax-Directed Translation Part II

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation. Introduction

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

Syntax-Directed Translation

Syntax-Directed Translation

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

Syntax Directed Translation

Syntax-Directed Translation

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Syntax Directed Translation

Abstract Syntax Tree

Syntax-Directed Translation

Semantic Analysis Attribute Grammars

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

COP5621 Exam 3 - Spring 2005

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Syntactic Directed Translation

Syntax Analysis Part IV

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

Summary: Semantic Analysis

Static and Dynamic Semantics

Static Checking and Type Systems

Lecture 14 Sections Mon, Mar 2, 2009

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Abstract Syntax Trees & Top-Down Parsing

Compiler Construction Assignment 3 Spring 2018

Syntax-Directed Translation

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

LR Parsing LALR Parser Generators

Chapter 4. Action Routines

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Abstract Syntax Trees Synthetic and Inherited Attributes

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

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

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

More On Syntax Directed Translation

LECTURE 3. Compiler Phases

Introduction to Compiler Construction

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

5. Syntax-Directed Definitions & Type Analysis

CSE302: Compiler Design

A Simple Syntax-Directed Translator

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Introduction to Compiler Construction

Attribute Grammars. Attribute Grammars

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

COP4020 Spring 2011 Midterm Exam

LR Parsing LALR Parser Generators

Chapter 2: Syntax Directed Translation and YACC

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Lecture Compiler Construction

Semantic Analysis with Attribute Grammars Part 3

4. Semantic Processing and Attributed Grammars

Semantic analysis. Syntax tree is decorated with typing- and other context dependent

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Programming Languages

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Syntax Directed Translation

CS 406: Syntax Directed Translation

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Intermediate Code Generation Part II

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Chapter 4 :: Semantic Analysis

Run-Time Environments

COP4020 Programming Assignment 2 - Fall 2016

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Lexical and Syntax Analysis

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Intermediate Code Generation

Semantic Analysis. Compiler Architecture

COP4020 Programming Assignment 2 Spring 2011

Type Checking. Chapter 6, Section 6.3, 6.5

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

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Introduction to Programming Using Java (98-388)

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Compiler Design Aug 1996

Chapter 6 Intermediate Code Generation

Syntax-Directed Translation. Lecture 14

Compiler Lab. Introduction to tools Lex and Yacc

Compiler Front-End. Compiler Back-End. Specific Examples

Principles of Programming Languages [PLP-2015] Detailed Syllabus

Using an LALR(1) Parser Generator

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

Transcription:

1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011

2 The Structure of our Compiler Revisited Character stream Lexical analyzer Token stream Syntax-directed translator Java bytecode Lex specification Yacc specification with semantic rules JVM specification

3 Syntax-Directed Definitions A syntax-directed definition (or attribute grammar) binds a set of semantic rules to productions Terminals and nonterminals have attributes holding values set by the semantic rules A depth-first traversal algorithm traverses the parse tree thereby executing semantic rules to assign attribute values After the traversal is complete the attributes contain the translated form of the input

4 Example Attribute Grammar Production L E n E E 1 + T E T T T 1 * F T F F ( E ) F digit Semantic Rule print(e.val) E.val := E 1.val + T.val E.val := T.val T.val := T 1.val * F.val T.val := F.val F.val := E.val F.val := digit.lexval Note: all attributes in this example are of the synthesized type

5 Example Annotated Parse Tree E.val = 16 L E.val = 14 E.val = 9 T.val = 5 T.val = 2 F.val = 5 T.val = 9 F.val = 5 F.val = 9 9 + 5 + 2 n Note: all attributes in this example are of the synthesized type

6 Annotating a Parse Tree With Depth-First Traversals procedure visit(n : node); begin for each child m of n, from left to right do visit(m); evaluate semantic rules at node n end

7 Depth-First Traversals (Example) L print(16) E.val = 16 E.val = 14 E.val = 9 T.val = 5 T.val = 2 F.val = 5 T.val = 9 F.val = 5 F.val = 9 9 + 5 + 2 n Note: all attributes in this example are of the synthesized type

8 Attributes Attribute values may represent Numbers (literal constants) Strings (literal constants) Memory locations, such as a frame index of a local variable or function argument A data type for type checking of expressions Scoping information for local declarations Intermediate program representations

9 Synthesized Versus Inherited Attributes Given a production A α then each semantic rule is of the form b := f(c 1,c 2,,c k ) where f is a function and c i are attributes of A and α, and either b is a synthesized attribute of A b is an inherited attribute of one of the grammar symbols in α

10 Synthesized Versus Inherited Attributes (cont d) Production Semantic Rule inherited D T L T int L id L.in := T.type T.type := integer := L.in synthesized

11 S-Attributed Definitions A syntax-directed definition that uses synthesized attributes exclusively is called an S-attributed definition (or S-attributed grammar) A parse tree of an S-attributed definition is annotated with a single bottom-up traversal Yacc/Bison only support S-attributed definitions

12 Example Attribute Grammar in Yacc %token DIGIT %% L : E \n { printf( %d\n, $1); } ; E : E + T { $$ = $1 + $3; } T { $$ = $1; } ; T : T * F { $$ = $1 * $3; } F { $$ = $1; } ; F : ( E ) { $$ = $2; } DIGIT { $$ = $1; } ; %% Synthesized attribute of parent node F

Bottom-up Evaluation of 13 S-Attributed Definitions in Yacc Stack $ $ 3 $ F $ T $ T * $ T * 5 $ T * F $ T $ E $ E + $ E + 4 $ E + F $ E + T $ E $ E n $ L val _ 3 3 3 3 _ 3 _ 5 3 _ 5 15 15 15 _ 15 _ 4 15 _ 4 15 _ 4 19 19 _ 19 Input 3*5+4n$ *5+4n$ *5+4n$ *5+4n$ 5+4n$ +4n$ +4n$ +4n$ +4n$ 4n$ n$ n$ n$ n$ $ $ Action shift reduce F digit reduce T F shift shift reduce F digit reduce T T * F reduce E T shift shift reduce F digit reduce T F reduce E E + T shift reduce L E n accept Semantic Rule $$ = $1 $$ = $1 $$ = $1 $$ = $1 * $3 $$ = $1 $$ = $1 $$ = $1 $$ = $1 + $3 print $1

14 Example Attribute Grammar with Synthesized+Inherited Attributes Production D T L T int T real L L 1, id L id Semantic Rule L.in := T.type T.type := integer T.type := real L 1.in := L.in; addtype(id.entry, L.in) addtype(id.entry, L.in) Synthesized: T.type, id.entry Inherited: L.in

15 Acyclic Dependency Graphs for Attributed Parse Trees A X Y Direction of value dependence A.a X.x Y.y A.a X.x Y.y A.a X.x Y.y A.a := f(x.x, Y.y) X.x := f(a.a, Y.y) Y.y := f(a.a, X.x)

16 Dependency Graphs with Cycles? Edges in the dependency graph determine the evaluation order for attribute values Dependency graphs cannot be cyclic X.x A.a Y.y A.a := f(x.x) X.x := f(y.y) Y.y := f(a.a) Error: cyclic dependence

17 Example Annotated Parse Tree D T.type = real L.in = real real L.in = real, id 3.entry L.in = real id 1.entry, id 2.entry

18 Example Annotated Parse Tree with Dependency Graph D T.type = real L.in = real real L.in = real, id 3.entry L.in = real id 1.entry, id 2.entry

19 Evaluation Order A topological sort of a directed acyclic graph (DAG) is any ordering m 1, m 2,, m n of the nodes of the graph, such that if m i m j is an edge, then m i appears before m j Any topological sort of a dependency graph gives a valid evaluation order of the semantic rules

20 Example Parse Tree with Topologically Sorted Actions T 1.type = real real L 3.in = real 9 10 1 id 1.entry D L 2.in = real, L 1.in = real 4 5 6 7 8 2, 3 id 2.entry id 3.entry Topological sort: 1. Get id 1.entry 2. Get id 2.entry 3. Get id 3.entry 4. T 1.type= real 5. L 1.in=T 1.type 6. addtype(id 3.entry, L 1.in) 7. L 2.in=L 1.in 8. addtype(id 2.entry, L 2.in) 9. L 3.in=L 2.in 10. addtype(id 1.entry, L 3.in)

21 Evaluation Methods Parse-tree methods determine an evaluation order from a topological sort of the dependence graph constructed from the parse tree for each input Rule-base methods the evaluation order is predetermined from the semantic rules Oblivious methods the evaluation order is fixed and semantic rules must be (re)written to support the evaluation order (for example S-attributed definitions)

22 L-Attributed Definitions The example parse tree on slide 18 is traversed in order, because the direction of the edges of inherited attributes in the dependency graph point top-down and from left to right More precisely, a syntax-directed definition is L- attributed if each inherited attribute of X j on the right side of A X 1 X 2 X n depends only on 1. the attributes of the symbols X 1, X 2,, X j-1 2. the inherited attributes of A Shown: dependences of inherited attributes A.a X 1.x X 2.x

23 L-Attributed Definitions (cont d) L-attributed definitions allow for a natural order of evaluating attributes: depth-first and left to right A X Y X.i:=A.i X A Y Y.i:=X.s A.s:=Y.s X.i := A.i Y.i := X.s A.s := Y.s Note: every S-attributed syntax-directed definition is also L-attributed

24 Using Translation Schemes for L-Attributed Definitions Production D T L T int T real L L 1, id L id Semantic Rule L.in := T.type T.type := integer T.type := real L 1.in := L.in; addtype(id.entry, L.in) addtype(id.entry, L.in) Translation Scheme D T { L.in := T.type } L T int { T.type := integer } T real { T.type := real } L { L 1.in := L.in } L 1, id { addtype(id.entry, L.in) } L id { addtype(id.entry, L.in) }

25 Implementing L-Attributed Definitions in Top-Down Parsers Attributes in L-attributed definitions implemented in translation schemes are passed as arguments to procedures (synthesized) or returned (inherited) D T { L.in := T.type } L T int { T.type := integer } T real { T.type := real } void D() { Type Ttype = T(); Type Lin = Ttype; L(Lin); } Type T() { Type Ttype; if (lookahead == INT) { Ttype = TYPE_INT; match(int); } else if (lookahead == REAL) { Ttype = TYPE_REAL; match(real); } else error(); return Ttype; } void L(Type Lin) { } Output: synthesized attribute Input: inherited attribute

26 Implementing L-Attributed Definitions in Bottom-Up Parsers More difficult and also requires rewriting L- attributed definitions into translation schemes Insert marker nonterminals to remove embedded actions from translation schemes, that is A X { actions } Y is rewritten with marker nonterminal N into A X N Y N ε { actions } Problem: inserting a marker nonterminal may introduce a conflict in the parse table

27 Emulating the Evaluation of L-Attributed Definitions in Yacc D T { L.in := T.type } L T int { T.type := integer } T real { T.type := real } L { L 1.in := L.in } L 1, id { addtype(id.entry, L.in) } L id { addtype(id.entry, L.in) } %{ Type Lin; /* global variable */ %} %% D : Ts L ; Ts : T { Lin = $1; } ; T : INT { $$ = TYPE_INT; } REAL { $$ = TYPE_REAL; } ; L : L, ID { addtype($3, Lin);} ID { addtype($1, Lin);} ; %%

28 Rewriting a Grammar to Avoid Inherited Attributes Production D L : T T int T real L L 1, id L id D Production D id L T int T real L, id L 1 L : T D Semantic Rule addtype(id.entry, L.type) T.type := integer T.type := real addtype(id.entry, L.type) L.type := T.type : T id id,id, int id,id, : T int