Docsity

Syntax-Directed Definition & Semantic Actions in EECS 483 at UMich, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Syntax-Directed Definition & Semantic Actions in EECS 483 at UMich and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis I Syntax Directed Definition Symbol Tables

EECS 483 – Lecture 11 University of Michigan Wednesday, October 11, 2006

  • 1 -

Announcements

™

Updated schedule (week behind syllabus) »

Today 10/11: Semantic analysis I »

Mon 10/16: Fall break – no class »

Wed 10/18: Semantic analysis II »

Mon 10/23: MIRV Q/A session (Yuan Lin) »

Wed 10/25: Semantic analysis III (Simon Chen) »

Mon 10/30: Exam review »

Wed 11/1: Exam 1 in class

™

Project 2 »

Teams of 2

Æ

Please send Simon/I mail with names

y

Persons can work individually if really want to

»

No extensions on deadline due to Exam, so get started!

™

Reading - 5.1-5.6, 7.

  • 3 -

Problems

™

Unstructured code: mixing parsing code withAST construction code

™

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?

™

May want to perform other actions concurrentlywith parsing phase

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

  • 4 -

Syntax-Directed Definition

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

  • 6 -

Building the AST

™

Use semantic actions to build the AST

™

AST is built bottom-up along with parsing

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

Recall: User-defined type for objects on the stack (%union)

  • 7 -

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

Assume left associative

  • 9 -

Type Declaration Example

D

D

id

int

id

T

T.type intType

D.type

D.type

AddType(id, D.type)

AddType(id, T.type)

int a, b

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

D

Æ

T id

D

Æ

D1, id

T

Æ

int

T

Æ

float

  • 10 -

Type Declaration Example 2

D

id

L

T

int

intType

T.type

L.type

D.type

AddType(id, L.type)

AddType(id, L.type)

int a, b

Propagate values both bottom-up and top-down

L

,^

id

L.type

D

Æ

TL

T

Æ

int

T

Æ

float

L

Æ

L1, id

L

Æ

id

  • 12 -

AST Attributes (2)

™

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

™

Terminals compute only synthesized attrs

™

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

™

Constant values called intrinsic attributes

  • 13 -

Strategies for Attribute Evaluation

™

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

™

Rules based

» Order of evaluation of attributes established when the

compiler is constructed

™

On-the-fly

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

parsing method, top-down or bottom-up)

  • 15 -

Multi-Pass Approach

™

Separate AST construction from semanticchecking phase

™

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

™

This approach is less error-prone

» It is better when efficiency is not a critical issue

™

Attribute evaluation proceeds as tree-walk of theAST

  • 16 -

Semantic Analysis

Lexically and syntactically correctprograms may still contain other errors

Lexical and syntax analyses are notpowerful enough to ensure the correctusage of variables, objects, functions, ...

™

Semantic analysis: Ensure that the programsatisfies a set of rules regarding the usage ofprogramming constructs (variables, objects,expressions, statements)

  • 18 -

Categories of Semantic Analysis

™

Examples of semantic rules

» Variables must be defined before being used » A variable should not be defined multiple times » In an assignment stmt, the variable and the expression

must have the same type » The test expr. of an if statement must have boolean

type

™

2 major categories

» Semantic rules regarding types » Semantic rules regarding scopes

  • 19 -

Type Information/Checking

™

Two main categories of semantic analysis »

Type information »

Scope information

™

Type Information: Describes what kind of valuescorrespond to different constructs: variables, statements,expressions, functions, etc. »

variables:

int a;

integer

»

expressions:

(a+1) == 2

boolean

»

statements:

a = 1.0;

floating-point

»

functions:

int pow(int n, int m) int = int,int

™

Type Checking: Set of rules which ensures the typeconsistency of different constructs in the program