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

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

CS 314 Principles of Programming Languages

CS241 Week 6 Tutorial Solutions

Formal Languages Simplifications of CFGs

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

FABER Formal Languages, Automata and Models of Computation

Finite State Automata and Determinisation

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

CS 275 Automata and Formal Language Theory

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

Unit 4. Combinational Circuits

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

CSE 401 Compilers. Today s Agenda

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

CSCI565 - Compiler Design

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

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:

Parsing and Pattern Recognition

2.4 Theoretical Foundations

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

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

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

Metodologie di progetto HW Technology Mapping. Last update: 19/03/09

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

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

Parse trees, ambiguity, and Chomsky normal form

arxiv: v2 [cs.lo] 24 Feb 2017

CS 275 Automata and Formal Language Theory

Engr354: Digital Logic Circuits

Nondeterministic Automata vs Deterministic Automata

a b b a pop push read unread

Memory Minimization for Tensor Contractions using Integer Linear Programming.

Global alignment. Genome Rearrangements Finding preserved genes. Lecture 18

1.4 Nonregular Languages

RELATIONAL MODEL.

Part 4. Integration (with Proofs)

Non Deterministic Automata. Formal Languages and Automata - Yonsei CS 1

Instructions. An 8.5 x 11 Cheat Sheet may also be used as an aid for this test. MUST be original handwriting.

TIME AND STATE IN DISTRIBUTED SYSTEMS

Nondeterministic Finite Automata

Lecture 6 Regular Grammars

CS375: Logic and Theory of Computing

CS 330 Formal Methods and Models

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

Alpha Algorithm: A Process Discovery Algorithm

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

Unfoldings of Networks of Timed Automata

INTEGRATION. 1 Integrals of Complex Valued functions of a REAL variable

Closure Properties of Regular Languages

CS 275 Automata and Formal Language Theory

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

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

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

Pre-Lie algebras, rooted trees and related algebraic structures

Non-Deterministic Finite Automata

Languages and Computation (G52LAC) Lecture notes Spring 2018

Review of CFGs and Parsing I Context-free Languages and Grammars. Winter 2014 Costas Busch - RPI 1

Linear Algebra Introduction

Bisimulation, Games & Hennessy Milner logic

1 PYTHAGORAS THEOREM 1. Given a right angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

CS 573 Automata Theory and Formal Languages

Lecture Notes No. 10

SWEN 224 Formal Foundations of Programming WITH ANSWERS

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

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

Solving Fixed-Point Equations by Derivation Tree Analysis

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

Refined interfaces for compositional verification

arxiv: v1 [math.ca] 21 Aug 2018

Tutorial Worksheet. 1. Find all solutions to the linear system by following the given steps. x + 2y + 3z = 2 2x + 3y + z = 4.

CSC 473 Automata, Grammars & Languages 11/9/10

Automatic Verification of Pointer Programs Using Grammar-Based Shape Analysis

1.3 Regular Expressions

Greedy regular expression matching

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

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

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

Lecture 6. Notes. Notes. Notes. Representations Z A B and A B R. BTE Electronics Fundamentals August Bern University of Applied Sciences

KNOWLEDGE-BASED AGENTS INFERENCE

Review for the Midterm

Algorithms & Data Structures Homework 8 HS 18 Exercise Class (Room & TA): Submitted by: Peer Feedback by: Points:

Prefix-Free Regular-Expression Matching

Lecture 3 Gaussian Probability Distribution

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

Normal Forms for Context-free Grammars

8 THREE PHASE A.C. CIRCUITS

Electromagnetism Notes, NYU Spring 2018

Automata and Languages

NFAs continued, Closure Properties of Regular Languages

Physics 116C Solution of inhomogeneous ordinary differential equations using Green s functions

Informatics 2A: Lecture 20. Shay Cohen. 31 October 2017

Formal Methods for XML: Algorithms & Complexity

Math 32B Discussion Session Week 8 Notes February 28 and March 2, f(b) f(a) = f (t)dt (1)

A Functorial Query Language

where the box contains a finite number of gates from the given collection. Examples of gates that are commonly used are the following: a b

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

Where did dynamic programming come from?

Intermediate Math Circles Wednesday 17 October 2012 Geometry II: Side Lengths

Transcription:

SEMNTI NLYSIS PRINIPLES OF PROGRMMING LNGUGES Norbert Zeh Winter 2018 Dlhousie University 1/28

