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

Similar documents
Syntax-Directed Translation

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

Context-sensitive Analysis

Syntax-Directed Translation Part I

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

Context-sensitive Analysis

Context-sensitive Analysis Part II

Syntax Directed Translation

Syntax Directed Translation

Semantic Analysis with Attribute Grammars Part 1

[Syntax Directed Translation] Bikash Balami

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Static and Dynamic Semantics

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Syntax-Directed Translation

Attribute Grammars. Attribute Grammars

Lecture 14 Sections Mon, Mar 2, 2009

COP4020 Spring 2011 Midterm Exam

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

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

Syntax-Directed Translation. Introduction

Syntactic Directed Translation

Summary: Semantic Analysis

CS 406: Syntax Directed Translation

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

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

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Abstract Syntax Trees Synthetic and Inherited Attributes

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Ambiguity and Errors Syntax-Directed Translation

Syntactic Directed Translation

Chapter 4. Action Routines

Syntax-Directed Translation Part II

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

Syntax-Directed Translation

Semantic Analysis Attribute Grammars

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

Syntax Directed Translation

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

Principles of Programming Languages

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Abstract Syntax Trees & Top-Down Parsing

Chapter 4 - Semantic Analysis. June 2, 2015

Syntax-Directed Translation

Error Handling Syntax-Directed Translation Recursive Descent Parsing

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

Principles of Programming Languages

COP5621 Exam 3 - Spring 2005

5. Semantic Analysis!

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

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

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

More On Syntax Directed Translation

CS606- compiler instruction Solved MCQS From Midterm Papers

4. Semantic Processing and Attributed Grammars

Time : 1 Hour Max Marks : 30

2. Reachability in garbage collection is just an approximation of garbage.

Today's Topics. CISC 458 Winter J.R. Cordy

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

An Introduction To Attribute Grammars

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Run-Time Environments

Compiler Construction

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

Chapter 3 (part 3) Describing Syntax and Semantics

CS143 Midterm Sample Solution Fall 2010

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

2068 (I) Attempt all questions.

Compiler construction

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

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

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

Building Compilers with Phoenix

COP PRINCIPLES OF PROGRAMMING LANGUAGES NOTES ON ATTRIBUTE GRAMMARS.

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

LECTURE 3. Compiler Phases

such internal data dependencies can be formally specied. A possible approach to specify

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

Compilers and computer architecture: A realistic compiler to MIPS

COSE312: Compilers. Lecture 1 Overview of Compilers

Semantic Analysis and Type Checking

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

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

CMPT 379 Compilers. Parse trees

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 10 Implementing Subprograms

The role of semantic analysis in a compiler

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Compiler Techniques MN1 The nano-c Language

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Syntax-Directed Translation. Lecture 14

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

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

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

Single-pass Static Semantic Check for Efficient Translation in YAPL

CSCI 565: Compiler Design and Implementation Spring 2006

Transcription:

Semantic analysis

Semantic analysis Semantic analysis checks for the correctness of contextual dependences: { nds correspondence between declarations and usage of identiers, { performs type checking/inference, {... Syntax tree is decorated with typing- and other context dependent information.

Semantic analysis Semantic analysis checks restrictions imposed by a static semantics of the language. Sometimes it is possible to expess semantic properties by context-free grammars, but usually this puts heavy restrictions to the language and/or complicates the grammar. Example { simple typed expressions: IntExp! int j intv ar j IntExp + IntExp oolexp! true j f alse j boolv ar j IntExp IntExp j not oolexp j oolexp & oolexp

Semantic analysis At rst glance, the grammar looks reasonable, but: { the grammar has two dierent (lexical) classes for variables; { additional types require new classes of variables; { most languages do not put restrictions to variable names based on their types; { moreover, usually one is allowed to use the same variable name for variables of dierent types in dierent context.

Attribute grammars are generalization of context-free grammars, where: { each grammar symbol has an associated set of attributes; { each production rule has a set of attribute evaluation rules (or semantic rules). The goal is to nd an evaluation of attributes which is consistent with the given semantic rules.

Example: Productions Semantic rules N! S :pos := 0 N:val := S:neg :val S! + S:neg := 1 S! S:neg := 1! 1 1 :pos := :pos + 1 :pos := :pos :val := 1 :val + :val! :pos := :pos :val := :val! 0 :val := 0! 1 :val := 2 :pos

Example: N S neg = 1 val = 0 val = 1 1 0 1

Semantic rules associated with a production A! are in the form y = f(x 1 ; : : : ; xn), where y and xi are attributes associated with symbols in the production, and f is a function. There are two kinds of attributes: { synthesized attributes: y is an attribute associated with the non-terminal A; { inherited attributes: y is an attribute associated with some symbol in. Synthesized attributes depend only attribute values of the subtrees. Inherited attributes may depend from the values of parent node and siblings.

Example: Productions Semantic rules N! S :pos := 0 N:val := S:neg :val S! + S:neg := 1 S! S:neg := 1! 1 1 :pos := :pos + 1 :pos := :pos :val := 1 :val + :val! :pos := :pos :val := :val! 0 :val := 0! 1 :val := 2 :pos synthesized attributes inherited attributes

An attribute a depends from b if the evaluation of a requires the value of b. Dependencies between attributes dene a dependency graph: { an directed graph, where edges show the dependencies between attributes; { describes the data ow during the attribute evaluation. Synthesized attributes have edges pointing upwards. Inherited attributes have edges pointing downwards and/or sidewise.

Example: N dependency graph S neg = 1 synthesized attributes inherited attributes val = 0 val = 1 1 0 1

Example: N dependency graph S neg = 1 synthesized attributes inherited attributes val = 0 val = 1 1 0 1

Example: N dependency graph S neg = 1 synthesized attributes inherited attributes val = 0 val = 1 1 0 1

Example: N dependency graph S neg = 1 synthesized attributes inherited attributes val = 0 val = 1 1 0 1

Example: N dependency graph S neg = 1 synthesized attributes inherited attributes val = 0 val = 1 1 0 1

Topological sorting of a directed acyclic graph is a process of ndig a linear ordering of its nodes, st., each node comes before all nodes to which it has outbound edges. Topological sorting of the dependency graph gives a valid evaluation ordering for attributes. N! In the case of cyclic dependency graphs a valid ordering may not exist.

Example: N S neg = 1 val = 0 val = 1 1 0 1

Example: 12 N 11 2 S neg = 1 8 5 4 7 10 pos 1 val = 0 val = 1 1 3 1 6 0 9 1

S-attribute grammar is an AG where all attributes are synthesized. S-attribute grammars interact well with R(k)-parsers since the evaluation of attributes is bottom-up. The values of attributes can be kept together with the associated symbol in the stack. efore reducution by production A!, attributes corresponding to symbols of are available in top of the stack. Hence, all the information for evaluating synthesized attributes of A are available, and these can be computed during reduction.

-attribute grammar is an AG where for all productions A! X 1 X 2 : : : Xn inherited attributes of symbol Xi (1 i n) depend only from inherited attributes of A and from attributes of symbols Xj (j < i). N! Each S-attribute grammar is also a -attribute grammar. -attribute grammars support the evaluation of attributes in depth-rst left-to-right order. Interacts well with (k) parsers (both table driven and recursive decent).