Docsity

Syntax Directed Definitions - Lecture Slides | EECS 483, Papers of Electrical and Electronics Engineering

Partial preview of the text

Download Syntax Directed Definitions - Lecture Slides | EECS 483 and more Papers Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis I Syntax-Directed Definitions

EECS 483 – Lecture 9 University of Michigan Wednesday, October 6, 2004

  • 1 -

Abstract Syntax Tree (AST) - Review

Y

Derivation = sequence ofapplied productions »

S

Æ

E+S

Æ

1+S

Æ

1+E

Æ

1+

Y

Parse tree = graphrepresentation of aderivation »

Doesn’t capture the orderof applying theproductions

Y

AST discardsunnecessary informationfrom the parse tree

S
E
S
( S )
E
E + S
E + S
E
( S )E + S
E
  • 3 -

AST Construction - LL

void parse_S() {

switch (token) {

case num: case ‘(‘:

parse_E(); parse_S’(); return; default:

ParseError();

Expr parse_S() {

switch (token) {

case num: case ‘(‘:

Expr left = parse_E(); Expr right = parse_S’(); if (right == NULL) return left else return new Add(left,right); default:

ParseError();

S
Æ
ES’
S’
Æ

ε

| +S
E
Æ

num | (S)

LL parsing: extend procedures for non-terminals

  • 4 -

AST Construction - LR

Y

We again need to add code for explicitAST construction

Y

AST construction mechanism

» Store parts of the tree on the stack » For each nonterminal symbol X on stack, also

store the sub-tree rooted at X on stack

» Whenever the parser performs a reduce

operation for a production X

Æ

, create an

AST node for X

  • 6 -

Problems

Y

Unstructured code: mixing parsing code withAST construction code

Y

Automatic parser generators

» The generated parser needs to contain AST

construction code » How to construct a customized AST data structure

using an automatic parser generator?

Y

May want to perform other actions concurrentlywith parsing phase

» E.g., semantic checks » This can reduce the number of compiler passes

  • 7 -

Syntax-Directed Definition

Y

Solution: Syntax-directed definition

» Extends each grammar production with an

associated semantic action (code):

y

S
Æ

E + S {action}

» The parser generator adds these actions into

the generated parser

» Each action is executed when the

corresponding production is reduced

  • 9 -

Building the AST

Y

Use semantic actions to build the AST

Y

AST is built bottom-up along with parsing Recall: User-defined type for objects on the stack (%union)

expr ::= NUM

{$$ = new Num($1.val); }

expr ::= expr PLUS expr

{$$ = new Add($1, $3); }

expr ::= expr MULT expr

{$$ = new Mul($1, $3); }

expr ::= LPAR expr RPAR

  • 10 -

Class Problem

E
Æ

num | (E) | E + E | E * E

Perform a LR derivation of the string: “(1+2)*3” Show where each part of the AST is constructed

  • 12 -

Type Declaration Example

Propagate type attributes while building AST from the bottom to the top

D
Æ

T id

D
Æ

D1, id

T
Æ

int

T
Æ

float

int a, b

D
D

id

T.type

D.type

D.type

AddType(id, D.type)

id

AddType(id, T.type)

T

intType

int

  • 13 -

Type Declaration Example 2

Propagate values both bottom-up and top-down

AddType(id, L.type)

D

id

L
T

int

intType

T.type

L.type

D.type

L
,^

id

L.type

int a, b

D
Æ
TL
T
Æ

int

T
Æ

float

L
Æ

L1, id

L
Æ

id

AddType(id, L.type)

  • 15 -

AST Attributes (2)

Y

An attribute for a node in the AST depends onvalues from parent nodes, sibling nodes andchildren nodes for evaluation

» Values from parents and siblings = inherited » Values from children = synthesized

Y

Terminals compute only synthesized attrs

Y

Non-terminals may compute either

» May compute inherited attrs from its children and pass

these values down the parse tree » May compute synthesized attribute and pass these

values up the parse tree

Y

Constant values called intrinsic attributes

  • 16 -

Strategies for Attribute Evaluation

Y

Walk dependence tree

» Construct AST, use that to establish the dependence

relationships to guide attribute evaluation » Most flexible, but may fail if get cycle » Build dep graph, topo sort determines order

Y

Rules based

» Order of evaluation of attributes established when the

compiler is constructed

Y

On-the-fly

» Order determined by order nodes are visited (e.g.,

parsing method, top-down or bottom-up)

  • 18 -

Multi-Pass Approach

Y

Separate AST construction from semanticchecking phase

Y

Traverse the AST and perform semantic checks(or other actions) only after the tree has been builtand its structure is stable

Y

This approach is less error-prone

» It is better when efficiency is not a critical issue

Y

Attribute evaluation proceeds as tree-walk of theAST