Docsity

Attribute Grammars: Semantics for Programming Language Analysis - Prof. Ana L. Milanova, Study Guides, Projects, Research of Programming Languages

Toggle sidebar
bg1
1
Spring 09 CSCI 4430, A Milanova/BG Ryder 1
Announcements
๎˜Project 1 due Tuesday, Feb 24th
๎˜Submit in WebCT Dropbox!
๎˜HW3
๎˜Will be posted Feb 24th
๎˜Will be due a week from exam, March 6th
๎˜Reminder: Midterm Feb 27th
Spring 09 CSCI 4430, A Milanova/BG Ryder 2
Last Class: Binding Time
๎˜Notion of binding time: the time when an
answer becomes associated to a question
๎˜Examples:
๎˜Binding of variable to location (scoping)
๎˜Static (most modern languages)
๎˜Dynamic (old Lisp)
๎˜Binding of method call to method target
๎˜Static
๎˜Dynamic
3
program
a, b, c: integer;
procedure P
c: integer;
procedure S
c, d: integer;
procedure R
โ€ฆ
end R;
R();
end S;
R();
S();
end P;
procedure R
a: integer;
= a, b, c;
end R;
โ€ฆ; P(); โ€ฆ
end program
Last Class: Static vs. Dynamic Scoping
main
---
---
a
b
c
main.P
c
main.R
a
Static Scoping:
abound to R.a,
bto main.b,
cto main.c
Dynamic Scoping:
abound to R.a,
bto main.b,
cto P.c
Spring 09 CSCI 4430, A Milanova/BG Ryder 4
Last Class: Cost of Dynamic Binding
๎˜Dynamic binding
๎˜Less efficient
๎˜More flexible
๎˜Cost of dynamic binding of variable to
location
๎˜Cost of dynamic binding of method call to
method target
Spring 09 CSCI 4430, A Milanova/BG Ryder 5
Example: Cost of Dynamic Dispatch in C++
๎˜Source: Driesen and Hรถlzle, OOPSLAโ€™96
Aa
Bacf Ce
Dag Ebd
Virtual function tables (VFTs)
Capital characters denote classes,
lowercase characters message sele ctors,
and numbers method addresses
A0
a
B3
a
1
c
2
f
C0
a
E0
a
4
e
7
b d
8
D5
a
1
c
2
f g
6
load [object_reg+#VFTOffset],table_reg
load [table_reg+#selectorOffset],met hod_reg
call method_reg
Extra instructions: cost extra!
4
e
6
Semantic Analysis
Reading: Scott, Chapter 4
pf3
pf4
pf5

Partial preview of the text

Download Attribute Grammars: Semantics for Programming Language Analysis - Prof. Ana L. Milanova and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

Spring 09 CSCI 4430, A Milanova/BG Ryder 1

Announcements

 Project 1 due Tuesday, Feb 24th

 Submit in WebCT Dropbox!

 HW

 Will be posted Feb 24th

 Will be due a week from exam, March 6th

 Reminder: Midterm Feb 27th

Spring 09 CSCI 4430, A Milanova/BG Ryder 2

Last Class: Binding Time

 Notion of binding time: the time when an

answer becomes associated to a question

 Examples:

 Binding of variable to location (scoping)

 Static (most modern languages)  Dynamic (old Lisp)

 Binding of method call to method target

 Static  Dynamic

3

program a, b, c: integer; procedure P c: integer; procedure S c, d: integer; procedure R โ€ฆ end R; R(); end S; R(); S(); end P; procedure R a: integer; = a, b, c; end R; โ€ฆ; P(); โ€ฆ end program

Last Class: Static vs. Dynamic Scoping

main


a b c main.P

c main.R

a

Static Scoping: a bound to R.a, b to main.b, c to main.c

Dynamic Scoping: a bound to R.a, b to main.b, c to P.c

Spring 09 CSCI 4430, A Milanova/BG Ryder 4

Last Class: Cost of Dynamic Binding

 Dynamic binding

 Less efficient

 More flexible

 Cost of dynamic binding of variable to

location

 Cost of dynamic binding of method call to

method target

Spring 09 CSCI 4430, A Milanova/BG Ryder 5

Example: Cost of Dynamic Dispatch in C++

 Source: Driesen and Hรถlzle, OOPSLAโ€™

A (^) a

B (^) acf C (^) e

D (^) ag E (^) bd

Virtual function tables (VFTs) Capital characters denote classes, lowercase characters message selectors, and numbers method addresses

A (^0)

a

B (^3)

a 1

c 2

f

C (^0)

a

E (^0)

a 4

e 7

b d 8

D (^5)

a 1

c 2

f g 6

load [object_reg+#VFTOffset],table_reg load [table_reg+#selectorOffset],method_reg call method_reg

Extra instructions: cost extra!

4

e

6

Semantic Analysis

Reading: Scott, Chapter 4

Spring 09 CSCI 4430, A Milanova/BG Ryder 7

Lecture Outline

 Static Semantics

 Syntax vs. Static Semantics

 Static Semantics vs. Dynamic Semantics

 Attribute Grammars

 S-attributed grammars  L-attributed grammars  Attribute flow

Spring 09 CSCI 4430, A Milanova/BG Ryder 8

Static Semantics

 So far we considered syntax analysis

 Informally, it deals with the form of PL constructs

 Today we consider static semantic analysis

 Informally, it deals with the meaning of PL

constructs

 The distinction between the two is fuzzy

 In practice, anything that is not expressed in

terms of CFG (LALR(1) grammars) is considered

semantics

Spring 09 CSCI 4430, A Milanova/BG Ryder 9

Static Semantics vs. Dynamic Semantics

 Static semantic analysis (compile-time)

 Informally, reasons about properties that can be

determined statically, without program execution

 E.g., determine static types of expressions, detect certain errors

 Dynamic semantic analysis (run-time)

 Reasons about properties that depend on

program execution

 E.g., could z=x/y cause division by zero, could expression a[i] index out of array bounds, etc.?

Spring 09 CSCI 4430, A Milanova/BG Ryder 10

The Role of Semantic Analysis

 Static Semantic Analysis

 Detect as many errors as possible  Type inference and type checking

 Dynamic Semantic Analysis

 Detect errors by performing dynamic checks  Tradeoff: dynamic checks slow program execution  PLs differ greatly in the amount of run-time checks  C++: none.  Java: many (we will see examples).

Spring 09 CSCI 4430, A Milanova/BG Ryder 11

Examples of Static Semantic Errors

 Undeclared variable: a variable must be

declared before use

 Type mismatch:

 x = y : declared type of left-hand-side does not

โ€œmatchโ€ declared type of right-hand-side

 A a; โ€ฆ ; a.m(): method m() cannot be

invoked on a variable of declared type A

 Definite-variable assignment in Java: a local

variable must be assigned before it is used

Spring 09 CSCI 4430, A Milanova/BG Ryder 12

Examples of Dynamic Semantic Errors

 Array indexed out of bounds:

 a[i], i goes beyond the length of a

 What happens in C++? What happens in Java?

 Null pointer dereference:

 a.m()in Java, a->m()in C++ and a is null

 What happens in C++? What happens in Java?

 Casting an object to a type of which it is not

an instance

 C++? Java?

Spring 09 CSCI 4430, A Milanova/BG Ryder 19

Example (ii)

 val: Attributes associated to symbols

 Intuitively, S.val holds the value of the expression represented by subtree rooted at symbol S.  Fresh attributes are associated with every node in the parse tree

 Indices used to distinguish between symbols with

same name within same production

 E.g., E โ†’ E 1 +T E.val := E 1 .val+T.val

 Attributes of terminals supplied by scanner

 Attributes of some symbols + and * never used

Spring 09 CSCI 4430, A Milanova/BG Ryder 20

Example (iii): Decorated parse tree for

input 35 + 2

E +

* F

S

num

num (^) 3

num (^) (^5) num 2

4 F 3

T 3

T 15 5 F 2

T 2

15

F 4

T 8

E 23

S โ†’ E print( E.val) E โ†’ E 1 +T E.val := E 1 .val+T.val E โ†’ T E.val := T.val T โ†’ T 1 F T.val := T 1 .valF.val T โ†’ F T.val := F.val F โ†’ num F.val := num. val

Spring 09 CSCI 4430, A Milanova/BG Ryder 21

Building an Abstract Syntax Tree (AST)

 So far, we talked about parse trees

 In fact, compilers use abstract syntax trees

 Suitable intermediate representation

 Define semantic analyses  Basis for translation into intermediate code

 An AST is an abbreviated parse tree

 Operators and keywords do not appear as leaves

but at the interior node that would be its parent

 Chains of single productions are collapsed

Spring 09 CSCI 4430, A Milanova/BG Ryder 22

Building ASTs for Expressions

E

E T

T

T F F

num:

T F


F

num:

num:

num:

Parse tree for 35+24 Abstract syntax tree: +


num: num:

num:

num:

How do we construct syntax trees for expressions?

Spring 09 CSCI 4430, A Milanova/BG Ryder 23

Attribute Grammar to build AST for

Expression (denote by AG2)

 An attribute grammar:

Production Semantic Rule E โ†’ E 1 + T E.nptr := mknode(โ€˜ + โ€™, E 1 .nptr, T.nptr) E โ†’ T E.nptr := T.nptr T โ†’ T 1 ***** F T.nptr := mknode(โ€˜*โ€™, T 1 .nptr, F.nptr) T โ†’ F T.nptr := F.nptr F โ†’ num F.nptr := mkleaf(โ€˜numโ€™, num. val)

Function mknode(op,left,right) creates an operator node with label op, and two fields containing pointers left and right Function mkleaf(num,num.val) creates a leaf node with label num, and a field containing the value of the number

Attribute โ€œnodepointerโ€

Spring 09 CSCI 4430, A Milanova/BG Ryder 24

Constructing ASTs for Expressions

 Input 3 * 5 + 2 * 4

S

S โ†’ E E โ†’ E+T | T T โ†’ T*F | F F โ†’ num

num, 3

F

T F

num, 5

E

num, 2

F

T

num, 4

F


T *,^ ,

T *,^ ,

E +,^ ,

Spring 09 CSCI 4430, A Milanova/BG Ryder 25

Another Example

 So far we saw a very special kind of attribute

grammars

 Consider the following context-free grammar:

 Goal: construct an attribute grammar that

computes the value of an expression

 Values must be computed โ€œnormallyโ€, i.e., 5 โ€“ 3 โ€“ 2 will be evaluated as (5-3)-2 and not as 5-(3-2)

E โ†’ TEโ€™ Eโ€™ โ†’ - TEโ€™ | ฮต T โ†’ FTโ€™ Tโ€™ โ†’ / FTโ€™ | ฮต F โ†’ num

Spring 09 CSCI 4430, A Milanova/BG Ryder 26

Attribute Grammar to Compute Value for

Expressions (denote by AG3)

Production Semantic Rules

E โ†’ TEโ€™ (1) Eโ€™.sub := T.val (2) E.val := Eโ€™.val Eโ€™ โ†’ - TEโ€™ 1 (1) Eโ€™ 1 .sub:= Eโ€™.sub - T.val (2) Eโ€™.val := Eโ€™ 1 .val Eโ€™ โ†’ ฮต (1) Eโ€™.val := Eโ€™.sub

T โ†’ FTโ€™ (1) Tโ€™.sub := F.val (2) T.val := Tโ€™.val Tโ€™ โ†’ / FTโ€™ 1 (1) Tโ€™ 1 .sub:= Tโ€™.sub / F.val (2) Tโ€™.val := Tโ€™ 1 .val Tโ€™ โ†’ ฮต (1) Tโ€™.val := Tโ€™.sub

F โ†’ num (1) F.val := num. val (synthesized)

E โ†’ TEโ€™ Eโ€™ โ†’ - TEโ€™ | ฮต T โ†’ FTโ€™ Tโ€™ โ†’ / FTโ€™ | ฮต F โ†’ num

Spring 09 CSCI 4430, A Milanova/BG Ryder 27

Example (ii)

Eโ€™

T Eโ€™ 1

E.g., 25 - 34 - 3 - 6 Eโ€™ holds subtotal 13 (for 25 - 34, computed so far) T holds value 3 (i.e., the value of next in the sum) Eโ€™ 1 gets subtotal 10 (for 25 - 3*4 - 3 computed as 13 - 3) Passed down the tree of Eโ€™ 1 to next Eโ€™ on chain Eventually, we hit Eโ€™ โ†’ ฮต and value gets subtotal 4 Value 4 is passed back up

โ€ฆ

โ€ฆ (^) โ€ฆ

3

13

10

Attribute Eโ€™ 1 .sub: computed based on parent Eโ€™ and sibling T ( Eโ€™.sub - T.val)

4

4

Eโ€™

Spring 09 CSCI 4430, A Milanova/BG Ryder 28

Example (iii)

 Attribute. val is the value of the subtree

 Attribute .sub is the subtotal carried from left

 Productions for nonterminals E, T and F do

not perform computation

 Thus, no need for. sub, only. val attribute

 .val attribute is carried to the right

 E.g., T in E โ†’ TEโ€™ : value of T is passed right to Eโ€™  E.g., T in Eโ€™ โ†’ - TEโ€™ 1 : value of T is passed right to Eโ€™ 1 as right operand when computing the subtotal of Eโ€™ 1

Spring 09 CSCI 4430, A Milanova/BG Ryder 29

Example (iv)

 Productions for nonterminals Eโ€™ and Tโ€™ do

perform computation

 Thus, Eโ€™ and Tโ€™ need to carry subtotal in .sub

 E.g., in Eโ€™ โ†’โ†’โ†’โ†’ - TEโ€™ 1 the subtotal of Eโ€™ 1 is computed by subtracting the value of T from the subtotal of Eโ€™; the subtotal of Eโ€™ 1 is carried down the subtree of Eโ€™ 1  E.g., in Tโ€™ โ†’โ†’โ†’โ†’ / FTโ€™ 1 the same happens

Spring 09 CSCI 4430, A Milanova/BG Ryder 30

Synthesized and Inherited Attributes

 Synthesized attributes

 Attribute value depends on descendents in tree

 E.g., val and nptr attributes in AG1 and AG

 Inherited attributes

 Attribute value depends on parent and siblings in

tree

 E.g., sub attribute in AG

 In order to compute value โ€œnormallyโ€ we needed sub (inherited attributes).