Semantic Analysis. CSCI 3136 Principles of Programming Languages. Faculty of Computer Science Dalhousie University. Winter Reading: Chapter 4

Similar documents
SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/28

CS 314 Principles of Programming Languages

CS241 Week 6 Tutorial Solutions

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

FABER Formal Languages, Automata and Models of Computation

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

Bottom-Up Parsing. Canonical Collection of LR(0) items. Part II

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER MACHINES AND THEIR LANGUAGES ANSWERS

CSE 401 Compilers. Today s Agenda

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Finite State Automata and Determinisation

Parsing and Pattern Recognition

System Validation (IN4387) November 2, 2012, 14:00-17:00

Parse trees, ambiguity, and Chomsky normal form

CS 330 Formal Methods and Models

2.4 Theoretical Foundations

CS 275 Automata and Formal Language Theory

Normal Forms for Context-free Grammars

Technische Universität München Winter term 2009/10 I7 Prof. J. Esparza / J. Křetínský / M. Luttenberger 11. Februar Solution

Nondeterministic Automata vs Deterministic Automata

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Formal Languages Simplifications of CFGs

CS 573 Automata Theory and Formal Languages

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis

Nondeterministic Finite Automata

arxiv: v2 [cs.lo] 24 Feb 2017

Engr354: Digital Logic Circuits

SWEN 224 Formal Foundations of Programming WITH ANSWERS

Prefix-Free Regular-Expression Matching

Linear Algebra Introduction

Homework 3 Solutions

Pre-Lie algebras, rooted trees and related algebraic structures

Memory Minimization for Tensor Contractions using Integer Linear Programming.

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

CS 275 Automata and Formal Language Theory

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-378: Computer Hardware Design Winter Notes - Unit 1

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-2700: Digital Logic Design Fall Notes - Unit 1

Matrices SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics (c) 1. Definition of a Matrix

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

BİL 354 Veritabanı Sistemleri. Relational Algebra (İlişkisel Cebir)

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

TIME AND STATE IN DISTRIBUTED SYSTEMS

Convert the NFA into DFA

@#? Text Search ] { "!" Nondeterministic Finite Automata. Transformation NFA to DFA and Simulation of NFA. Text Search Using Automata

Non-deterministic Finite Automata

ILLUSTRATING THE EXTENSION OF A SPECIAL PROPERTY OF CUBIC POLYNOMIALS TO NTH DEGREE POLYNOMIALS

Petri Nets. Rebecca Albrecht. Seminar: Automata Theory Chair of Software Engeneering

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

CSCI565 - Compiler Design

= state, a = reading and q j

Test Generation from Timed Input Output Automata

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Alpha Algorithm: A Process Discovery Algorithm

Refined interfaces for compositional verification

XML and Databases. Outline. 1. Top-Down Evaluation of Simple Paths. 1. Top-Down Evaluation of Simple Paths. 1. Top-Down Evaluation of Simple Paths

Digital Circuit Design

5. Every rational number have either terminating or repeating (recurring) decimal representation.

Chapter 3. Vector Spaces. 3.1 Images and Image Arithmetic

Cyclic Inputs. Surface Generation (Part II) Ambiguity-preserving Generation. Ambiguity-preserving Generation. Ambiguity-preserving Generation

AVL Trees. D Oisín Kidney. August 2, 2018

Languages and Computation (G52LAC) Lecture notes Spring 2018

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

NFAs continued, Closure Properties of Regular Languages

CS 330 Formal Methods and Models

CS 330 Formal Methods and Models

CHAPTER 1 Regular Languages. Contents

Thoery of Automata CS402

3 Regular expressions

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

2. Lexical Analysis. Oscar Nierstrasz

For a, b, c, d positive if a b and. ac bd. Reciprocal relations for a and b positive. If a > b then a ab > b. then

Some Theory of Computation Exercises Week 1

NON-DETERMINISTIC FSA

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

NFAs continued, Closure Properties of Regular Languages

Maintaining Mathematical Proficiency

XML and Databases. Exam Preperation Discuss Answers to last year s exam. Sebastian Maneth NICTA and UNSW

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Vectors. a Write down the vector AB as a column vector ( x y ). A (3, 2) x point C such that BC = 3. . Go to a OA = a

Review for the Midterm

Unit 4. Combinational Circuits

Regular languages refresher

Chapter 4 State-Space Planning