PROGRM TRNSLTION FLOW HRT Soure progrm (hrter strem) Snner (lexil nlysis) Front end Prse tree Prser (syntti nlysis) Token strem Bk end Modified intermedite form Modified trget lnguge Semnti nlysis nd ode genertion Mhine-independent ode improvement Trget ode genertion Mhine-speifi ode improvement bstrt syntx tree or other intermedite form Trget lnguge (e.g., ssembly) Symbol tble 2/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 3/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 3/28

SYNTX ND SEMNTIS Syntx Desribes form of vlid progrm n be desribed by ontext-free grmmr 4/28

SYNTX ND SEMNTIS Syntx Desribes form of vlid progrm n be desribed by ontext-free grmmr Semntis Desribes mening of progrm nnot be desribed by ontext-free grmmr 4/28

SYNTX ND SEMNTIS Syntx Desribes form of vlid progrm n be desribed by ontext-free grmmr Semntis Desribes mening of progrm nnot be desribed by ontext-free grmmr Some onstrints tht my pper syntti re enfored by semnti nlysis! Exmple: Use of identifier only fter its delrtion 4/28

SEMNTI NLYSIS Semnti nlysis Enfores semnti rules Builds intermedite representtion (e.g., bstrt syntx tree) Fills symbol tble Psses results to intermedite ode genertor 5/28

SEMNTI NLYSIS Semnti nlysis Enfores semnti rules Builds intermedite representtion (e.g., bstrt syntx tree) Fills symbol tble Psses results to intermedite ode genertor Two pprohes Interleved with syntti nlysis s seprte phse 5/28

SEMNTI NLYSIS Semnti nlysis Enfores semnti rules Builds intermedite representtion (e.g., bstrt syntx tree) Fills symbol tble Psses results to intermedite ode genertor Two pprohes Interleved with syntti nlysis s seprte phse Forml mehnism ttribute grmmrs 5/28

ENFORING SEMNTI RULES Stti semnti rules Enfored by ompiler t ompile time Exmple: Do not use undelred vrible. 6/28

ENFORING SEMNTI RULES Stti semnti rules Enfored by ompiler t ompile time Exmple: Do not use undelred vrible. Dynmi semnti rules ompiler genertes ode for enforement t runtime. Exmples: Division by zero, rry index out of bounds Some ompilers llow these heks to be disbled. 6/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 7/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 7/28

TTRIBUTE GRMMRS ttribute grmmr n ugmented ontext-free grmmr: Eh symbol in prodution hs number of ttributes. Eh prodution is ugmented with semnti rules tht opy ttribute vlues between symbols, Evlute ttribute vlues using semnti funtions, Enfore onstrints on ttribute vlues. 8/28

TTRIBUTE GRMMR: EXMPLE E E + T E E T E T T T F T T / F T F F F F ( E ) F onst 9/28

TTRIBUTE GRMMR: EXMPLE E E + T E E T E T T T F T T / F T F F F F ( E ) F onst E 1 E 2 + T { E 1.vl = dd(e 2.vl, T.vl) } E 1 E 2 T { E 1.vl = sub(e 2.vl, T.vl) } E T { E.vl = T.vl } T 1 T 2 F { T 1.vl = mul(t 2.vl, F.vl) } T 1 T 2 / F { T 1.vl = div(t 2.vl, F.vl) } T F { T.vl = F.vl } F 1 F 2 { F 1.vl = neg(f 2.vl) } F ( E ) { F.vl = E.vl } F onst { F.vl = onst.vl } 9/28

SYNTHESIZED ND INHERITED TTRIBUTES Synthesized ttributes ttributes of LHS of prodution re omputed from ttributes of RHS of prodution. 10/28

SYNTHESIZED ND INHERITED TTRIBUTES Synthesized ttributes ttributes of LHS of prodution re omputed from ttributes of RHS of prodution. Inherited ttributes ttributes flow from left to right: From LHS to RHS, From symbols on RHS to symbols lter on the RHS. 10/28

SYNTHESIZED TTRIBUTES: EXMPLE The lnguge L = { n b n n n > 0} = {b, bb, bbb,...} is not ontext-free but n be defined using n ttribute grmmr: 11/28

SYNTHESIZED TTRIBUTES: EXMPLE The lnguge L = { n b n n n > 0} = {b, bb, bbb,...} is not ontext-free but n be defined using n ttribute grmmr: S B {ondition:.ount = B.ount =.ount} {.ount = 1} 1 2 { 1.ount = 2.ount + 1} B b {B.ount = 1} B 1 B 2 b {B 1.ount = B 2.ount + 1} {.ount = 1} 1 2 { 1.ount = 2.ount + 1} 11/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb 12/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S B B b B b b 12/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 2 B b 2 1 1 B b 1 b 12/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: 3 = 3 = 3 S 3 3 B 3 2 2 B b 2 1 1 B b 1 b 12/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb 13/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S B B b b 13/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 2 B 3 2 1 B b 2 1 b 1 13/28

