COMPILER DESIGN. Syntax-Directed Translation. COMP 442/6421 Compiler Design. Department of Computer Science and Software Engineering

Similar documents
Chapter 4 :: Semantic Analysis

Chapter 4. Action Routines

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

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 406: Syntax Directed Translation

Parsing Scheme (+ (* 2 3) 1) * 1

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

Syntactic Analysis. Top-Down Parsing

Question Points Score

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Semantic Analysis. Role of Semantic Analysis

Summary: Semantic Analysis

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

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Lecture 9. February 7, 2018

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

Chapter 4 - Semantic Analysis. June 2, 2015

Programming Languages

Syntax-Directed Translation. Introduction

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

The role of semantic analysis in a compiler

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

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

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

QUESTION BANK CHAPTER 1 : OVERVIEW OF SYSTEM SOFTWARE. CHAPTER 2: Overview of Language Processors. CHAPTER 3: Assemblers

Chapter 3. Describing Syntax and Semantics ISBN

Recursive Descent Parsers

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

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

Lecture 14 Sections Mon, Mar 2, 2009

Lexical and Syntax Analysis (2)

COMPILER DESIGN. Intermediate representations Introduction to code generation. COMP 442/6421 Compiler Design

COP4020 Spring 2011 Midterm Exam

Static and Dynamic Semantics

Syntax-Directed Translation

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Alternatives for semantic processing

5. Semantic Analysis!

A Simple Syntax-Directed Translator

Abstract Syntax Trees Synthetic and Inherited Attributes

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Syntax-Directed Translation Part I

Parsing III. (Top-down parsing: recursive descent & LL(1) )

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

Unit 13. Compiler Design

Compiler principles, PS1

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

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

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

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

CSE302: Compiler Design

A simple syntax-directed


Table-Driven Top-Down Parsers

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

Introduction to Syntax Directed Translation and Top-Down Parsers

[Syntax Directed Translation] Bikash Balami

Syntax Analysis Top Down Parsing

The procedure attempts to "match" the right hand side of some production for a nonterminal.

Semantic Analysis and Type Checking

Syntax-Directed Translation

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

CSE 12 Abstract Syntax Trees

Software II: Principles of Programming Languages

Semantic actions for expressions

COMPILER DESIGN - QUICK GUIDE COMPILER DESIGN - OVERVIEW

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

Syntactic Directed Translation

Syntax-Directed Translation

CA Compiler Construction

Chapter 4. Lexical and Syntax Analysis

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Syntax-Directed Translation. Lecture 14

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Object-oriented Compiler Construction

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

Abstract Syntax Trees & Top-Down Parsing

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

Syntax Directed Translation

LANGUAGE PROCESSORS. Introduction to Language processor:

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

Compiler Code Generation COMP360

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

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

Syntax Analysis. Chapter 4


Transcription:

Concordia University 1 COMPILER DESIGN Syntax-Directed Translation

Syntax-directed translation: analysis and synthesis Translation process driven by the syntactic structure of the program, as generated by the parser. 2 In syntax-directed translation, the semantic analysis and translation steps of the compilation process is divided in two parts: analysis (syntactic, semantic) synthesis (translation and optimization) The semantic analysis becomes the link between analysis and synthesis: translation (synthesis) is conditional to positive semantic analysis. The syntax-directed translation process is inducing very strong coupling between the syntax analysis phase, the semantic checking phase, and the translation phase.

Syntax-directed translation: semantic actions, semantic values, attribute migration Semantic actions are integrated in the parsing process. 3 Semantic actions are functions whose goal is to accumulate and/or process semantic information about the program as the parsing is done. Different pieces of information are gathered during the parsing of different syntactical rules. These pieces of information, named semantic values, or semantic attributes are gathered and accumulated in semantic records until they can be processed by further semantic actions. Some of the information that must be gathered spans over the application of several syntactical rules. This raises the need to migrate this information across the parse tree, a process known as attribute migration.

