Static and Dynamic Semantics

Similar documents
COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Chapter 4. Action Routines

Programming Languages

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Semantic Analysis. Role of Semantic Analysis

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation Part I

Chapter 4 :: Semantic Analysis

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

Semantic Analysis Attribute Grammars

COP5621 Exam 3 - Spring 2005

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Syntax-Directed Translation. Introduction

Lecture 14 Sections Mon, Mar 2, 2009

[Syntax Directed Translation] Bikash Balami

LECTURE 3. Compiler Phases

Syntax-Directed Translation

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

CSE302: Compiler Design

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

5. Syntax-Directed Definitions & Type Analysis

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

COP4020 Spring 2011 Midterm Exam

Abstract Syntax Tree

Syntax-Directed Translation Part II

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Syntax-Directed Translation

Summary: Semantic Analysis

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

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

Working of the Compilers

Chapter 4 - Semantic Analysis. June 2, 2015

Principles of Programming Languages

Syntax Directed Translation

Syntax-Directed Translation

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

Syntax-Directed Translation

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

CPS 506 Comparative Programming Languages. Syntax Specification

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

Error Handling Syntax-Directed Translation Recursive Descent Parsing

CS A331 Programming Language Concepts

A Simple Syntax-Directed Translator

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

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

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Principles of Programming Languages

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

Syntax Directed Translation

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

MIDTERM REVIEW. Lectures 1-15

CS 406: 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.

More On Syntax Directed Translation

Error Handling Syntax-Directed Translation Recursive Descent Parsing. Lecture 6

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

Syntax-Directed Translation. Lecture 14

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

Chapter 4. Abstract Syntax

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

Pioneering Compiler Design

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

22c:111 Programming Language Concepts. Fall Syntax III

CSE 12 Abstract Syntax Trees

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntax Analysis Part I

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

UNIT IV INTERMEDIATE CODE GENERATION

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

CST-402(T): Language Processors

Syntax Directed Translation

Recursive Descent Parsers

ECE251 Midterm practice questions, Fall 2010

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Semantic Analysis. Compiler Architecture

4. Semantic Processing and Attributed Grammars

Ambiguity and Errors Syntax-Directed Translation

Chapter 3 (part 3) Describing Syntax and Semantics

Syntactic Directed Translation

Question Points Score

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

Compilers. Compiler Construction Tutorial The Front-end

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

Project Compiler. CS031 TA Help Session November 28, 2011

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

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

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

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

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

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

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

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

Time : 1 Hour Max Marks : 30

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Transcription:

Copyright R.A. van Engelen, FSU Department of Computer Science, 2000 Semantic Analysis In this set of notes you will learn about: Static semantics Dynamic semantics Attribute grammars Abstract syntax trees Putting theory into practice: A Java interpreter of simple expressions A Java translator of simple expressions to Lisp Note: These slides cover Chapter 4 of the textbook upto and including Section 4.3 Static and Dynamic Semantics Syntax concerns the form of a valid program, while semantic concerns its meaning Static semantic rules are enforced by a compiler at compile time Implemented in semantic analysis phase of the compiler Context-free grammars are not powerful enough to descri certain rules, such as checking variable declaration with variable use Examples: Type checking; Identifiers are used in appropriate context; Check subroutine call arguments; Check labels Dynamic semantic rules are enforced by the compiler by generating code to perform the checks Examples: Array subscript values are within bounds; Arithmetic errors; Pointers are not dereferenced unless pointing to valid object; A variable is used but hasn t bee initialized Some languages (Euclid, Eiffel) allow programmers to ad explicit dynamic semantic checks in the form of assertion e.g. assert denominator not= 0 When a check fails at run time, an exception is raised

Attribute Grammars An attribute grammar links syntax with semantics Every grammar production has a semantic rule with actions to modify attributes of (non)terminals A (non)terminal may have a number of attributes to hold semantic information about the (non)terminal General form: <A> -> <B> <C> A.a :=...; B.a :=...; C.a :=... Used by a compiler to enforce static semantics and/or to produce an abstract syntax tree while parsing token stream Can also be used to build simple interpreters Example Attribute Grammar Example attribute grammar for evaluating simple expressions <E 1 > -> <E 2 > + <T> <E 1 > -> <E 2 > - <T> <E> -> <T> <T 1 > -> <T 2 > * <F> <T 1 > -> <T 2 > / <F> <T> -> <F> <F 1 > -> - <F 2 > <F> -> ( <E> ) <F> -> unsigned_int E 1.val := E 2.val + T.val E 1.val := E 2.val - T.val E.val := T.val T 1.val := T 2.val * F.val T 1.val := T 2.val / F.val T.val := F.val F 1.val := -F 2.val F.val := E.val F.val := unsigned_int.val The val attribute of a (non)terminal holds the subtotal value o the subexpression described by the (non)terminal Nonterminals are indexed in the attribute grammar (e.g. <T 1 >) distinghuish multiple occurrences of the nonterminal in a production

