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

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

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

Syntax Directed Translation

Syntax Directed Translation

Syntax-Directed Translation

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Syntax-Directed Translation Part II

Semantic Analysis Attribute Grammars

Alternatives for semantic processing

Principles of Programming Languages

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

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

Syntax-Directed Translation Part I

5. Semantic Analysis!

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

[Syntax Directed Translation] Bikash Balami

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Syntax-Directed Translation

Syntax-Directed Translation. Introduction

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Context-sensitive Analysis

Syntax-Directed Translation

Syntactic Directed Translation

Context-sensitive Analysis

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Syntax-Directed Translation

Syntactic Directed Translation

A Simple Syntax-Directed Translator

Chapter 4 - Semantic Analysis. June 2, 2015

Principles of Programming Languages

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

Syntax-Directed Translation. Lecture 14

CS 406/534 Compiler Construction LR(1) Parsing and CSA

Chapter 4 :: Semantic Analysis

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

CSE302: Compiler Design

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

CA Compiler Construction

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Summary: Semantic Analysis

Chapter 4. Action Routines

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

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

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

CS 406: Syntax Directed Translation

Lecture 14 Sections Mon, Mar 2, 2009

intermediate code generator syntax tree token stream intermediate representation parser checker tree

CSCI Compiler Design

Abstract Syntax Tree

COP5621 Exam 3 - Spring 2005

How do LL(1) Parsers Build Syntax Trees?

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

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

Introduction to Parsing. Lecture 8

CSCI312 Principles of Programming Languages

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Semantic Analysis with Attribute Grammars Part 1

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

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

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

Introduction to Syntax Directed Translation and Top-Down Parsers

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

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

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

CS 4120 Introduction to Compilers

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

Context-sensitive Analysis Part II Chapter 4 (up to Section 4.3)

Abstract Syntax Trees & Top-Down Parsing

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

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

Context-Free Grammars

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CS 314 Principles of Programming Languages

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

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Top down vs. bottom up parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

4. Semantic Processing and Attributed Grammars

Yacc: A Syntactic Analysers Generator

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Project Compiler. CS031 TA Help Session November 28, 2011

Syntactic Analysis. Top-Down Parsing

CS 406/534 Compiler Construction Parsing Part I

More On Syntax Directed Translation

Semantic actions for declarations and expressions

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

Semantic actions for declarations and expressions. Monday, September 28, 15

CS606- compiler instruction Solved MCQS From Midterm Papers

Context-sensitive Analysis Part II

CSCI Compiler Design

Wednesday, September 9, 15. Parsers

Compiler Construction

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

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

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

Transcription:

Syntax-Directed Translation CS3300 - Compiler Design Syntax Directed Translation V. Krishna Nandivada IIT Madras Attach rules or program fragments to productions in a grammar. Syntax directed definition (SDD) E 1 E 2 + T E 1.code = E 2.code T.code + Syntax directed translation Scheme (SDT) E E + T {print + } // semantic action F id {print id.val} V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 2 / 29 SDD and SDT scheme Example: SDD vs SDT scheme infix to postfix trans SDD: Specifies the values of attributes by associating semantic rules with the productions. SDT scheme: embeds program fragments (also called semantic actions) within production bodies. The position of the action defines the order in which the action is executed (in the middle of production or end). SDD is easier to read; easy for specification. SDT scheme can be more efficient; easy for implementation. SDTScheme E E + T {print + } E E T {print } E T T 0 {print 0 } T 1 {print 1 } T 9 {print 9 } SDD E E + T E.code = E.code T.code + E E T E.code = E.code T.code E T E.code = T.code T 0 T.code = 0 T 1 T.code = 1 T 9 T.code = 9 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 3 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 4 / 29