Syntax-directed translation: semantic actions Some semantic actions (implemented as semantic routines) do the analysis phase by performing semantic aggregation and/or checking in productions that need such aggregation and/or checks, depending on the semantic rules defined by the language specification, e.g. type checking/aggregation. 4 = type(a=b+c) A = B + C; type(a) A + type(b+c) type(b) Semantic actions assemble information in order to validate and eventually generate a meaning (i.e. translation) for the program elements generated by the productions. They are the starting point for translation(synthesis). Thus, the semantic routines are the heart of the compiler. B C type(c)

Syntax-directed translation: relationship to parse trees, nodes, and parsing methods Conceptually, semantic actions are associated with parse tree nodes: Semantic actions at the leaves of the tree typically gather semantic information, either from the token the node represents, often with the help from a lookup in the symbol table (i.e. to get the type of an identifier). Semantic actions in intermediate nodes typically aggregate, validate, or use semantic information and pass the information up in the tree (i.e. perform attribute migration). 5 As parse trees are also related to grammar rules, so are semantic actions. Semantic actions are inserted in the right-hand sides of the grammar: In recursive-descent predictive parsing, they are represented by function calls inserted in the parsing functions, attribute migration is performed by using parameters passed and returned between parsing functions. In table-driven parsers, semantic action symbols are inserted in the right-hand-sides and pushed onto the stack. Popping a semantic action symbol from the stack triggers the corresponding semantic action. Attribute migration is made by way of the stack. Some parsers may have to rely on a separate semantic stack. Alternatively, recursive-descent predictive parsers can also use a semantic stack instead of local variables and parameter passing/return values.

Syntax-directed translation and attribute grammars Semantic routines can be formalized using attribute grammars. Attribute grammars augment ordinary context-free grammars with attributes that represent semantic properties such as type, value or correctness used in semantic analysis (checking) and code generation (translation). 6 It is useful to keep checking and translation facilities distinct in the semantic routines implementation. Semantic checking is machine-independent and code generation is not, so separating them gives more flexibility to the compiler (front/back end).

Attributes An attribute is a property of a programming language construct, including data type, value, memory location/size, translated code, etc. Implementation-wise, they are also called semantic records. 7 The process of computing the value of an attribute is called binding. Static binding concerns binding that can be done at compile-time, and dynamic binding happens at run-time, e.g. for polymorphism. In our project, we are concerned solely on static compile-time binding.

Attributes migration Static attribute binding is done by gathering, propagating, and aggregating attributes while traversing the parse tree. Attributes are gathered at tree leaves, propagated across tree nodes, and aggregated at some parent nodes when additional information is available. This can be done as the program is being parsed using syntax-directed translation. 8 Synthetized attributes : attributes gathered from a child in the syntax tree Inherited attributes : attributes gathered from a sibling in the syntax tree

Example: Semantic rules and attribute migration 9 E 1 id = E 2 E = : [E 2 : v: ] [(id: ) defvar] [id.val v] [E 1.val: v] Y=3*X+Z E(v 3*vx+vz : ) E 1 E 2 E 3 E * : [E 2 : v 2 : ] [E 3 : v 3 : ] [E 1.val: v 2 v 3 ] id(v Y : ) = E(v (3*vx)+vz : ) E 1 E 2 + E 3 E + : [E 2 : v 2 : ] [E 3 : v 3 : ] [E 1.val: v 2 +v 3 ] E(v 3*vx : ) + E(v Z : ) E id E id : [(id: ) defvar] [(E.val: ) id.val] E(3: ) * E(v X : ) id(v Z : ) E const E const : [const : v: ] [(E.val: ) v] const(3: ) id(v X : ) Here, semantic information only flows upwards in the tree. All aggregated semantic information is available on the same subtree.

Example 2: attribute migration Problems arise when rules are factorized: 10 E a+b*c E TE E +TE T FT T FT F id const F 1 id(v a : ) T 1 T 1 F 2 + E 1 T 2 T 2 E 2 id(v b : ) * F 3 T 3 id(v c : )