Decorated Parse Trees Parse trees are decorated with the attribute values Example decorated parse tree of (1+3)*2 showing val attribute values The val attribute of a node holds the subtotal value of the subexpression down the node Synthesized and Inherited Attributes Synthesized attributes hold values used by the parent node an flow upwards <E 1 > -> <E 2 > + <T> E 1.val := E 2.val + T.val Inherted attributes are defined by the parent node and flow downwards TT.st := T.val; E.val := TT.va TT 2.st := TT 1.st + T.val; Attribute Flow An attribute flow algorithm propagates attribute values throug

the parse tree by traversing the tree according to the defined-used dependencies between attributes Production Action Attribute flow TT.st := T.val <TT1 > -> + <T> <TT 2 > TT 2.st := TT 1.st + T.val <TT1 > -> + <T> <TT 2 > E.val := TT.val S- and L-Attributed Grammars A grammar is called S-attributed if all attributes are synthesiz A grammar is called L-attributed if the parse tree traversal is left-to-right and depth-first An essential grammar property for a one-pass compiler, because semantic rules can be applied directly during parsing and parse trees do not need to be kept in memory Example L-attributed grammar for top-down parsing and evaluation of simple expressions <TT 1 > -> - <T> <TT 2 > <T> -> <F> <FT> <FT 1 > -> * <F> <FT 2 > <FT 1 > -> / <F> <FT 2 > <FT> -> e <F 1 > -> - <F 2 > <F> -> ( <E> ) <F> -> unsigned_int TT.st := T.val; E.val := TT.val TT 2.st := TT 1.st + T.val; TT 2.st := TT 1.st - T.val; FT.st := F.val; T.val := FT.val FT 2.st := FT 1.st * F.val; FT 1.val := FT 2.val FT 2.st := FT 1.st / F.val; FT 1.val := FT 2.val FT.val := FT.st F 1.val := -F 2.val F.val := E.val F.val := unsigned_int.val

Example Decorated Parse Tree Fully decorated parse tree of (1+3)*2 Constructing Abstract Syntax Trees Example L-attributed grammar for constructing AST of simp expressions <TT 1 > -> - <T> <TT 2 > <T> -> <F> <FT> <FT 1 > -> * <F> <FT 2 > <FT 1 > -> / <F> <FT 2 > <FT> -> e <F 1 > -> - <F 2 > <F> -> ( <E> ) <F> -> unsigned_int TT.st := T.ptr; E.ptr := TT.ptr TT 2.st := mk_bin_op("+", TT 1.st, T.p TT 1.ptr := TT 2.ptr TT 2.st := mk_bin_op("-", TT 1.st, T.p TT 1.ptr := TT 2.ptr TT.ptr := TT.st FT.st := F.ptr; T.ptr := FT.ptr FT 2.st := mk_bin_op("*", FT 1.st, F.p FT 1.ptr := FT 2.ptr FT 2.st := mk_bin_op("/", FT 1.st, F.p FT 1.ptr := FT 2.ptr FT.ptr := FT.st F 1.ptr := mk_un_op("-", F 2.ptr) F.ptr := E.ptr F.ptr := make_leaf(unsigned_int.val mk_bin_op: constructs binary operator AST-node and returns pointer mk_un_op: constructs unary operator AST-node and returns po mk_leaf: constructs AST-leaf and returns pointer

Example Decorated Parse Tree With AST Decorated parse tree of (1+3)*2 with constructed AST Recursive Descent Parser for L-Attributed Grammar Productions for each nonterminal are implemented as a subroutine Subroutine returns synthesized attributes of the nonterminal Subroutine takes inherited attributes of the nonterminal as subroutine arguments TT.st := T.val; E.val := TT.val TT 2.st := TT 1.st + T.val; function E() Tval := T() TTst := Tval TTval := TT(TTst) return TTval function TT(TT1st) case (input_token()) of + : Tval := T() TT2st := TT1st + Tval TT2val := TT(TT2st) TT1val := TT2val otherwise: TT1val := TT1st return TT1val

Exercise: Write a recursive descent parser in Java to evaluate simple expressions. Answer: Exercise: Write a recursive descent parser in Java to construct an abstract syntax tree (AST) for simple expressions. Modify the parser to generate Lisp expressions from the AST. Answer: