

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























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.
Problems
» The generated parser needs to contain AST
construction code » How to construct a customized AST data structure
using an automatic parser generator?
» E.g., semantic checks » This can reduce the number of compiler passes
Syntax-Directed Definition
y
E + S {action}
Building the AST
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)
Class Problem
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
Type Declaration Example
id
int
id
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
T id
D1, id
int
float
Type Declaration Example 2
id
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
id
L.type
int
float
L1, id
id
AST Attributes (2)
» Values from parents and siblings = inherited » Values from children = synthesized
» 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
Strategies for Attribute Evaluation
» 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
» Order of evaluation of attributes established when the
compiler is constructed
» Order determined by order nodes are visited (e.g.,
parsing method, top-down or bottom-up)
Multi-Pass Approach
» It is better when efficiency is not a critical issue
Semantic Analysis
Categories of Semantic Analysis
» 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
» Semantic rules regarding types » Semantic rules regarding scopes
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