Syntax-Directed Translation. Introduction

Similar documents
Syntax-Directed Translation Part I

Syntax-Directed Translation

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

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation

Syntax-Directed Translation

Principles of Programming Languages

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

Syntax Directed Translation

Syntax-Directed Translation

Principles of Programming Languages

Abstract Syntax Tree

Syntax-Directed Translation Part II

Syntax-Directed Translation

Summary: Semantic Analysis

Lecture 14 Sections Mon, Mar 2, 2009

Syntax Directed Translation

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

Semantic Analysis Attribute Grammars

Chapter 4 :: Semantic Analysis

As we have seen, token attribute values are supplied via yylval, as in. More on Yacc s value stack

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation. Lecture 14

CSE302: Compiler Design

Programming Languages

COP5621 Exam 3 - Spring 2005

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

Chapter 4. Action Routines

Using an LALR(1) Parser Generator

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

5. Syntax-Directed Definitions & Type Analysis

Syntax Directed Translation

LR Parsing LALR Parser Generators

Chapter 2: Syntax Directed Translation and YACC

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntactic Directed Translation

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

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

CS 406: Syntax Directed Translation

Semantic Analysis. Role of Semantic Analysis

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

An Introduction to LEX and YACC. SYSC Programming Languages

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

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

Chapter 4 - Semantic Analysis. June 2, 2015

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

CPS 506 Comparative Programming Languages. Syntax Specification

More On Syntax Directed Translation

Compiler Lab. Introduction to tools Lex and Yacc

Semantic actions for expressions

Yacc: A Syntactic Analysers Generator

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

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Semantic actions for declarations and expressions

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

A Simple Syntax-Directed Translator

Static and Dynamic Semantics

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

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

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

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

Syntax and Grammars 1 / 21

Lexical and Syntax Analysis

APT Session 6: Compilers

Semantic Analysis. Compiler Architecture

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

Error Handling Syntax-Directed Translation Recursive Descent Parsing

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

Semantic Analysis with Attribute Grammars Part 3

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Last Time. What do we want? When do we want it? An AST. Now!

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

APT Session 7: Compilers

Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

COP4020 Spring 2011 Midterm Exam

Abstract Syntax Trees & Top-Down Parsing

Syntax Analysis Part IV

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

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

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

LR Parsing LALR Parser Generators

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

4. Semantic Processing and Attributed Grammars

Programming Project II

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

CS606- compiler instruction Solved MCQS From Midterm Papers

Semantic actions for declarations and expressions

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

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

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

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

Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

Wednesday, September 9, 15. Parsers

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

Semantic Analysis with Attribute Grammars Part 1

Transcription:

Syntax-Directed Translation Introduction Translation of languages guided by context-free grammars Attach attributes to the grammar symbols Values of the attributes are computed by semantic rules associated with the grammar productions Attribute may represent: Number, type, string, memory location, label, etc. Syntax directed definitions: Each production has a set of semantic rules associated with it Conceptually we parse the input token stream, build the parse tree and then traverse the tree as needed to evaluate the semantic rules. The rules may generate code, save information in the symbol table, issue error messages, etc. CS 5300 - SJAllan 2 1

Introduction In some cases we don t have to follow this outline literally We may evaluate the rules during parsing Synthesized attribute: The value of the attribute at a parent node can be found from the attributes of its children It can be evaluated by traversing the tree bottom-up Inherited attribute: The value of the attribute at a node can be found from the attributes at its parent node and/or its sibling nodes Can be evaluated by traversing the tree top-down S-attributed definition: A syntax-directed definition where all attributes are synthesized (the S stands for synthesized). CS 5300 - SJAllan 3 Example: Simple Desk Calculator Production L En E E 1 + T E T T T 1 * F T F F (E) F digit 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 CS 5300 - SJAllan 4 2