Syntax directed translation - overview Syntax directed definition 1 Construct a parse tree 2 Compute the values of the attributes at the nodes of the tree by visiting the tree Key: We don t need to build a parse tree all the time. Translation can be done during parsing. class of SDTs called L-attributed translations. class of SDTs called S-attributed translations. SDD is a CFG along with attributes and rules. An attribute is associated with grammar symbols (attribute grammar). Rules are are associated with productions. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 5 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 6 / 29 Attributes Specifying the actions: Attribute grammars Attribute is any quantity associated with a programming construct. Example: data types, line numbers, instruction details Two kinds of attributes: for a non-terminal A, at a parse tree node N A synthesized attribute: defined by a semantic rule associated with the production at N. defined only in terms of attribute values at the children of N and at N itself. An inherited attribute: defined by a semantic rule associated with the parent production of N. Idea: attribute the syntax tree can add attributes (fields) to each node specify equations to define values can use attributes from parent and children Example: to ensure that constants are immutable: add type and class attributes to expression nodes rules for production on := that 1 check that LHS.class is variable 2 check that LHS.type and RHS.type are consistent or conform (unique) defined only in terms of attribute values at the parent of N siblings of N and at N itself. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 7 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 8 / 29

Attribute grammars Example To formalize such systems Knuth introduced attribute grammars: grammar-based specification of tree attributes value assignments associated with productions each attribute uniquely, locally defined label identical terms uniquely Can specify context-sensitive actions with attribute grammars PRODUCTION D T L T int T real L L 1, id L id SEMANTIC RULES 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) V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 9 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 10 / 29 Example: Evaluate signed binary numbers Example (continued) The attributed parse tree for -101: PRODUCTION SEMANTIC RULES NUM SIGN LIST LIST.pos := 0 if SIGN.neg NUM.val := -LIST.val else NUM.val := LIST.val SIGN + SIGN.neg := false SIGN - SIGN.neg := true LIST BIT BIT.pos := LIST.pos LIST.val := BIT.val LIST LIST 1 BIT LIST 1.pos := LIST.pos + 1 BIT.pos := LIST.pos LIST.val := LIST 1.val + BIT.val BIT 0 BIT.val := 0 BIT 1 BIT.val := 2 BIT.pos SIGN neg: T NUM LIST BIT val: -5 pos: 2 pos: 2 LIST pos: 1 LIST BIT pos: 0 val: 5 pos: 1 val: 0 BIT pos: 0 val: 1 val and neg are synthesized attributes pos is an inherited attribute - 1 0 1 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 11 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 12 / 29

Dependences between attributes The attribute dependency graph values are computed from constants & other attributes synthesized attribute value computed from children inherited attribute value computed from siblings & parent key notion: induced dependency graph nodes represent attributes edges represent flow of values graph is specific to parse tree size is related to parse tree s size can be built alongside parse tree The dependency graph must be acyclic Evaluation order: topological sort the dependency graph to order attributes using this order, evaluate the rules The order depends on both the grammar and the input string V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 13 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 14 / 29 Example (continued) Example: A topological order The attribute dependency graph: SIGN neg: T NUM LIST2 BIT0 val: -5 pos: 2 pos: 2 LIST1 pos: 1 LIST0 BIT1 0 pos: 0 val: 5 pos: 1 val: 0 BIT2 pos: 0 val: 1 1 SIGN.neg 2 LIST 0.pos 3 LIST 1.pos 4 LIST 2.pos 5 BIT 0.pos 6 BIT 1.pos 7 BIT 2.pos 8 BIT 0.val 9 LIST 2.val 10 BIT 1.val 11 LIST 1.val 12 BIT 2.val 13 LIST 0.val 14 NUM.val Evaluating in this order yields NUM.val: -5-1 0 1 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 15 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 16 / 29