, g. Exercise 1. Generator polynomials of a convolutional code, given in binary form, are g. Solution 1.

Greedy regular expression matching

q 2 in δ 2. [4: 1 mark q 1 in δ 1 and q 2 and (q 1, q 2 ) (q 1, q 2) whenever q 1 for each component] (b) The resulting DFA:

Review: The Riemann Integral Review: The definition of R b

CS 275 Automata and Formal Language Theory

CHAPTER 1 PROGRAM OF MATRICES

Chapter 8 Roots and Radicals

A Functorial Query Language

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Lecture 09: Myhill-Nerode Theorem

A Lower Bound for the Length of a Partial Transversal in a Latin Square, Revised Version

ENGR 3861 Digital Logic Boolean Algebra. Fall 2007

arxiv: v1 [cs.fl] 13 Oct 2010

Unfoldings of Networks of Timed Automata

Lecture Notes No. 10

Transcription:

Semnti nlysis SI 16 Priniples of Progrmming Lnguges Fulty of omputer Siene Dlhousie University Winter 2012 Reding: hpter 4

Motivtion Soure progrm (hrter strem) Snner (lexil nlysis) Front end Prse tree Prser (syntti nlysis) Token strem k end Modified intermedite form Modified trget lnguge Semnti nlysis nd ode genertion Mhine-independent ode improvement Trget ode genertion Mhine-speifi ode improvement strt syntx tree or other intermedite form Trget lnguge (e.g., ssemly) Symol tle Semnti nlysis

Syntx nd Semntis Syntx Desries form of vlid progrm n e desried y ontext-free grmmr Semntis Desries mening of progrm nnot e e desried y ontext-free grmmr Some onstrints tht my pper syntti re enfored y semnti nlysis: E.g., use of identifier only fter its delrtion Semnti nlysis

Semnti nlysis Enfores semnti rules uilds intermedite representtion (e.g., strt syntx tree) Fills symol tle Psses results to intermedite ode genertor Two pprohes Interleved with syntti nlysis s seprte phse Forml mehnism: ttriutes grmmrs Semnti nlysis

Enforing Semnti Rules Stti semnti rules Enfored y ompiler t ompile time Exmple: Do not use undelred vrile Dynmi semnti rules ompiler genertes ode for enforement t run time Exmples: division y zero, rry index out of ounds Some ompilers llow these heks to e disled Semnti nlysis

ttriute Grmmrs n ttriute grmmr is n ugmented ontext-free grmmr: Eh symol in prodution hs numer of ttriutes. Eh prodution is ugmented with semnti rules tht opy ttriute vlues etween symols, Evlute ttriute vlues using semnti funtions, Enfore onstrints on ttriute vlues. Semnti nlysis

ttriute Grmmrs: Exmple E E + T E E - T E T T T * F T T / F T F F -F F ( E ) F onst Semnti nlysis

ttriute Grmmrs: Exmple E E + T E E - T E T T T * F T T / F T F F -F F ( E ) 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 onst F onst E 1.vl := sum(e 2.vl, T.vl) E 1.vl := differene(e 2.vl, T.vl) E.vl := T.vl T 1.vl := produt(t 2.vl, F.vl) T 1.vl := quotient(t 2.vl, F.vl) T.vl := F.vl F 1.vl := negte(f 2.vl) F.vl := E.vl F.vl := onst.vl Semnti nlysis

Synthesized ttriutes (ottom-up) The lnguge { n n n n 1} = {,,,... } is not ontext-free ut n e reognized y n ttriute grmmr. Synthesized ttriutes: ttriutes of LHS re omputed from ttriutes of RHS. S ondition:.ount =.ount =.ount.ount := 1 1 2 1.ount := 2.ount + 1.ount := 1 1 2 1.ount := 2.ount + 1.ount := 1 1 2 1.ount := 2.ount + 1 Semnti nlysis

Synthesized ttriutes: Prse Tree Deortion (1) Input: Prse tree S Semnti nlysis

Synthesized ttriutes: Prse Tree Deortion (1) Input: Prse tree S = = 2 2 2 1 1 1 Semnti nlysis

Synthesized ttriutes: Prse Tree Deortion (2) Input: Prse tree S Semnti nlysis

Synthesized ttriutes: Prse Tree Deortion (2) Input: Prse tree S 2 2 2 1 2 1 1 Semnti nlysis