Example 2: attribute migration Solution: migrate attributes sideways, i.e. inherited attributes 11 E a+b*c E TE E +TE T FT T FT F id const F 1 id(v a : ) T 1 T 1 F 2 + E 1 T 2 T 2 E 2 id(v b : ) * F 3 T 3 id(v c : ) Here, left operands are available on a different subtree, so they have to be migrated sideways across different subtrees in order to be aggregated.

Attribute migration: implementation in recursive-descent predictive parser 12 Parse(){ semrec Es } lookahead = NextToken() if (E(Es);Match('$')) return(true); return(false); //semantic record created //before the call //passed as a reference //to parsing functions //that will compute its value semrec potentially represents any kind of semantic record. In this example, it represents semantic records that carry type information across the parsing of an expression. It could also be tree nodes that are created, migrated and grafted/adopted in order to construct an abstract syntax tree. It could also in fact be both, as the tree is created, information, such as type or symbol tables can be gathered as the parse is done or the tree is traversed.

Attribute migration: implementation in recursive-descent predictive parser 13 E(semrec &Es){ semrec Ts,E's if (lookahead is in [0,1,(]) if (T(Ts);E'(Ts,E's);) write(e->te') Es = E's return(true) return(false) return(false) } // E' inherits Ts from T // Synthetised attribute sent up Each parsing function potentially defines its own semantic records used locally the processing of its own subtree. Ts,E's are semantic records produced/used by the T() and E () functions and returned by them to the E() function.

Attribute migration: implementation in recursive-descent predictive parser 14 E'(semrec &Ti, type &E's){ semrec Ts,E'2s if (lookahead is in [+]) if (Match('+');T(Ts);E'(Ts,E'2s)) write(e'->te') E's = semcheckop(ti,e'2s) } return(true) return(false) if (lookahead is in [$,)] write(e'->epsilon) E's = Ti return(true) return(false) // E' inherits from T // Semantic check & synthetized // attribute sent up // Synth. attr. is inhertied // from T (sibling, not child) // and sent up Some semantic actions will do some semantic checking and/or semantic aggregation, such as a tree node adopting a child node, or inferring the type of an expression from two child operands.

Attribute migration: implementation in recursive-descent predictive parser 15 T(semrec &Ts){ semrec Fs, T's if (lookahead is in [0,1,(]) if (F(Fs);T'(Fs,T's);) write(t->ft') Ts = T's return(true) return(false) return(false) } // T' inherits Fs from F // Synthetized attribute sent up

Attribute migration: implementation in recursive-descent predictive parser 16 T'(semrec &Fi, type &T's){ semrec Fs, T'2s if (lookahead is in [*]) if (Match('*');F(Fs);T'(Fs,T'2s)) write(t'->*ft') T's = semcheckop(fi,t'2s) } return(true) return(false) if (lookahead is in [+,$,)] write(t'->epsilon) T's = Fi return(true) return(false) // T' inherits from F // Semantic check and // synthetized attribute sent up // Synthetized attribute is // inhertied from F sibling // and sent up the tree

Attribute migration: implementation in recursive-descent predictive parser 17 F(semrec &Fs){ semrec Es if (lookahead is in [id]) if (Match('id')) write(f->id) Fs = gettype(id.name,table) } return(true) return(false) if (lookahead is in [(]) if (Match('(');E(Es);Match(')')) write(f->(e)) Fs = Es return(true) return(false) return(false) // Gets the attribute ``type'' // from the symbol table and // sends it up the tree as Fs // Synthetized attribute from E // sent up the tree as attribute // of F Some semantic actions at the leaves will create new information to be propagated up.

Attribute migration: implementation in recursive-descent predictive parser 18 type semcheckop(type ti,type ts){ if (ti == ts) return(ti) return(typerror) } type gettype(name, table){ if (name is in table) return (type) return(typerror) }

Attribute migration: example 19 Es E a+b*c E s Ts T 1 Ti E 1 E s T s E s Fs F 1 Fi T 1 T s + Ts T 2 Ti E 2 E s Fs T s id(v a : ) Fs F 2 Fi T 2 T s Fs T s id(v b : ) * Fs F 3 Fi T 3 T s Fs id(v c : )