View
248
Download
3
Category
Preview:
Citation preview
8/7/2019 Chap 5 SDT
1/49
Syntax-Directed Translation
8/7/2019 Chap 5 SDT
2/49
Source Program
Target Lang. Prog.
Semantic Analyzer
Syntax Analyzer
Lexical Analyzer
Front
End
Back End
Int. Code Generator
Intermediate Code
8/7/2019 Chap 5 SDT
3/49
Semantic Analyzer (Static Checker)
Catches all remaining errors
Semantic Analyzer checks the sourceprogram for semantic errors and gathers
type information for the subsequent codegeneration phase.
Compiler must do more than recognize
whether a sentence belongs to thelanguage
8/7/2019 Chap 5 SDT
4/49
Semantic Analyzer
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
Static checks done by the compiler
Dynamic checks done at run time
8/7/2019 Chap 5 SDT
5/49
Semantic Analysis
Source code
Lexical Analysis
Syntactic Analysis
Semantic Analysis
lexical
errors
syntaxerrors
semantic
errors
tokens
Syntax / Parse Tree
Annotated Parse Tree
8/7/2019 Chap 5 SDT
6/49
Semantic Analyzer
The semantic analyzer completes the symbol table with
information on the characteristics of each identifier.
In symbol table one entry is created for each identifier and
constant.
Scope is taken into account. Two different variables with the same name will have
different entries in the symbol table.
Nodes in the Syntax Tree containing identifiers or constants
really contain pointers into the symbol table.
The semantic analyzer completes the table using
information from declarations and perhaps the context
where the identifiers occur.
8/7/2019 Chap 5 SDT
7/49
Kinds of Checks Uniqueness checks
Certain names must be unique ( a var. is defined exactly once)
Many languages require variable declarations
Flow-of-control checks Match control-flow operators with structures
Example: break applies to innermost loop/switch or break is used
within a scope whose closing statement is missing.
Type checks Check compatibility of operators and operands
Name-related checks (var. that is defined previously isused)
Logical checks (Not done by Semantic Analyzer) Program is syntactically and semantically correct, but does not do the
correct thing
8/7/2019 Chap 5 SDT
8/49
Semantic Analyzer (Cont.)
Example of semantic errors that are tackled
statically: Undeclared identifiers Unreachable statements Identifiers used in the wrong context 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 Methods called with the wrong number of
parameters or with parameters of the wrong type. ...
8/7/2019 Chap 5 SDT
9/49
Semantic Analyzer v/s Syntax Analyzer
There can be overlapping of Syntax and SemanticAnalysis phase, by evaluating semantic rules duringparsing, without explicitly constructing a parse treeor a graph showing dependencies betweenattributes.
This is done generally in single passimplementation of a compiler and this is importantcompile time efficiency.
There are two notations for associating semanticrules with productions of CFG: Syntax Directed Definitions
Translation Schemes
8/7/2019 Chap 5 SDT
10/49
Semantic Analyzer
Semantic Analysis is performed in a syntax-directedfashion by augmenting the Context Free Grammar with
with each grammar symbol it associates a set ofattributes, e.g) type of construct or value ofconstruct
With each production, a set of semantic rules andthe parsing process by evaluation of attributesusing semantic rules and the parsing process byevaluation of attributes.
There are two schemes for this:
Sattributed scheme
Lattributed scheme
8/7/2019 Chap 5 SDT
11/49
Introduction
Here we will learn: translation of languages guided by
context-free grammar.
A Syntax Directed Definition uses a context free grammar tospecify the syntactic structure of input:
With each grammar symbol, it associates a set of attributes;e.g.) Type of construct, value of the construct, Numbers,types, table references, or strings etc.
And with each production, a set of semantic rules forcomputing values of the attributes associated with the
symbols appearing in that production.
The grammar and the set of semantic rules constitute theSyntax Directed Definition .
8/7/2019 Chap 5 SDT
12/49
Syntax Directed Definitions The output for each input x is specified in the following manner:
Construct a Parse tree for x. Suppose a node n in the parse tree is labeled by the
grammar symbol X.
We write X.a to denote the value of attribute a of X at that
node. The value of X.a at n is computed using the semantic rule for
attribute a associated with the production X used at thatparticular node n in the parse tree.
A parse tree showing the attribute values at each node iscalled anAnnotated parse Tree.
Values of Attributes in nodes of annotated parse-tree are either,
initialized to constant values or by the lexical analyzer.
determined by the semantic-rules.
8/7/2019 Chap 5 SDT
13/49
Syntax Directed Definitions
A SDD is a generalization of a CFG in whicheach grammar symbol has an associated set ofattributes partitioned into two sets:
Synthesized Attributes
Inherited Attributes
If we think of a node for the grammar symbol in aparse tree as a record with fields for holding
information, then an attribute corresponds to thename of a field.
8/7/2019 Chap 5 SDT
14/49
Form of a Syntax Directed Definition
In a SDD, each grammar production A is associated with it
a set of semantic rules of the form:
b := f ( c1, c2, .ck)
where f is a function and either:
b is a Synthesized Attribute of A, and c1, c2, .ck are
attributes belonging to the grammar symbols of theproductions, or
b is an inherited attribute of one of the grammar symbolson the right side of production A , c1, c2, .ck are
attributes belonging to grammar symbols of production A .
8/7/2019 Chap 5 SDT
15/49
Inherited & Synthesized AttributeLet X0 X1 ... Xn be a production
If the computing rule of X0s attribute is of theform A(X0) = f ( A(X1), ... , A(Xn) )
Synthesized attribute
If the computing rule of Xjs attribute is of theform A(Xj) = f ( A(X0), ..., A(Xi), )
Inherited attribute
Terminals have intrinsic attributes Lexical values supplied by the lexical
analyzer
8/7/2019 Chap 5 SDT
16/49
Synthesized Attributes
Attributes are values attached to the nodes of aparse tree.
The identifiers $$, $1, $2, etc., in Yacc actions aresynthesized attributes.
The value of a synthesized attribute at a node iscomputed from the values of attributes at thechildren of that node in parse tree.
Synthesized attributes are values that can be
computed bottom-up in the parse tree. Synthesized attributes can be easily computed by
a shift-reduce parser that keeps the values of theattributes on the parsing stack.
8/7/2019 Chap 5 SDT
17/49
Attribute Grammars Example Each grammar symbols has
a set ofattributes E.g. the value of E1 is the
attribute E1.val
Each grammar rule has a set
of rules over the symbolattributes
Copy rules
Semantic Function rules
E.g. sum, quotient
8/7/2019 Chap 5 SDT
18/49
8/7/2019 Chap 5 SDT
19/49
8/7/2019 Chap 5 SDT
20/49
Evaluating an SDD at the Nodes of a Parse Tree Example :
8/7/2019 Chap 5 SDT
21/49
S-Attributed Definitions
An SDD is S-attributedif every attribute is synthesized.
Example shown in the previous slide is an example of S-attributed definition.
When an SDD is S-attributed, we can evaluate its attributes inany bottom-up order of the nodes of the parse tree.
postorder(N){
for ( each child CofN, from the left )postorder(C);
evaluate the attributes associated with nodeN;
}
S-attributed definitions can be implemented during bottom-upparsing, since a bottom-up parse corresponds to a postordertraversal.
An S-attributed SDD can be implemented naturally in
conjunction with an LR parser.
8/7/2019 Chap 5 SDT
22/49
8/7/2019 Chap 5 SDT
23/49
Inherited Attributes and Bottom Up TranslationSome inherited attributes might not be available when we are
reducing by a certain production.Consider the translation scheme:
PRODUCTION SEMANTIC RULE
D p T L L.in = T.typeT p int T.type = integer
T p real T.type = real
Lp L1 , id L1.in = L.in
addtype(id.entry, L.in)
Lp id addtype(id.entry, L.in)
Attempt top down parsing overreal id1, id2 , id3
8/7/2019 Chap 5 SDT
24/49
Example with Inherited Attributes Even though inherited attrib can be simulated by synthesized it is
more natural to write Syntax-Directed Definitions using inherited.
below in is an inherited attribute ofL
PRODUCTION SEMANTIC RULE
D p T L L.in=
T.typeT p int T.type =integer
T p real T.type =real
L p L1 , id L1.in = L.in
addtype(id.entry, L.in)L p id addtype(id.entry, L.in)
8/7/2019 Chap 5 SDT
25/49
Example
Example real id1, id2 , id3
identry=id2
id
entry=id3
,
L
in=real
T
type=real
D
real
id
entry=id1
,
L
in=real
L
in=real
addtype(id1,real)
addtype(id2,real)
addtype(id3,real)
8/7/2019 Chap 5 SDT
26/49
L-Attributed Definitions A syntax-directed definition is called L-Attributed
if In the semantic rules of a production
Ap X1Xnthe inherited attributes of Xj depend only on
The attributes (synthesized or inherited) of the symbolsX1Xj-1 The inherited attributes of A
All S-attributed Definitions are also L-attributed.
S-attributed schemes do not permit inherited attributes andare too restrictive.
It is often more natural to use syntax directed definitions withinherited attributes.
8/7/2019 Chap 5 SDT
27/49
L-Attributed Definitions An SDD is L-attributed Between the attributes associated with a production body,
SDG edges can go from left to right, but not from right to left
(hence L-attributed). Each attribute must be either
Synthesized, or
Inherited, but with the rules limited as follows. Suppose that
there is a productionA pX1X2 Xn, and that there is aninherited attributeXi.a computed by a rule associated withthis production. Then the rule may use only:
Inherited attributes associated with the headA.
Either inherited or synthesized attributes associated withthe occurrence of symbolsX1X2 Xi-1 located to the leftofXi.
Inherited or synthesized attributes associated with thisoccurrence ofXi itself, but only in such a way that there
are no cycles in a DG formed by the attributes of thisXi.
8/7/2019 Chap 5 SDT
28/49
L-Attributed
Definitions
Example:
The SDD in Fig. is L-attributed.
Example:Any SDD containing the following production and rules cannotbe L-attributed.
A p B C A.s =B.b;
B.i = f(C.c,A.s)
8/7/2019 Chap 5 SDT
29/49
Evaluating an SDD at the Nodes of a Parse Tree
Imagine that the rules of an SDD are applied by first
constructing a parse tree and then using the rules to evaluateall of the attributes at each of the nodes of the parse tree(annotated parse tree).
How do we construct an annotated parse tree?
In what order do we evaluate attributes? Before we can evaluate an attribute at a node of a parsetree, we must evaluate all the attributes upon which itsvalue depends.
With synthesized attributes, we can evaluate attributes in any
bottom-up order, such as that of a postorder traversal of theparse tree.
For SDD with both inherited and synthesized attributes, thereis no guarantee that there is even one order in which toevaluate attributes at nodes.
8/7/2019 Chap 5 SDT
30/49
8/7/2019 Chap 5 SDT
31/49
Evaluating an SDD at the Nodes of a Parse Tree Example:
Fig. 5.4: An SDD based on a grammar
suitable for top-down parsing.
8/7/2019 Chap 5 SDT
32/49
Conceptual View of SDT
Conceptually with both syntax directed definitions and
translation schemes, input token stream is parsed, theparse tree is build, and then the tree is traversed asneeded to evaluate the semantic rules at the parse treenodes.
Inputstring Parsetree
Dependencygraph
Evaluationorder forsemantic rules
8/7/2019 Chap 5 SDT
33/49
Dependency Graphs
Dependency graphs are a useful tool fordetermining an evaluation order for the attributeinstances in a given parse tree.
A dependency graph (DG) depicts the flow of
information among the attribute instances in aparticular parse tree; an edge from one attributeinstance to another means that the value of thefirst is needed to compute the second.
8/7/2019 Chap 5 SDT
34/49
Dependency Graphs
Edges express constraints implied by the semantic
rules. For each parse tree node, say a node labeled by grammar
symbolX, the DG has a node for each attribute associatedwithX.
Suppose that a semantic rule associated with a productionp defines the value of synthesized attributeA.b in terms ofthe value ofX.c. Then, the DG has an edge fromX.ctoA.b.
Suppose that a semantic rule associated with a productionp defines the value of inherited attribute B.cin terms of thevalue ofX.a. Then, the DG has an edge fromX.a to B.c.
8/7/2019 Chap 5 SDT
35/49
Example:
Ep E1 + T E.val = E1.val+ T.val
8/7/2019 Chap 5 SDT
36/49
Dependency Graphs Example 5.5:
8/7/2019 Chap 5 SDT
37/49
8/7/2019 Chap 5 SDT
38/49
8/7/2019 Chap 5 SDT
39/49
Applications of Syntax-Directed Translation
Construction of syntax trees
Two SDDs for constructing syntax trees forexpressions
1.An S-attribute definition, suitable for use
during bottom-up parsing2.L-attributed, suitable for use during top-down
parsing
8/7/2019 Chap 5 SDT
40/49
Translation Scheme A translation scheme is a Context Free Grammar in which
program fragments called semantic actions are embeddedwithin the right sides of productions
A Translation scheme is like a SDD, except that the order of
evaluation of semantic rules is explicitly shown.
The position at which an action is to be executed is shown byenclosing it between braces and writing it within the right side of
a production:
e.g.) rest + term { print(+) } rest_1
rest
+ term { print ( + ) } rest_1
8/7/2019 Chap 5 SDT
41/49
Translation Scheme A Translation Scheme generates an output for each
sentence x generated by the underlying grammar byexecuting the actions in the order they appear during adepthfirst traversal of a parse tree of x.
In figure below the action { print (+) } will be performed
after the subtree for term is traversed but before child forrest_1 is visited.
rest
+ term { print ( + ) } rest_1
8/7/2019 Chap 5 SDT
42/49
Syntax-Directed Translation Schemes
Syntax-directed translation schemes are a complementary
notation to SDDs. All of the applications of SDDs discussed in previous slides
can be implemented using SDT schemes.
Typically, SDTs are implemented during parsing, withoutbuilding a parse tree.
We focus on the use of SDTs to implement two important
classes of SDDs: The underlying grammar is LR-parsable, and the SDD is
S-attributed.
The underlying grammar is LL-parsable, and the SDD is L-attributed.
8/7/2019 Chap 5 SDT
43/49
Comparison A syntax-directed translation scheme embeds program
fragments called semantic actions within productionbodies, as in
Ep E1 + T {print +}
SDDs can be more readable, and hence more useful forspecifications.
SD translation scheme can be more efficient, thus moreuseful for implementations.
L-attributed translations
S-attributed translations
8/7/2019 Chap 5 SDT
44/49
Postfix Translation Schemes SDTs with all actions at the right ends of the production
bodies are calledpostfix
SD
Ts. Example :
8/7/2019 Chap 5 SDT
45/49
SDTs With Actions Inside Productions
An action may be placed at any position within the body of
a production. It is performed immediately after all symbols to its left are
processed.
Example: desk calculator running example into an SDTthat prints the prefix form of an expression.
8/7/2019 Chap 5 SDT
46/49
SDTs With Actions Inside Productions
8/7/2019 Chap 5 SDT
47/49
Eliminating Left Recursion from SDTs When transforming the grammar, treat the actions as if they
were terminal symbols.
Example 5.17:
A pAE | FA p FR
R p ER | I
EpE1 + T{print(+);}
Ep T
Ep TR
R p + T{print(+);}R
Rp I
8/7/2019 Chap 5 SDT
48/49
Example
exprp expr+ term {print(+) }exprp expr- term {print(-) }exprp termterm p 0 {print(0) }term p 9 {print(9) }
Show SDT for the string w=
5 + 4 + 6
8/7/2019 Chap 5 SDT
49/49
Translation eg 2
Side-effects and Translation Schemes.
Do the construction as before but:
Side-effect in front of a symbol will be executed in astate when we make the move following that symbol
to another state. Side-effects on the rightmost end are executed during
reduce actions.
E p EE p E + E {print(+)}
| E * E {print(*)}
| {parenthesis++} ( E ) {parenthesis--}
| id { print(id); print(parenthesis); }
Do for example id*(id+id)$
side-effects
attachedtothesymbols
totherightofthem.
Recommended