Principles of Programming Languages

Similar documents
Principles of Programming Languages

Syntax-Directed Translation Part I

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

Syntax-Directed Translation

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

Syntax-Directed Translation

Syntax-Directed Translation. Introduction

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

Syntax-Directed Translation

Syntax-Directed Translation Part II

[Syntax Directed Translation] Bikash Balami

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

Syntax Directed Translation

Semantic Analysis Attribute Grammars

Abstract Syntax Tree

Compila(on /15a Lecture 6. Seman(c Analysis Noam Rinetzky

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

Syntax Directed Translation

CSE302: Compiler Design

Syntax-Directed Translation

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

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

COP5621 Exam 3 - Spring 2005

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

Lecture 14 Sections Mon, Mar 2, 2009

Syntax-Directed Translation

5. Syntax-Directed Definitions & Type Analysis

Abstract Syntax Trees Synthetic and Inherited Attributes

Lecture Compiler Construction

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

Programming Languages

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

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

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Summary: Semantic Analysis

Announcements. Working in pairs is only allowed for programming assignments and not for homework problems. H3 has been posted

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Syntax and Grammars 1 / 21

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

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

Principles of Programming Languages

Syntactic Directed Translation

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

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

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

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

Principles of Programming Languages

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

Static and Dynamic Semantics

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

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

4. Semantic Processing and Attributed Grammars

A Simple Syntax-Directed Translator

Syntax-Directed Translation. Lecture 14

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

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

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL

Chapter 4. Action Routines

Chapter 4 :: Semantic Analysis

Principles of Programming Languages

Building Compilers with Phoenix

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Compiler Lab. Introduction to tools Lex and Yacc

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Principles of Programming Languages

Abstract Syntax Trees & Top-Down Parsing

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

UNIT IV INTERMEDIATE CODE GENERATION

Compiler Code Generation COMP360

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Semantic Analysis. Compiler Architecture

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Semantic Analysis. Role of Semantic Analysis

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Semantic Analysis with Attribute Grammars Part 3

Operational Semantics of Cool

Error Handling Syntax-Directed Translation Recursive Descent Parsing

CSE3322 Programming Languages and Implementation

Operational Semantics. One-Slide Summary. Lecture Outline

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

CS 415 Midterm Exam Spring 2002

Chapter 2: Syntax Directed Translation and YACC

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

Ways to implement a language

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

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

LECTURE 3. Compiler Phases

CS 406: Syntax Directed Translation

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

CS606- compiler instruction Solved MCQS From Midterm Papers

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

Undergraduate Compilers in a Day

Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Compiler Design (40-414)

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Transcription:

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 10! Con:nua:on of the course Syntax- Directed Transla:on (1)

Con:nua:on of the course [Nov- Dec 2014] 22 h Introduc:on to compilers Lexical analysis Parsing [Feb- May 2015] ~50 h Syntax directed transla:on Intermediate code genera:on Code genera:on ===================================== Concepts of Programming Languages <to be detailed > ===================================== May 27-29: 2 nd Mid- Term Exam Can be taken by everybody 2

Con:nua:on of the course (2) Office hours: Wednesday, 4-6 pm 9 Credits vs. 12 Credits: s:ll a problem for somebody? Important: no lectures on Friday, March 6 Tuesday, March 17 Friday, March 20 Need to recover several lectures with 3 lectures per week Possible days and hours: Thursday, 2-4 pm 3

The Structure of the Front- End Source Program (Character stream) Lexical analyzer Token stream Parser Syntax- directed translator Develop parser and code generator for translator Intermediate representation Syntax defini:on (BNF grammar) IR specifica:on 4

Syntax- Directed Transla:on Briefly introduced in the first lectures General technique to manipulate programs, based on context- free grammars Tightly bound with parsing Will be used for sta:c analysis (type checking) and (intermediate) code genera:on Several other uses: Genera:on of abstract syntax trees Evalua:on of expressions Implementa:on of Domain Specific Languages (see example on typeseing math formulas in the book) Partly supported by parser generators like Yacc 5

Syntax- Directed Defini:ons A syntax- directed defini/on (or a1ribute grammar) binds a set of seman/c rules to produc:ons Terminals and nonterminals have a1ributes holding values, which are set by the seman:c rules A depth- first (postorder) traversal algorithm traverses the parse tree execu:ng seman:c rules to assign akribute values Aler the traversal is complete the akributes contain the translated form of the input 6

Example: evalua:ng expressions with synthesized akributes 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 A Syntax- Directed Defini/on (SDD) or A1ribute Grammar 7

Example: An Annotated Parse Tree E.val = 16 L E.val = 14 E.val = 9 T.val = 5 T.val = 9 F.val = 5 F.val = 9 T.val = 2 F.val = 5 Productions L E n E E 1 + T E T T T 1 * F T F F ( E ) F digit 9 + 5 + 2 n 8

Annota:ng 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 9

Depth- First Traversals (Example) L print(16) E.val = 16 E.val = 14 E.val = 9 T.val = 5 T.val = 9 F.val = 5 F.val = 9 T.val = 2 F.val = 5 Semantic Rules 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 9 + 5 + 2 n 10

Akributes Each grammar symbol can have any number of akributes Akribute values typically represent Numbers (literal constants) Strings (literal constants) Memory loca:ons, such as a frame index of a local variable or func:on argument A data type for type checking of expressions Scoping informa:on for local declara:ons Intermediate program representa:ons 11

Synthesized vs. Inherited Akributes Given a produc:on A α then each seman:c rule is of the form b := f(c 1,c 2,,c k ) where f is a func:on and c i are akributes of A and α, and either b is a synthesized akribute of A b is an inherited akribute of one of the grammar symbols in α 12

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

S- Akributed Defini:ons A syntax- directed defini:on that uses synthesized akributes exclusively is called an S- a1ributed defini/on (or S- a1ributed grammar) A parse tree of an S- akributed defini:on can be annotated with a single bokom- up traversal [Yacc/Bison only support S- akributed defini:ons] 14

Example: genera:on of Abstract Syntax Trees A parse tree is called a concrete syntax tree An abstract syntax tree (AST) is defined by the compiler writer as a more convenient intermediate representa:on E E + T T T * id id id Concrete syntax tree + id * id id Abstract syntax tree 15

S- Akributed Defini:ons for Genera:ng Abstract Syntax Trees Production E E 1 + T E E 1 - T E T T T 1 * id T T 1 / id T id Semantic Rule E.nptr := mknode( +, E 1.nptr, T.nptr) E.nptr := mknode( -, E 1.nptr, T.nptr) E.nptr := T.nptr T.nptr := mknode( *, T 1.nptr, mkleaf(id, id.entry)) T.nptr := mknode( /, T 1.nptr, mkleaf(id, id.entry)) T.nptr := mkleaf(id, id.entry) 16

Genera:ng Abstract Syntax Trees Synthesize AST E.nptr E.nptr + T.nptr T.nptr T.nptr * id id id + id * id id 17

Example Akribute Grammar with Synthesized + Inherited Akributes Grammar generating declaration of typed variables The attributes add typing information to the symbol table via side effects 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 18

Evalua:on order of akributes In presence of inherited attributes, it is not obvious in which order the attributes can be evaluated Grammar generating declaration of typed variables The attributes add typing information to the symbol table Semantic Rule via side effects Production D T L T int T real L L 1, id L id 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 19

Evalua:on order of akributes In presence of inherited akributes, it is not obvious in what order the akributes should be evaluated Akributes of a nonterminal in a produc:on may depend in an arbitrary way on akributes of other symbols The evalua:on order must be consistent with such dependencies 20

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

Dependency Graphs with Cycles? Edges in the dependency graph determine the evalua:on order for akribute 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 22

Example Annotated Parse Tree D T L T int T real L L 1, id L id 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) D T.type = real L.in = real real L.in = real, id 3.entry L.in = real id 1.entry, id 2.entry 23

Example Annotated Parse Tree with Dependency Graph D T L T int T real L L 1, id L id 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) D T.type = real L.in = real real L.in = real, id 3.entry L.in = real id 1.entry, id 2.entry 24

