SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh - - PowerPoint PPT Presentation

semantic analysis
SMART_READER_LITE
LIVE PREVIEW

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh - - PowerPoint PPT Presentation

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/28 PROGRAM TRANSLATION FLOW CHART code improvement Symbol table other intermediate form Abstract syntax tree or Back end target language


slide-1
SLIDE 1

SEMANTIC ANALYSIS

PRINCIPLES OF PROGRAMMING LANGUAGES

Norbert Zeh Winter 2018

Dalhousie University 1/28

slide-2
SLIDE 2

PROGRAM TRANSLATION FLOW CHART

Scanner (lexical analysis) Parser (syntactic analysis) Semantic analysis and code generation Source program (Character stream) Token stream Parse tree Front end Machine-independent code improvement Target code generation Machine-specific code improvement Modified intermediate form Target language (e.g., assembly) Modified target language Back end Abstract syntax tree or

  • ther intermediate form

Symbol table 2/28

slide-3
SLIDE 3

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

3/28

slide-4
SLIDE 4

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

3/28

slide-5
SLIDE 5

SYNTAX AND SEMANTICS

Syntax

  • Describes form of a valid program
  • Can be described by a context-free grammar

Semantics

  • Describes meaning of a program
  • Cannot be described by a context-free grammar

Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration

4/28

slide-6
SLIDE 6

SYNTAX AND SEMANTICS

Syntax

  • Describes form of a valid program
  • Can be described by a context-free grammar

Semantics

  • Describes meaning of a program
  • Cannot be described by a context-free grammar

Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration

4/28

slide-7
SLIDE 7

SYNTAX AND SEMANTICS

Syntax

  • Describes form of a valid program
  • Can be described by a context-free grammar

Semantics

  • Describes meaning of a program
  • Cannot be described by a context-free grammar

Some constraints that may appear syntactic are enforced by semantic analysis! Example: Use of identifier only after its declaration

4/28

slide-8
SLIDE 8

SEMANTIC ANALYSIS

Semantic analysis

  • Enforces semantic rules
  • Builds intermediate representation (e.g., abstract syntax tree)
  • Fills symbol table
  • Passes results to intermediate code generator

Two approaches

  • Interleaved with syntactic analysis
  • As a separate phase

Formal mechanism

  • Attribute grammars

5/28

slide-9
SLIDE 9

SEMANTIC ANALYSIS

Semantic analysis

  • Enforces semantic rules
  • Builds intermediate representation (e.g., abstract syntax tree)
  • Fills symbol table
  • Passes results to intermediate code generator

Two approaches

  • Interleaved with syntactic analysis
  • As a separate phase

Formal mechanism

  • Attribute grammars

5/28

slide-10
SLIDE 10

SEMANTIC ANALYSIS

Semantic analysis

  • Enforces semantic rules
  • Builds intermediate representation (e.g., abstract syntax tree)
  • Fills symbol table
  • Passes results to intermediate code generator

Two approaches

  • Interleaved with syntactic analysis
  • As a separate phase

Formal mechanism

  • Attribute grammars

5/28

slide-11
SLIDE 11

ENFORCING SEMANTIC RULES

Static semantic rules

  • Enforced by compiler at compile time
  • Example: Do not use undeclared variable.

Dynamic semantic rules

  • Compiler generates code for enforcement at runtime.
  • Examples: Division by zero, array index out of bounds
  • Some compilers allow these checks to be disabled.

6/28

slide-12
SLIDE 12

ENFORCING SEMANTIC RULES

Static semantic rules

  • Enforced by compiler at compile time
  • Example: Do not use undeclared variable.

Dynamic semantic rules

  • Compiler generates code for enforcement at runtime.
  • Examples: Division by zero, array index out of bounds
  • Some compilers allow these checks to be disabled.

6/28

slide-13
SLIDE 13

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

7/28

slide-14
SLIDE 14

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

7/28

slide-15
SLIDE 15

ATTRIBUTE GRAMMARS

Attribute grammar An augmented context-free grammar:

  • Each symbol in a production has a number of attributes.
  • Each production is augmented with semantic rules that
  • Copy attribute values between symbols,
  • Evaluate attribute values using semantic functions,
  • Enforce constraints on attribute values.

8/28

slide-16
SLIDE 16

ATTRIBUTE GRAMMAR: EXAMPLE

E → E + T E → E − T E → T T → T ∗ F T → T / F T → F F → − F F → ( E ) F → const E1 E2 T E1 val add E2 val T val E1 E2 T E1 val sub E2 val T val E T E val T val T1 T2 F T1 val mul T2 val F val T1 T2 F T1 val div T2 val F val T F T val F val F1 F2 F1 val neg F2 val F E F val E val F const F val const val

9/28

slide-17
SLIDE 17