SYNTHESIZED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: 3 2 3 S 3 2 B 3 2 1 B b 2 1 b 1 13/28

INHERITED TTRIBUTES: EXMPLE gin, we onsider the lnguge L = { n b n n n > 0} = {b, bb, bbb,...}. 14/28

INHERITED TTRIBUTES: EXMPLE gin, we onsider the lnguge L = { n b n n n > 0} = {b, bb, bbb,...}. S B {B.inhount =.ount;.inhount =.ount} {.ount = 1} 1 2 { 1.ount = 2.ount + 1} B b {ondition : B.inhount = 1} B 1 B 2 b {B 2.inhount = B 1.inhount 1} {ondition :.inhount = 1} 1 2 { 2.inhount = 1.inhount 1} 14/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb 15/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S B B b B b b 15/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 B 2 B b 1 B b b 15/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 B b 1 B b b 15/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 2 B b 2 1 1 B b 1 b 15/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (1) Input: bbb Prse tree: S 3 3 B 3 2 2 B b 2 1 1 = 1 B b 1 = 1 b 15/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb 16/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S B B b b 16/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 B 2 B b 1 b 16/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 3 B 3 2 B b 1 b 16/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 3 B 3 2 2 B b 2 1 b 1 16/28

INHERITED TTRIBUTES: PRSE TREE DEORTION (2) Input: bb Prse tree: S 3 3 B 3 2 2 1 B b 2 1 b 1 = 1 16/28

TTRIBUTE FLOW nnottion or deortion of the prse tree: Proess of evluting ttributes 17/28

TTRIBUTE FLOW nnottion or deortion of the prse tree: Proess of evluting ttributes Synthesized ttributes: ttributes of LHS of eh prodution re omputed from ttributes of symbols on the RHS of the prodution. ttributes flow bottom-up in the prse tree. 17/28

TTRIBUTE FLOW nnottion or deortion of the prse tree: Proess of evluting ttributes Synthesized ttributes: ttributes of LHS of eh prodution re omputed from ttributes of symbols on the RHS of the prodution. ttributes flow bottom-up in the prse tree. Inherited ttributes: ttributes for symbols in the RHS of eh prodution re omputed from ttributes of symbols to their left in the prodution. ttributes flow top-down in the prse tree. 17/28

S-TTRIBUTED ND L-TTRIBUTED GRMMRS S-ttributed grmmr ll ttributes re synthesized. ttributes flow bottom-up. 18/28

S-TTRIBUTED ND L-TTRIBUTED GRMMRS S-ttributed grmmr ll ttributes re synthesized. ttributes flow bottom-up. L-ttributed grmmr For eh prodution X Y 1 Y 2... Y k, X.syn depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y k. For ll 1 i k, Y i.inh depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y i 1. 18/28

S-TTRIBUTED ND L-TTRIBUTED GRMMRS S-ttributed grmmr ll ttributes re synthesized. ttributes flow bottom-up. L-ttributed grmmr For eh prodution X Y 1 Y 2... Y k, X.syn depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y k. For ll 1 i k, Y i.inh depends on X.inh nd ll ttributes of Y 1, Y 2,..., Y i 1. S-ttributed grmmrs re speil se of L-ttributed grmmrs. 18/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 3 + 5 E T E E T T + 5 3 10 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 E E T E T T + 5 3 10 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 E E T E T T + 5 3 10 This grmmr ptures left-ssoitivity. 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T 10 3 + 5 = (10 3) + 5 = 12 E ET 10 3 + 5 10 (3 + 5) = 2 T n + 12 E 7 E dd T 5 10 E sub T dd + 5 5 3 10 T sub 3 3 10 10 This grmmr ptures left-ssoitivity. 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 10 10 E T 10 7 sub sub 3 3 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 12 E dd T 5 T dd + 5 5 3 E Rule R PREDIT(R) E T {n} E ET {n} T n {n} + {+} { } This grmmr ptures left-ssoitivity. 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (1) simple grmmr for rithmeti expressions using ddition nd subtrtion: E T E ET T n + 10 10 10 E T 10 7 sub sub 3 3 10 3 + 5 = (10 3) + 5 = 12 10 3 + 5 10 (3 + 5) = 2 12 E dd T 5 T dd + 5 5 3 E Rule R PREDIT(R) E T {n} E ET {n} T n {n} + {+} { } This grmmr ptures left-ssoitivity. It is not LL(1)! 19/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } T E $ 10 T 3 E + E T E 5 ϵ 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 3 + E T E 5 ϵ 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T sub 3 sub 3 3 T E + 5 ϵ E 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 T E + 5 ϵ E 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 dd T 5 E + dd 5 5 ϵ E 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ E 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E $ 10 10 10 T 7 sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ E 12 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 10 E $ 10 10 12 T 7 E sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ 12 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: PREDIT E TE $ {n} E ϵ {$} E TE {+, } T n {n} + {+} { } E T 10 E 12 10 $ 10 10 12 T 7 E sub 3 sub 3 3 dd T 5 12 E + dd 5 5 ϵ 12 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: 12 PREDIT E E TE $ {n} T 10 E E 12 $ ϵ {$} 10 E TE {+, } 12 10 10 T 7 E T n {n} sub 3 + {+} sub 3 3 T E dd { } 5 12 + dd 5 5 ϵ 12 20/28