Evalua:on 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 evalua:on order of the seman:c rules 25

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) 26

Evalua:on Methods Parse- tree methods determine an evalua:on order from a topological sort of the dependency graph constructed from the parse tree for each input Rule- base methods the evalua:on order is pre- determined from the seman:c rules Oblivious methods the evalua:on order is fixed and seman:c rules must be (re)wriken to support the evalua:on order (for example S- akributed defini:ons) 27

L- Akributed Defini:ons 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 Possible dependences of inherited attributes A.a X 1.x X 2.x 28

L- Akributed Defini:ons (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 (since it doesn t have any inherited attribute) 29

Syntax- Directed Transla:on Schemes A translation scheme is a CF grammar embedded with semantic actions rest + term { print( + ) } rest Embedded semantic action rest + term { print( + ) } rest 30

Syntax- Directed Transla:on Schemes Translation Schemes are an alternative notation for Syntax-Directed Definitions The semantic rules can be suitably embedded into productions SDT s can always be implemented by building the parse tree first, and then performing the actions in left-to-right depth-first order In several cases they can be implemented during parsing, without building the whole parse tree first 31

Posrix Transla:on Schemes If the grammar is LR (thus can be parsed bokom- up) and the SDD is S- akributed (synthesized akributes only), seman:c ac:ons can be placed at the end of the produc:ons They are executed when the body is reduced to the head These are called posiix SDTs 32

Example Transla:on Scheme for Posrix Nota:on expr expr + term expr expr - term expr term term 0 term 1 term 9 { print( + ) } { print( - ) } { print( 0 ) } { print( 1 ) } { print( 9 ) } 33

Example Transla:on Scheme (cont d) expr term 9 expr { print( + ) } expr + term { print( 2 ) } { print( - ) } - term 2 { print( 5 ) } 5 { print( 9 ) } Translates 9-5+2 into postfix 95-2+ 34

Implementa:on of Posrix SDTs Posrix SDTs can be implemented during LR parsing The ac:ons are executed when reduc:ons occur The akributes of grammar symbols can be put on the stack, together with the symbol or the state corresponding to it Since all akributes are synthesized, the akribute for the head can be computed when the reduc:on occurs, because all akributes of symbols in the body are already computed 35