ATTRIBUTE GRAMMAR: EXAMPLE

E → E + T E → E − T E → T T → T ∗ F T → T / F T → F F → − F F → ( E ) F → const E1 → E2 + T { E1.val = add(E2.val, T.val) } E1 → E2 − T { E1.val = sub(E2.val, T.val) } E → T { E.val = T.val } T1 → T2 ∗ F { T1.val = mul(T2.val, F.val) } T1 → T2 / F { T1.val = div(T2.val, F.val) } T → F { T.val = F.val } F1 → − F2 { F1.val = neg(F2.val) } F → ( E ) { F.val = E.val } F → const { F.val = const.val }

9/28

slide-18
SLIDE 18

SYNTHESIZED AND INHERITED ATTRIBUTES

Synthesized attributes Attributes of LHS of production are computed from attributes of RHS of production. Inherited attributes Attributes flow from left to right:

  • From LHS to RHS,
  • From symbols on RHS to symbols later on the RHS.

10/28

slide-19
SLIDE 19

SYNTHESIZED AND INHERITED ATTRIBUTES

Synthesized attributes Attributes of LHS of production are computed from attributes of RHS of production. Inherited attributes Attributes flow from left to right:

  • From LHS to RHS,
  • From symbols on RHS to symbols later on the RHS.

10/28

slide-20
SLIDE 20

SYNTHESIZED ATTRIBUTES: EXAMPLE

The language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .} is not context-free but can be defined using an attribute grammar: S A B C Condition: A count B count C count A a A count 1 A1 A2 a A1 count A2 count 1 B b B count 1 B1 B2 b B1 count B2 count 1 C c C count 1 C1 C2 c C1 count C2 count 1

11/28

slide-21
SLIDE 21

SYNTHESIZED ATTRIBUTES: EXAMPLE

The language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .} is not context-free but can be defined using an attribute grammar: S → A B C {Condition: A.count = B.count = C.count} A → a {A.count = 1} A1 → A2 a {A1.count = A2.count + 1} B → b {B.count = 1} B1 → B2 b {B1.count = B2.count + 1} C → c {C.count = 1} C1 → C2 c {C1.count = C2.count + 1}

11/28

slide-22
SLIDE 22

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 3 3

12/28

slide-23
SLIDE 23

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 3 3

12/28

slide-24
SLIDE 24

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 3 3

12/28

slide-25
SLIDE 25

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 1 2 3 1 2 3 3 = 3 = 3

12/28

slide-26
SLIDE 26

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 2 3

13/28

slide-27
SLIDE 27

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 2 3

13/28

slide-28
SLIDE 28

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 2 3

13/28

slide-29
SLIDE 29

SYNTHESIZED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 1 2 1 2 3 3 ̸= 2 ̸= 3

13/28

slide-30
SLIDE 30

INHERITED ATTRIBUTES: EXAMPLE

Again, we consider the language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .}. S A B C B inhCount A count C inhCount A count A a A count 1 A1 A2 a A1 count A2 count 1 B b Condition B inhCount 1 B1 B2 b B2 inhCount B1 inhCount 1 C c Condition C inhCount 1 C1 C2 c C2 inhCount C1 inhCount 1

14/28

slide-31
SLIDE 31

INHERITED ATTRIBUTES: EXAMPLE

Again, we consider the language L = {anbncn | n > 0} = {abc, aabbcc, aaabbbccc, . . .}. S → A B C {B.inhCount = A.count; C.inhCount = A.count} A → a {A.count = 1} A1 → A2 a {A1.count = A2.count + 1} B → b {Condition : B.inhCount = 1} B1 → B2 b {B2.inhCount = B1.inhCount − 1} C → c {Condition : C.inhCount = 1} C1 → C2 c {C2.inhCount = C1.inhCount − 1}

14/28

slide-32
SLIDE 32

INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1

15/28

slide-33
SLIDE 33

INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1

15/28

slide-34
SLIDE 34

INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1

15/28

slide-35
SLIDE 35

INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1

15/28

slide-36
SLIDE 36

INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 1 1 1

15/28

slide-37
SLIDE 37

INHERITED ATTRIBUTES: PARSE TREE DECORATION (1)

Input: aaabbbccc Parse tree: A a a A a A B b b B b B C c c C c C S 1 2 3 3 3 2 2 1 1 1 = 1 1 = 1

15/28

slide-38
SLIDE 38

INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1

16/28

slide-39
SLIDE 39

INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1

16/28

slide-40
SLIDE 40

INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1

16/28

slide-41
SLIDE 41

INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1

16/28

slide-42
SLIDE 42

INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 1 1 1

16/28

slide-43
SLIDE 43

INHERITED ATTRIBUTES: PARSE TREE DECORATION (2)