Synthesized Attributes Synthesized attributes are very common in practice Bison uses this type of attribute A syntax-directed definition that uses synthesized attributes exclusively is said to be an S-attributed definition A parse tree for an S-attributed definition can always be annotated by evaluating the semantic rules for the attributes at each node bottom-up CS 5300 - SJAllan 5 Example: Annotated parse tree for 3*5+4n L n E.Val = 19 E.Val = 15 + T.Val = 4 T.Val = 15 F.Val = 4 T.Val = 3 * F.Val = 5 digit.lexval = 4 F.Val = 3 digit.lexval = 5 digit.lexval = 3 CS 5300 - SJAllan 6 3

Inherited Attributes Inherited attributes are convenient for expressing the dependence of a programming language construct on the context in which it appears. CS 5300 - SJAllan 7 Example Using inherited attributes to parse a declaration and add type information to the symbol table. Production D TL 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) CS 5300 - SJAllan 8 4

Annotated Parse Tree for real, id1, id2, id3 D T.Type = real L.in = real.., real L.in = real id3 L.In = real, id2 id1 CS 5300 - SJAllan 9 Bottom-up Parsing If all attributes are synthesized then a bottom-up parser can evaluate the attributes while it parses Add attribute fields to the nonterminals on the stack Before each production apply the semantic rule associated with the production E.g. Assume the semantic rule associated with the A XYZ production is : A.a := f (X.x, Y.y, Z.z) CS 5300 - SJAllan 10 5

Bottom-up Parsing Before the reduction the parser stack looks like: After the reduction the parser stack looks like: X Y Z X.X Y.y Z.Z A A.A CS 5300 - SJAllan 11 Bottom-up Parsing This is the method that Bison uses for maintaining attributes CS 5300 - SJAllan 12 6

Bison Generated Parsers Bison maintains a semantic stack which, in tandem with the parser stack, helps Bison manipulate and determine the meaning of the program it compiles The values in the semantic stack represent the values or meanings of all the grammar symbols Exactly what constitutes values for the entries in the semantic stack depends heavily on the semantics of the language being compiled CS 5300 - SJAllan 13 Semantic Stack Manipulation of the semantic stack is triggered by the parser during reduce moves Values of symbols on the rhs of the production are popped off the semantic stack A value for the symbol on the lhs is determined from these values and the semantic rules of the language, and a new value is pushed back on the stack CS 5300 - SJAllan 14 7

Semantic Stack All symbols in the grammar have a value associated with them in the semantic stack even though some of these values may be NULL (indicating no information is needed about this symbol) Not all reductions require semantic actions CS 5300 - SJAllan 15 Semantic Rules Bison allows you to specify the semantic action associated with the symbol on the lhs of the production These actions may return a value and may utilize the values returned by previous actions The lexical analyzer can return values for tokens, if desired, through the use of the global variable yylval CS 5300 - SJAllan 16 8

Semantic Rules An action is specified by writing arbitrary C/C++ statements enclosed in {} Exp : Exp PLUS Exp { $$ = binaryop( $1, A_Plus, $3); } To return a value for the symbol Exp on the lhs, a value is assigned to $$ To refer to values of symbols on the rhs, the variables $1, $2,, are used These refer to the values returned by the components of the rhs of the production reading from right to left CS 5300 - SJAllan 17 Semantic Actions If no action is specified after a particular production, the default action is performed {$$ = $1;} CS 5300 - SJAllan 18 9

Union The values on the semantic stack do not have to be of a single type You can define the type of the value using the %union statement in Bison Bison must know which type to associate with each symbol in the grammar This is done using the %type statement Beware, once you starting using %type, you are forced to type all symbols even though you may not be using them You are required to use %type statements in your project CS 5300 - SJAllan 19 %union and %type %union { int int_val; char *name_ptr; CONS *cons_ptr; ID *id_ptr; TYPE *ty_ptr; EXPR *exp_ptr; } %type <int_val> INTCONSTSY IfHead %type <id_ptr> IdList FieldList CS 5300 - SJAllan 20 10