Top Banner
Programming Languages Attribute Grammars Chapter 4 1
21

Chapter 4 Languages - katie.cs.mtech.edu

Mar 25, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4 Languages - katie.cs.mtech.edu

Programming

Languages

Attribute Grammars

Chapter 4

1

Page 2: Chapter 4 Languages - katie.cs.mtech.edu

Objective

Today’s primary objective:

Know that action routines are similar to

attribute grammars, but are more flexible

Know that action routines are often used

in production compilers

Understand an action routine example

2

Page 3: Chapter 4 Languages - katie.cs.mtech.edu

Action Routines

Action routine – a semantic function that a

grammar writer can associate with a

grammar for the compiler to execute at

particular points during parsing

Action routines are more flexible than

attribute grammars.

3

Page 4: Chapter 4 Languages - katie.cs.mtech.edu

Action Routines

Action routines – routines attached to a

grammar which is executed by the

compiler as it parses code

Often used in production compilers

Can be used to build parse trees

4

Page 5: Chapter 4 Languages - katie.cs.mtech.edu

Greatest Common Divisor

Program

5

Page 6: Chapter 4 Languages - katie.cs.mtech.edu

Greatest Common Divisor

Syntax Tree

6

Page 7: Chapter 4 Languages - katie.cs.mtech.edu

Greatest Common Divisor

Syntax Tree - Continued

7

Page 8: Chapter 4 Languages - katie.cs.mtech.edu

Greatest Common Divisor

Abstract Syntax Tree

8

Page 9: Chapter 4 Languages - katie.cs.mtech.edu

Example for LL Parsing LL parsing – action routines can appear anywhere within a right-had side. Pointer to the action routine is pushed onto the stack and is executed when it is at the top of the stack.

Thus:

Action routine at the beginning is executed when the parser predicts the production

Routine embedded in the middle of the right-hand side is executed when the parser “matches” the symbol to its left

9

Page 10: Chapter 4 Languages - katie.cs.mtech.edu

Action Routine ExampleAction routines can be used to build a tree.

10

Page 11: Chapter 4 Languages - katie.cs.mtech.edu

Action

Routine

Example

11

Page 12: Chapter 4 Languages - katie.cs.mtech.edu

Action

Routine

Example

12

Page 13: Chapter 4 Languages - katie.cs.mtech.edu

Action

Routine

Example

13

Page 14: Chapter 4 Languages - katie.cs.mtech.edu

Action Routine ExampleConsider the grammar:

E → T TT

TT → + T TT | - T TT | ε

T → F FT

FT → * F FT | / F FT | ε

F → - F

F → ( E )

F → const

Figure 4.9 in text, page 195

14

Page 15: Chapter 4 Languages - katie.cs.mtech.edu

Action Routine Example

Action routines added:

E → T {TT.st:=T.ptr} TT {E.ptr:=TT.ptr}

TT1 → T {TT2.st:=make_bin_op(“+”,TT1.st,T.ptr)}

TT2 {TT1.ptr:=TT2.ptr}

TT1 → T {TT2.st:=make_bin_op(“-”,TT1.st,T.ptr)}

TT2 {TT1.ptr:=TT2.ptr}

TT → ε {TT.ptr:=TT.st}

T → F {FT.st:=F.ptr} FT {T.ptr:=FT.ptr}

15

Page 16: Chapter 4 Languages - katie.cs.mtech.edu

Action Routine Example -

continued

FT1 → F {FT2.st:=make_bin_op(“x”,FT1.st,F.ptr)}

FT2 {FT1.ptr:=FT2.ptr}

FT1 → F {FT2.st:=make_bin_op(“/”,FT1.st,F.ptr)}

FT2 {FT1.ptr:=FT2.ptr}

FT → ε {FT.ptr:=FT.st}

F1 → F2 {F1.ptr:=make_un_op(“+/-”,FT2.ptr)}

F → (E) {F.ptr:=E.ptr}

F → const {F.ptr:=make_leaf(const.ptr)}

16

Page 17: Chapter 4 Languages - katie.cs.mtech.edu

Build Tree

Use the previous grammar with action

routines to build a tree for the expression:

2 * 3 + 5

17

Page 18: Chapter 4 Languages - katie.cs.mtech.edu

Build Tree

Stack:

F F → const {F.ptr:=make_leaf(const.ptr)}

FT

T

TT

E

18

Page 19: Chapter 4 Languages - katie.cs.mtech.edu

Build Tree

Stack:

FT1→ * F {FT2.st:= make_bin_op(“x”,FT1.st, F.ptr)}

FT2 {FT1.ptr:= FT2.ptr}

*

F

FT

TT

19

Page 20: Chapter 4 Languages - katie.cs.mtech.edu

Simplified Action Routine

Example

Demonstrating action routines using a more

simple (LR) grammar.

E → E + T

E → E – T

E → T

T → T * F

T → T / F

T → F

F → - F

F → (E)

F → const

20

Page 21: Chapter 4 Languages - katie.cs.mtech.edu

Simplified Action Routine

ExampleMore simplified example:

E → E + T

E1.ptr:= make_bin_op(“+”, E2.ptr, T.ptr)

E → E - T

E1.ptr:= make_bin_op(“-”, E2.ptr, T.ptr)

E → T

E.ptr:= T.ptr

T → T * F

T1.ptr:= make_bin_op(“x”, T2.ptr, F.ptr)

T → T / F

T1.ptr:= make_bin_op(“/”, T2.ptr, F.ptr)

T → F

T.ptr:= F.ptr

F → - F

F1.ptr:= make_un_op(“+/-”, F2.ptr) (+/- - indicates a change of sign, similar to on calculators)

F → (E)

F.ptr:=E.ptr

F → const

F.ptr:=make_leaf(const.val)

21