Input: aaabbccc Parse tree: A a a A a A B b b B C c c C c C S 1 2 3 3 3 2 2 1 2 ̸= 1 1 = 1

16/28

slide-44
SLIDE 44

ATTRIBUTE FLOW

Annotation or decoration of the parse tree:

  • Process of evaluating attributes

Synthesized attributes:

  • Attributes of LHS of each production are computed from attributes of

symbols on the RHS of the production.

  • Attributes flow bottom-up in the parse tree.

Inherited attributes:

  • Attributes for symbols in the RHS of each production are computed from

attributes of symbols to their left in the production.

  • Attributes flow top-down in the parse tree.

17/28

slide-45
SLIDE 45

ATTRIBUTE FLOW

Annotation or decoration of the parse tree:

  • Process of evaluating attributes

Synthesized attributes:

  • Attributes of LHS of each production are computed from attributes of

symbols on the RHS of the production.

  • Attributes flow bottom-up in the parse tree.

Inherited attributes:

  • Attributes for symbols in the RHS of each production are computed from

attributes of symbols to their left in the production.

  • Attributes flow top-down in the parse tree.

17/28

slide-46
SLIDE 46

ATTRIBUTE FLOW

Annotation or decoration of the parse tree:

  • Process of evaluating attributes

Synthesized attributes:

  • Attributes of LHS of each production are computed from attributes of

symbols on the RHS of the production.

  • Attributes flow bottom-up in the parse tree.

Inherited attributes:

  • Attributes for symbols in the RHS of each production are computed from

attributes of symbols to their left in the production.

  • Attributes flow top-down in the parse tree.

17/28

slide-47
SLIDE 47

S-ATTRIBUTED AND L-ATTRIBUTED GRAMMARS

S-attributed grammar

  • All attributes are synthesized.
  • Attributes flow bottom-up.

L-attributed grammar For each production X Y1Y2 Yk,

  • X syn depends on X inh and all attributes of Y1 Y2

Yk.

  • For all 1

i k, Yi inh depends on X inh and all attributes of Y1 Y2 Yi

1.

S-attributed grammars are a special case of L-attributed grammars.

18/28

slide-48
SLIDE 48

S-ATTRIBUTED AND L-ATTRIBUTED GRAMMARS

S-attributed grammar

  • All attributes are synthesized.
  • Attributes flow bottom-up.

L-attributed grammar For each production X → Y1Y2 . . . Yk,

  • X.syn depends on X.inh and all attributes of Y1, Y2, . . . , Yk.
  • For all 1 ≤ i ≤ k, Yi.inh depends on X.inh and all attributes of Y1, Y2, . . . , Yi−1.

S-attributed grammars are a special case of L-attributed grammars.

18/28

slide-49
SLIDE 49

S-ATTRIBUTED AND L-ATTRIBUTED GRAMMARS

S-attributed grammar

  • All attributes are synthesized.
  • Attributes flow bottom-up.

L-attributed grammar For each production X → Y1Y2 . . . Yk,

  • X.syn depends on X.inh and all attributes of Y1, Y2, . . . , Yk.
  • For all 1 ≤ i ≤ k, Yi.inh depends on X.inh and all attributes of Y1, Y2, . . . , Yi−1.

S-attributed grammars are a special case of L-attributed grammars.

18/28

slide-50
SLIDE 50

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 3 5 10 3 5 12 10 3 5 10 3 5 2 10 T E 3 A T 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!

19/28

slide-51
SLIDE 51

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 10 3 5 12 10 3 5 10 3 5 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!

19/28

slide-52
SLIDE 52

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!

19/28

slide-53
SLIDE 53

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!

19/28

slide-54
SLIDE 54

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT R E T n E EAT n T n n A A This grammar captures left-associativity. It is not LL(1)!

19/28

slide-55
SLIDE 55

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT( ( (R) ) ) E → T {n} E → EAT {n} T → n {n} A → + {+} A → − {−} This grammar captures left-associativity. It is not LL(1)!

19/28

slide-56
SLIDE 56

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (1)

A simple grammar for arithmetic expressions using addition and subtraction: E → T E → EAT T → n A → + A → − 10 − 3 + 5 = (10 − 3) + 5 = 12 10 − 3 + 5 ̸= 10 − (3 + 5) = 2 10 T E − 3 A T + 5 E A T E 10 10 10 sub sub 3 3 add add 5 5 7 12 Rule R PREDICT( ( (R) ) ) E → T {n} E → EAT {n} T → n {n} A → + {+} A → − {−} This grammar captures left-associativity. It is not LL(1)!

19/28