Evaluation strategies Avoiding cycles Parse-tree methods (dynamic) 1 build the parse tree 2 build the dependency graph 3 topological sort the graph 4 evaluate it (cyclic graph fails) Hard to tell, for a given grammar, whether there exists any parse tree whoe depdency graphs have cycles. Focus on classes of SDD s that guarantee an evaluation order do not permit dependency graphs with cycles. L-attributed class of SDTs called L-attributed translations. S-attributed class of SDTs called S-attributed translations. What if there are cycles? V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 17 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 18 / 29 Top-down (LL) on-the-fly one-pass evaluation Bottom-up (LR) on-the-fly one-pass evaluation L-attributed grammar: Informally dependency-graph edges may go from left to right, not other way around. given production A X 1 X 2 X n inherited attributes of X j depend only on: 1 inherited attributes of A 2 arbitrary attributes of X 1,X 2, X j 1 synthesized attributes of A depend only on its inherited attributes and arbitrary RHS attributes synthesized attributes of an action depends only on its inherited attributes i.e., evaluation order: Inh(A), Inh(X 1 ), Syn(X 1 ),..., Inh(X n ), Syn(X n ), Syn(A) This is precisely the order of evaluation for an LL parser S-attributed grammar: L-attributed only synthesized attributes for non-terminals actions at far right of a RHS Can evaluate S-attributed in one bottom-up (LR) pass. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 19 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 20 / 29

Evaluate S-attributed grammar in bottom-up parsing Inherited Vs Synthesised attributes Evaluate it in any bottum-up order of the nodes in the parse tree. (One option:) Apply postorder to the root of the parse tree: void postorder (N) { for (each child C of N) do postorder(c); evaluate the attributes associated with N; done } post order traversal of the parse tree corresponds to the exact order in which the bottom-up parsing builds the parse tree. Thus, we can evaluate S-attributed in one bottom-up (LR) pass. Synthesized attributes are limited Inherited attributes (are good): derive values from constants, parents, siblings used to express context (context-sensitive checking) inherited attributes are more natural We want to use both kinds of attributes can always rewrite L-attributed LL grammars (using markers and copying) to avoid inherited attribute problems with LR Self reading (if interested) Dragon book Section 5.5.4. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 21 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 22 / 29 LL parsers and actions LL parsers and actions How does an LL parser handle (aka - execute) actions? Expand productions before scanning RHS symbols, so: push actions onto parse stack like other grammar symbols pop and perform action when it comes to top of parse stack push EOF push Start Symbol token next token() repeat pop X if X is a terminal or EOF then if X = token then token next token() else error() else if X is an action perform X else / X is a non-terminal / if M[X,token] = X Y 1 Y 2 Y k then push Y k,y k 1,,Y 1 else error() until X = EOF V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 23 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 24 / 29

LR parsers and action symbols Action-controlled semantic stacks What about LR parsers? Scan entire RHS before applying production, so: cannot perform actions until entire RHS scanned can only place actions at very end of RHS of production introduce new marker non-terminals and corresponding productions to get around this restriction becomes A w action β A Mβ M w action Approach: stack is managed explicitly by action routines actions take arguments from top of stack actions place results back on stack Advantages: actions can directly access entries in stack without popping (efficient) Disadvantages: implementation is exposed action routines must include explicit code to manage stack (or use stack abstract data type). yacc, bison, CUP do this automatically V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 25 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 26 / 29 LR parser-controlled semantic stacks LL parser-controlled semantic stacks Idea: let parser manage the semantic stack LR parser-controlled semantic stacks: parse stack contains already parsed symbols maintain semantic values in parallel with their symbols add space in parse stack or parallel stack for semantic values every matched grammar symbol has semantic value pop semantic values along with symbols LR parsers have a very nice fit with semantic processing Problems: parse stack contains predicted symbols, not yet matched often need semantic value after its corresponding symbol is popped Solution: use separate semantic stack push entries on semantic stack along with their symbols on completion of production, pop its RHS s semantic values V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 27 / 29 V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 28 / 29

Attribute Grammars Advantages clean formalism automatic generation of evaluator high-level specification Disadvantages evaluation strategy determines efficiency increased space requirements parse tree evaluators need dependency graph results distributed over tree circularity testing Intel s 80286 Pascal compiler used an attribute grammar evaluator to perform context-sensitive analysis. Historically, attribute grammar evaluators have been deemed too large and expensive for commercial-quality compilers. V.Krishna Nandivada (IIT Madras) CS3300 - Aug 2017 29 / 29