LL(1) PRSING, LEFT-SSOITIVITY, ND L-TTRIBUTED GRMMRS (2) n LL(1) grmmr for the sme lnguge: E TE $ E ϵ E 1 TE 2 T n + {E.in = T.vl; E.vl = E.vl} {E.vl = E.in} {E 2.in =.fun(e 1.in, T.vl); E 1.vl = E 2.vl} {T.vl = n.vl} {T.fun = dd} {T.fun = sub} 20/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 21/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 21/28

TION ROUTINES tion routines re instrutions for d-ho trnsltion interleved with prsing. Prser genertors (e.g., bison or y) llow progrmmers to speify tion routines in the grmmr. tion routines n pper nywhere in rule (s long s the grmmr is LL(1)). 22/28

TION ROUTINES: EXMPLE Exmple: E T {$3.in = $1.fun($0.in, $2.vl)} E {$0.vl = $3.vl} 23/28

TION ROUTINES: EXMPLE Exmple: E T {$3.in = $1.fun($0.in, $2.vl)} E {$0.vl = $3.vl} orresponding prse funtion in reursive desent prser: def prseee(node0): node1 = PrseTreeNode() node2 = PrseTreeNode() node3 = PrseTreeNode() prse(node1) prset(node2) node3.op = node1.fun(node0.in, node2.vl) prseee(node3) node0.vl = node3.vl 23/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 24/28

ROD MP Syntx, semntis, nd semnti nlysis ttribute grmmrs tion routines bstrt syntx trees 24/28

BSTRT SYNTX TREES Problem with prse trees: They represent the full derivtion of the progrm using grmmr rules. Some grmmr vribles re there only to id in prsing (e.g., to eliminte left-reursion or ommon prefixes). ode genertor is esier to implement if the output of the prser is s ompt s possible. bstrt syntx tree (ST) ompressed prse tree tht represents the progrm struture rther thn the prsing proess. 25/28

BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... 26/28

BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... fun foo swp drop +. 26/28

BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... fun foo swp drop +. Fun fun id Stmts. Stmt (swp) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ 26/28

BSTRT SYNTX TREE: EXMPLE (1) Fun fun id Stmts. Stmts ϵ Stmts Stmt Stmts Stmt... fun foo swp drop +. Fun fun id Stmts. ST: Stmt (swp) Stmts Fun Stmt (drop) Stmts id Stmts Stmt (+) Stmts Stmt (swp) Stmt (drop) Stmt (+) ϵ 26/28

BSTRT SYNTX TREE: EXMPLE (2) def prsefun(node0): node1 = PrseTreeNode() node2 = PrseTreeNode() mthfunkw() prseid(node1) prsesttements(node2) mthendkw() def prsesttements(node0): if next token is.: node0.sttements = [] else: node1 = PrseTreeNode() node2 = PrseTreeNode() prsesttement(node1) prsesttement(node2) node0.sttements = \ [node1.sttement] + \ node2.sttements Fun fun id Stmts. id Stmt (swp) Stmt (drop) Fun Stmts Stmt (+) Stmts Stmts Stmts Stmt (swp) Stmt (drop) Stmt (+) ϵ 27/28

SUMMRY Semnti nlysis ugments the prsing proess to represent the mening of the progrm. The output is often n nnotted bstrt syntx tree (ST). ttribute grmmrs nd tion routines re used to onstrut the ST. 28/28