slide-57
SLIDE 57

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} A 5 T E E 3 T A 10 E T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-58
SLIDE 58

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-59
SLIDE 59

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-60
SLIDE 60

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-61
SLIDE 61

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-62
SLIDE 62

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-63
SLIDE 63

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-64
SLIDE 64

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-65
SLIDE 65

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-66
SLIDE 66

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-67
SLIDE 67

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {n} E′ → ϵ {$} E′ → ATE′ {+, −} T → n {n} A → + {+} A → − {−} + A 5 ϵ T E′ E′ 3 T − A 10 E′ T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-68
SLIDE 68

LL(1) PARSING, LEFT-ASSOCIATIVITY, AND L-ATTRIBUTED GRAMMARS (2)

An LL(1) grammar for the same language: PREDICT E → TE′$ {E′.in = T.val; E.val = E′.val} E′ → ϵ {E′.val = E′.in} E′

1

E → ATE′

2

E {E′

2.in = A.fun(E′ 1.in, T.val); E′ 1.val = E′ 2.val}

T → n {T.val = n.val} A → + {T.fun = add} A → − {T.fun = sub} A 5 T E E 3 T A 10 E T $ E 10 10 10 sub sub 3 3 7 add add 5 5 12 12 12 12 12

20/28

slide-69
SLIDE 69

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

21/28

slide-70
SLIDE 70

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

21/28

slide-71
SLIDE 71

ACTION ROUTINES

Action routines are instructions for ad-hoc translation interleaved with parsing. Parser generators (e.g., bison or yacc) allow programmers to specify action routines in the grammar. Action routines can appear anywhere in a rule (as long as the grammar is LL(1)).

22/28

slide-72
SLIDE 72

ACTION ROUTINES: EXAMPLE

Example: E′ → AT {$3.in = $1.fun($0.in, $2.val)} E′ {$0.val = $3.val} Corresponding parse function in recursive descent parser:

def parseEE(node0): node1 = ParseTreeNode() node2 = ParseTreeNode() node3 = ParseTreeNode() parseA(node1) parseT(node2) node3.op = node1.fun(node0.in, node2.val) parseEE(node3) node0.val = node3.val

23/28

slide-73
SLIDE 73

ACTION ROUTINES: EXAMPLE

Example: E′ → AT {$3.in = $1.fun($0.in, $2.val)} E′ {$0.val = $3.val} Corresponding parse function in recursive descent parser:

def parseEE(node0): node1 = ParseTreeNode() node2 = ParseTreeNode() node3 = ParseTreeNode() parseA(node1) parseT(node2) node3.op = node1.fun(node0.in, node2.val) parseEE(node3) node0.val = node3.val

23/28

slide-74
SLIDE 74

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

24/28

slide-75
SLIDE 75

ROAD MAP

  • Syntax, semantics, and semantic analysis
  • Attribute grammars
  • Action routines
  • Abstract syntax trees

24/28

slide-76
SLIDE 76

ABSTRACT SYNTAX TREES

Problem with parse trees:

  • They represent the full derivation of the program using grammar rules.
  • Some grammar variables are there only to aid in parsing (e.g., to eliminate

left-recursion or common prefixes).

  • Code generator is easier to implement if the output of the parser is as

compact as possible. Abstract syntax tree (AST) A compressed parse tree that represents the program structure rather than the parsing process.

25/28

slide-77
SLIDE 77

ABSTRACT SYNTAX TREE: EXAMPLE (1)

Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts

26/28

slide-78
SLIDE 78

ABSTRACT SYNTAX TREE: EXAMPLE (1)

Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts

26/28

slide-79
SLIDE 79

ABSTRACT SYNTAX TREE: EXAMPLE (1)

Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ

26/28

slide-80
SLIDE 80

ABSTRACT SYNTAX TREE: EXAMPLE (1)

Fun → fun id Stmts . Stmts → ϵ Stmts → Stmt Stmts Stmt → . . . fun foo swap drop + . AST: Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+) Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ

26/28

slide-81
SLIDE 81

ABSTRACT SYNTAX TREE: EXAMPLE (2)

def parseFun(node0): node1 = ParseTreeNode() node2 = ParseTreeNode() matchFunKW() parseId(node1) parseStatements(node2) matchEndKW() def parseStatements(node0): if next token is .: node0.statements = [] else: node1 = ParseTreeNode() node2 = ParseTreeNode() parseStatement(node1) parseStatement(node2) node0.statements = \ [node1.statement] + \ node2.statements

Fun id fun Stmts . Stmt (swap) Stmts Stmt (drop) Stmts Stmt (+) Stmts ϵ Fun id Stmts Stmt (drop) Stmt (swap) Stmt (+)

27/28

slide-82
SLIDE 82

SUMMARY

  • Semantic analysis augments the parsing process to represent the meaning
  • f the program.
  • The output is often an annotated abstract syntax tree (AST).
  • Attribute grammars and action routines are used to construct the AST.

28/28