Semantic Analysis I: Syntax Directed Definitions | EECS 483, Papers of Electrical and Electronics Engineering

Material Type: Paper; Class: Compiler Constr; Subject: Electrical Engineering And Computer Science; University: University of Michigan - Ann Arbor; Term: Fall 2003;

Typology: Papers

Pre 2010

Uploaded on 09/17/2009

koofers-user-y4i
koofers-user-y4i 🇺🇸

5

(1)

9 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Semantic Analysis I
Syntax-Directed Definitions
Intro to Semantic Analysis
EECS 483 – Lecture 9
University of Michigan
Wednesday, October 1, 2003
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Semantic Analysis I: Syntax Directed Definitions | EECS 483 and more Papers Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis I Syntax-Directed Definitions Intro to Semantic Analysis

EECS 483 – Lecture 9 University of Michigan Wednesday, October 1, 2003

  • 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 -

Implicit AST Construction

Y

LL/LR parsing techniques implicitly buildAST

Y

The parse tree is captured in the derivation

» LL parsing: AST represented by applied

productions

» LR parsing: AST represented by applied

reductions

Y

We want to explicitly construct the ASTduring the parsing phase

  • 4 -

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

  • 6 -

AST Construction for LR - Example

S

Æ

E + S | S

E

Æ

num | (S)

S + E

Num(1)

Num(2)

input string: “1 + 2 + 3” Add

S

.^

Add

Num(1)

Num(2)

Num(3)

Add

stack

Num(3)

Before reduction: S

Æ

E + S

After reduction: S

Æ

E + S

  • 7 -

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

  • 9 -

Semantic Actions

Y

Actions = C code (for bison/yacc)

Y

The actions access the parser stack

» Parser generators extend the stack of symbols with

entries for user-defined structures (e.g., parse trees)

Y

The action code should be able to refer to thegrammar symbols in the productions

» Need to refer to multiple occurences of the same non-

terminal symbol, distinguish RHS vs LHS occurrence y

E

Æ

E + E

» Use dollar variables in yacc/bison ($$, $1, $2, etc.)

y

expr ::= expr PLUS expr

{$$ = $1 + $3;}

  • 10 -

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

  • 12 -

Other Syntax-Directed Definitions

Y

Can use syntax-directed definitions toperform semantic checks during parsing

» E.g., type checks

Y

Benefit = efficiency

» One single compiler pass for multiple tasks

Y

Disadvantage = unstructured code

» Mixes parsing and semantic checking phases » Performs checks while AST is changing

  • 13 -

Type Declaration Example D

Æ

T id

{AddType(id, T.type);

D.type = T.type; }

D

Æ

D1, id

{AddType(id, D1.type);

D.type = D1.type; }

T

Æ

int

{T.type = intType; }

T

Æ

float

{T.type = floatType; }

{AddType($2, $1.type);$$.type = $1.type; }

this really looks like

  • 15 -

Type Declaration Example 2

D

Æ

TL

{AddType(id, T.type);

D.type = T.type; L.type = D.type; }

T

Æ

int

{T.type = intType; }

T

Æ

float

{T.type = floatType; }

L

Æ

L1, id

{AddType(id, L1.type);

L

Æ

id

{AddType(id, ???); }

  • 16 -

Propagation of Values 2

Propagate values both bottom-up and top-down

int a, b

D

id

L

T

int

intType

T.type

L.type

D.type

L

,^

id

L.type

AddType(id, L.type)

AddType(id, L.type)

  • 18 -

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

  • 19 -

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)