Programming Languages

Similar documents
Semantic Analysis. Role of Semantic Analysis

Chapter 4 :: Semantic Analysis

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

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Chapter 4. Action Routines

Static and Dynamic Semantics

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

Abstract Syntax Trees Synthetic and Inherited Attributes

Chapter 4 - Semantic Analysis. June 2, 2015

COP5621 Exam 3 - Spring 2005

Syntax-Directed Translation. Introduction

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

Semantic Analysis Attribute Grammars

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

Summary: Semantic Analysis

Syntax-Directed Translation

COP4020 Spring 2011 Midterm Exam

Lecture 14 Sections Mon, Mar 2, 2009

CS A331 Programming Language Concepts

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Syntax-Directed Translation

Program Assignment 2 Due date: 10/20 12:30pm

CSE302: Compiler Design

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Syntax Directed Translation

A Simple Syntax-Directed Translator

5. Syntax-Directed Definitions & Type Analysis

Parsing II Top-down parsing. Comp 412

Syntax-Directed Translation Part II

UNIT IV INTERMEDIATE CODE GENERATION

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

[Syntax Directed Translation] Bikash Balami

LECTURE 3. Compiler Phases

Question Bank. 10CS63:Compiler Design

Syntax-Directed Translation Part I

Principles of Programming Languages

CS 314 Principles of Programming Languages

More On Syntax Directed Translation

Principles of Programming Languages

Syntax-Directed Translation

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

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

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Chapter 2: Syntax Directed Translation and YACC

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler Design Aug 1996

COS 320. Compiling Techniques

Syntax-Directed Translation

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

Semantic Analysis with Attribute Grammars Part 3

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

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

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

Chapter 3. Describing Syntax and Semantics

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

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

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

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

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

LR Parsing LALR Parser Generators

2068 (I) Attempt all questions.

Semantic Analysis. Lecture 9. February 7, 2018

Syntax-Directed Translation. Lecture 14

QUESTIONS RELATED TO UNIT I, II And III

Defining Languages GMU

Compiler Construction

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Error Handling Syntax-Directed Translation Recursive Descent Parsing

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

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

Recursive Descent Parsers

Context-Free Grammars

LECTURE 7. Lex and Intro to Parsing

Compiler Code Generation COMP360

Compiler Construction

CMPT 379 Compilers. Parse trees

COMPILER DESIGN. For COMPUTER SCIENCE

Describing Syntax and Semantics

3. Parsing. Oscar Nierstrasz

Semantic actions for declarations and expressions

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

Semantic actions for declarations and expressions

CSE 12 Abstract Syntax Trees

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

Syntax. In Text: Chapter 3

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

Compiler Design Overview. Compiler Design 1

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

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

Syntactic Directed Translation

Syntax-Directed Translation

Downloaded from Page 1. LR Parsing

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

List of Figures. About the Authors. Acknowledgments

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Transcription:

Programming Languages Tevfik Koşar Lecture - IX February 14 th, 2006 1 Roadmap Semantic Analysis Role of Semantic Analysis Static vs Dynamic Analysis Attribute Grammars Evaluating Attributes Decoration of Parse Trees Synthesized and Inherited Atributes 2 1

Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are semantic analysis (intermediate) code generation The principal job of the semantic analyzer is to enforce static semantic rules constructs a syntax tree (usually first) information gathered is needed by the code generator 3 Static vs Dynamic Semantics Static Semantics At compile time Type checking Dynamic Semantics At runtime Division by zero Out-of-bound indexing an array 4 2

Role of Semantic Analysis There is considerable variety in the extent to which parsing, semantic analysis, and intermediate code generation are interleaved One approach interleaves construction of a syntax tree with parsing (no explicit parse tree), and then follows with separate, sequential phases for semantic analysis and code generation 5 Attribute Grammars Both semantic analysis and (intermediate) code generation can be described in terms of annotation, or "decoration" of a parse or syntax tree ATTRIBUTE GRAMMARS provide a formal framework for decorating such a tree 6 3

Attribute Grammars We'll start with decoration of parse trees, then consider syntax trees Consider the following LR (bottom-up) grammar for arithmetic expressions made of constants, with precedence and associativity: 7 Attribute Grammars E E + T E E T E T T T * F T T / F T F F - F F (E) F const This says nothing about what the program MEANS 8 4

Attribute Grammars We can turn this into an attribute grammar as follows (similar to Figure 4.1): E E + T E 1.val = E 2.val + T.val E E T E 1.val = E 2.val - T.val E T E.val = T.val T T * F T 1.val = T 2.val * F.val T T / F T 1.val = T 2.val / F.val T F T.val = F.val F - F F 1.val = - F 2.val F (E) F.val = E.val F const F.val = C.val 9 Attribute Grammars The attribute grammar serves to define the semantics of the input program Attribute rules are best thought of as definitions, not assignments They are not necessarily meant to be evaluated at any particular time, or in any particular order, though they do define their left-hand side in terms of the right-hand side 10 5

Evaluating Attributes The process of evaluating attributes is called annotation, or DECORATION, of the parse tree [see Figure 4.2 for (1+3)*2] When a parse tree under this grammar is fully decorated, the value of the expression will be in the val attribute of the root The code fragments for the rules are called SEMANTIC FUNCTIONS Strictly speaking, they are cast as functions, e.g., E 1.val = sum (E 2.val, T.val), cf., Figure 4.1 11 Evaluating Attributes 12 6

Evaluating Attributes This is a very simple attribute grammar: Each symbol has at most one attribute the punctuation marks have no attributes These attributes are all so-called SYNTHESIZED attributes: They are calculated only from the attributes of things below them in the parse tree 13 Evaluating Attributes In general, we are allowed both synthesized and INHERITED attributes: Inherited attributes may depend on things above or to the side of them in the parse tree Tokens have only synthesized attributes, initialized by the scanner (name of an identifier, value of a constant, etc.). Inherited attributes of the start symbol constitute run-time parameters of the compiler 14 7

Evaluating Attributes The grammar above is called S-ATTRIBUTED because it uses only synthesized attributes Its ATTRIBUTE FLOW (attribute dependence graph) is purely bottom-up It is SLR(1), but not LL(1) An equivalent LL(1) grammar requires inherited attributes: 15 LL(1) Grammar Example 1: expr const expr_tail expr_tail - const expr_tail ε Create the parse tree for 9 4 3 and evaluate it. 16 8

LL(1) Grammar Example 2: E T TT TT 1 + T TT 2 TT 1 - T TT 1 TT ε T F FT FT 1 * F FT 2 FT 1 / F FT 2 FT ε F 1 - F 2 F ( E ) F const 17 Evaluating Attributes Example Attribute grammar : E T TT E.val = TT.val TT.st = T.val TT 1 + T TT 2 TT 1.v = TT 2.val TT 2.st = TT 1.st + T.val TT 1 - T TT 1 TT 1.val = TT 2.val TT 2.st = TT 1.st - T.val TT ε TT.val = TT.st T F FT T.val = FT.val FT.st = F.val 18 9

Evaluating Attributes Example Attribute grammar: FT 1 * F FT 2 FT 1.val = FT 2.val FT 2.st = FT 1.st * F.val FT 1 / F FT 2 FT 1.val = FT 2.val FT 2.st = FT 1.st / F.val FT ε FT.val = FT.st F 1 - F 2 F1.val = - F2.val F ( E ) F.val = E.val F const F.val = C.val 19 Evaluating Attributes Example 20 10

Evaluating Attributes Example Attribute grammar in Figure 4.3: This attribute grammar is a good bit messier than the first one, but it is still L-ATTRIBUTED, which means that the attributes can be evaluated in a single left-to-right pass over the input In fact, they can be evaluated during an LL parse Each synthetic attribute of a LHS symbol (by definition of synthetic) depends only on attributes of its RHS symbols 21 Evaluating Attributes Example Attribute grammar in Figure 4.3: Each inherited attribute of a RHS symbol (by definition of L-attributed) depends only on inherited attributes of the LHS symbol, or synthetic or inherited attributes of symbols to its left in the RHS L-attributed grammars are the most general class of attribute grammars that can be evaluated during an LL parse 22 11

Evaluating Attributes There are certain tasks, such as generation of code for short-circuit Boolean expression evaluation, that are easiest to express with non-l-attributed attribute grammars Because of the potential cost of complex traversal schemes, however, most real-world compilers insist that the grammar be L- attributed 23 12