Inherited ttriutes (Top-Down) Inherited ttriutes: ttriutes flow from left to right (from LHS to RHS nd from symols of RHS to symols of RHS further to the right). S.inhount :=.ount;.inhount :=.ount.ount := 1 1 2 1.ount := 2.ount + 1 ondition:.inhount = 1 1 2 2.inhount := 1.inhount 1 ondition:.inhount := 1 1 2 2.inhount := 1.inhount 1 Semnti nlysis

Inherited ttriutes: Prse Tree Deortion (1) Input: Prse tree S Semnti nlysis

Inherited ttriutes: Prse Tree Deortion (1) Input: Prse tree S 2 2 2 1 1 = 1 1 = 1 Semnti nlysis

Inherited ttriutes: Prse Tree Deortion (2) Input: Prse tree S Semnti nlysis

Inherited ttriutes: Prse Tree Deortion (2) Input: Prse tree S 2 2 1 2 1 1 = 1 Semnti nlysis

ttriute Flow nnottion or deortion of prse tree Proess of evluting ttriutes Synthesized ttriutes ttriutes of LHS of prodution re omputed from ttriutes of the symols in its RHS. ttriutes flow ottom-up in the prse tree. Inherited ttriutes ttriutes for symols in RHS re omputed from ttriutes of LHS nd symols in RHS preeding them. ttriutes flow top-down in the prse tree. Semnti nlysis

S-ttriuted nd L-ttriuted Grmmrs S-ttriuted grmmr ll ttriutes re synthesized. ttriutes flow ottom-up. L-ttriuted grmmr For eh prodution X Y 1 Y 2... Y k, X.syn depends on X.inh nd Y 1.inh, Y 1.syn, Y 2.inh, Y 2.syn,..., Y k.inh, Y k.syn. For ll 1 i k, Y i.inh depends on X.inh nd Y 1.inh, Y 1.syn, Y 2.inh, Y 2.syn,..., Y i 1.inh, Y i 1.syn. S-ttriuted grmmrs re speil se of L-ttriuted grmmrs. Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E E T E T + 5 T - 10 Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E E T E T + 5 T - 10 This grmmr ptures left-ssoitivity. Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E 7 E 12 dd T 5 E 10 su T + 5 5 T 10-10 10 This grmmr ptures left-ssoitivity. Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs simple grmmr for rithmeti expressions using only ddition nd sutrtion E T E ET 10 + 5 = (10 ) + 5 = 12 10 + 5 10 ( + 5) = 2 T n + - E 7 E 12 dd T 5 Rule R PREDIT(R) E T 10 10 10 10 - su T + 5 5 E T E ET T n + - {n} {n} {n} {+} {-} This grmmr ptures left-ssoitivity. It is not LL(1). Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs n LL(1) grmmr for the sme lnguge E T E $ E ɛ E 1 T E 2 T n + - Rule R E T E $ {n} E 2.op :=.fun(e E 1.op, T.vl); ɛ E 1.vl {$} := E 2.vl E T E {+,-} T n + - PREDIT(R) {n} {+} {-} Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs n LL(1) grmmr for the sme lnguge E T E $ E ɛ E 1 T E 2 T n + - Rule R E T E $ {n} E 2.op :=.fun(e E 1.op, T.vl); ɛ E 1.vl {$} := E 2.vl E T E {+,-} T n + - PREDIT(R) {n} {+} {-} E T E $ 10 T E - T E + 5 ɛ Semnti nlysis

LL(1) Prsing, Left-ssoitivity, nd L-ttriuted Grmmrs n LL(1) grmmr for the sme lnguge E T E $ E ɛ E 1 T E 2 T n + - E.op := T.vl; E.vl := E.vl E.vl := E.op E 2.op :=.fun(e 1.op, T.vl); E 1.vl := E 2.vl T.vl := n.vl.fun := dd.fun := su E 12 10 10 12 T E $ 10 10 su T 7 E 12 - dd T 5 12 E 12 + 5 5 ɛ Semnti nlysis

tion Routines tion routines re instrutions for d-ho trnsltion interleved with prsing. Prser genertors llow progrmmers to speify tion routines in the grmmr. tion routines n pper nywhere in rule (s long s the grmmr is LL(1)). Exmple E 1 T { E 2.op =.fun(e 1.op, T.vl) } E 2 { E 1.vl = E 2.vl } tion routines re supported, for exmple, in y nd ison. Semnti nlysis