Вы находитесь на странице: 1из 53

Module 3

1
2
The Compiler So Far

3
4
Semantic Analysis
Source code

lexical
Lexical Analysis errors
tokens
syntax
Syntactic Analysis errors
AST
semantic
Semantic Analysis errors
AST

Intermediate Code Gen

5
6
7
8
9
Phases of a Compiler
1. Lexical Analyzer (Scanner)
Takes source Program and Converts into tokens
2. Syntax Analyzer (Parser)
Takes tokens and constructs a parse tree.
3. Semantic Analyzer
Takes a parse tree and constructs an abstract
syntax tree with attributes.(annotated parse
tree/decorated parse tree)

10
Syntax
determines valid form of program
Can be described by a context-free grammar
Semantics
behavior of valid program
Cannot be be described by a context-free grammar

11
Goals of a Semantic Analyzer
Compiler must do more than recognize whether a sentence
belongs to the language
Find remaining errors that would make program invalid
undefined variables, types
type errors that can be caught statically
Figure out useful information for later phases
types of all expressions
data layout
Semantics
Semantic analyzer
Determines meaning of program
Enforces semantic rules
Formal mechanism: Attributes grammars 12
Enforcing Semantic Rules
Static checks done at compile time
Enforced by compiler at compile time
Example: Do not use undeclared variable
Dynamic checks done at run time
Compiler generates code for enforcement at run time
Division by zero
Array bounds checks

13
The Semantic Processing phase consists of:
Checking the Static Semantics of the language
Generating an Intermediate Representation of the program
Checking the static semantics include:
Making sure that all identifiers used in a program are declared
Making sure that all functions called are declared or defined
Making sure that parameters are passed correctly
Checking the uses of operators and types of expressions
Entering identifiers in symbol tables

14
Examples of Reported Errors
Undeclared identifier
Multiply declared identifier
Index out of bounds
Wrong number or types of args to call
Incompatible types for operation
Break statement outside switch/loop
Goto with no label

15
16
17
Attributes

18
19
20
21
22
23
24
25
26
Example

27
28
29
30
31
32
33
34
35
Synthesized & Inherited Attributes

Synthesized attribute : EE1+E2 { E.val =E1.val +


E2.val}
Inherited attribute :AXYZ {Y.val = 2 * A.val}
Syntax-Directed Definitions
SDD is a context free grammar together with
attributes and values

Attributes are associated with grammar symbols


and rules with productions
Attribute Grammars
Decorated context free grammar
Associate attributes with nonterminals of
grammar
Associate rule with each production
E1=E2+T
E1:val := sum(E2:val,T:val)

38
39
Example: Simple Semantic
Production desk calculator
Rules
L En print(E.val)
E E1 + T E.val := E1.val + T.val
ET E.val := T.val
T T1 * F T.val := T1.val * F.val
TF T.val := F.val
F (E) F.val := E.val
F digit F.val := digit.lexval
Example: Annotated parse tree for 3*5+4
EE1+T2 {E.val=E1.val+T.val}
Production L
L En(n for new-
line) n
E E1 + T E.val=19
ET
T T1 * F E.val=15 + T.val=4
TF
F (E)
T.val=15 F.val=4
F digit

T.val=3 * F.val=5 digit.lexval=4

F.val=3 digit.lexval =5

digit.lexval =3
S-attributed definition
A syntax directed translation that uses
synthesized attributes exclusively is said to be
a S-attributed definition.

A parse tree for a S-attributed definition can


be annotated by evaluating the semantic rules
for the attributes at each node, bottom up
from leaves to the root.

42
Eg:Syntax-directed definition with Production Semantic Rules
inherited attribute L.in D TL L.In := T.type
Us real id1,id2,id3 ng T int T.Type := int
inherited attributes to parse a T real T.Type := real
declaration and add type
D symbol table.
information to the L L1, id L1.in := L.in
Addtype (id.entry,
L.in=real L.in)
T.type=real
L id Addtype (id.entry,
L.in)
,
real L1.in=real id

id The call to addtype is a side effect of SDD.


L.in=real , The terminal id has an attribute entry
,which is a pointer to symbol table entry
for that identifier

id
L-Attributed Definitions
An SDD is L-attributed if every attribute is
Synthesized, or
Inherited, such that, for a production
A -> X1 X2 . . . Xn, to find inherited attribute Xi.a, use:
Inherited attributes from the head A.
Inherited or synthesized attributes from X1 .. Xi-1
(symbols to the left of Xi in the RHS);
Inherited or synthesized attributes of this Xi that entail
no cycles.
use attributes from above or from the left
Conceptual views of syntax directed
translation
Execution
Order for
Parse Semantic
I/P Dependency
Tree rules
Graph
Dependency Graph
"Dependency graphs" are a useful tool for determining an
evaluation order for the attribute instances in a given parse tree.
While an annotated parse tree shows the values of attributes, a
dependency graph helps us determine how those values can be
computed.

If b:= f(c), i.e. b depends on c c must be computed


before b
In the dependency graph: c b

46
Dependency graph example
Production Semantic Rule
E -> E1 + E2 E.val := E1.val + E2.val

Wherever this rule appears in the parse, tree


we draw:

47
Dependency Graph
D TL L.type := T.type
T int T.type := integer
Dependency T real T.type := real
specifies L L1, id L1.type := L.type, addtype (id.entry, L,type)
the evaluation L id addtype (id.entry, L.type)
order D

T.type = int
T L1 L1.type = T.type

int
L2 L2.type = L1.type
, id addtype(id.entry, L1.type)

L3.type = L2.type
L3 , id addtype(id.entry, L2.type)

Input: int id, id, id


id addtype(id.entry,
L3.type)
Evaluation orders for SDDs
A dependency graph is used to determine the
order of computation of attributes
Dependency graph
For each parse tree node, the parse tree has a node
for each attribute associated with that node
If a semantic rule defines the value of synthesized
attribute A.b in terms of the value of X.c then the
dependency graph has an edge from X.c to A.b
If a semantic rule defines the value of inherited
attribute B.c in terms of the value of X.a then the
dependency graph has an edge from X.a to B.c
Annotated EXAMPLE:3*5 parse Tree for
Example of mixed attributesd2, id3
51
Ordering the evaluation of
attributes
If dependency graph has an edge from M to N
then M must be evaluated before the
attribute of N
Thus the only allowable orders of evaluation
are those sequence of nodes N1,N2,,Nk such
that if there is an edge from Ni to Nj then i<j
Such an ordering is called a topological sortof
a graph
53

Вам также может понравиться