Lec 29to31
Lec 29to31
Lec 29to31
2. Values of these attributes are evaluated by the semantic rules associated with the
production rules.
A. Syntax-Directed Definitions:
– give high-level specifications for translations
– hide many implementation details such as order of evaluation of semantic actions.
– We associate a production rule with a set of semantic actions, and we do not say when they
will be evaluated.
B. Translation Schemes:
– indicate the order of evaluation of semantic actions associated with a production rule.
– In other words, translation schemes give a little bit information about implementation
details.
July 2005 CP422 Compiler Design 3
Syntax-Directed Definitions
1. A syntax-directed definition is a generalization of a context-free grammar in which:
– Each grammar symbol is associated with a set of attributes.
– This set of attributes for a grammar symbol is partitioned into two subsets called
• synthesized and
• inherited attributes of that grammar symbol.
– Each production rule is associated with a set of semantic rules.
3. This dependency graph determines the evaluation order of these semantic rules.
4. Evaluation of a semantic rule defines the value of an attribute. But a semantic rule
may also have some side effects such as printing a value.
OR
b is an inherited attribute one of the grammar symbols in α (on the right side
of the production), and c1,c2,…,cn are attributes of the grammar symbols in the
production ( A→α ).
E.val=17 return
E.val=5 + T.val=12
digit.lexval=5 digit.lexval=3
E.val=17
E.val=5 T.val=12
digit.lexval=5 digit.lexval=3
1. Symbols E, T, and F are associated with synthesized attributes loc and code.
2. The token id has a synthesized attribute name (it is assumed that it is evaluated by the
lexical analyzer).
3. It is assumed that || is the string concatenation operator.
July 2005 CP422 Compiler Design 11
Syntax-Directed Definition – Inherited Attributes
D L.in=real
id id.entry=p
id
to entry for c
id num 4
to entry for a
July 2005 CP422 Compiler Design 15
Directed Acyclic Graphs for Expressions
a+ a*(b–c)+(b–c)*d
+ *
* d
a -
b c
July 2005 CP422 Compiler Design 16
S-Attributed Definitions
1. Syntax-directed definitions are used to specify syntax-directed translations.
3. We would like to evaluate the semantic rules during parsing (i.e. in a single pass, we will parse
and we will also evaluate semantic rules during the parsing).
stack parallel-stack
top Z Z.z
Y Y.y
X X.x top
A A.a
. . . .
July 2005 CP422 Compiler Design 18
Bottom-Up Eval. of S-Attributed Definitions (cont.)
Production Semantic Rules
L → E return print(val[top-1])
E → E1 + T val[ntop] = val[top-2] + val[top]
E→T
T → T1 * F val[ntop] = val[top-2] * val[top]
T→F
F→(E) val[ntop] = val[top-1]
F → digit
.. .. .
r 9
L→ Er T T →T *F
E→
E→
..
E+T E
T
I2: L →E r
E →E +T
+ I8: E →E+ T
T → T*F .. (
F 4
.. ..
5
T→ T*F T→ F d
..
T→ F T I3: E →T F → (E) 6
F→ (E) T →T *F F→ d
F→ d
F . *
.
I4: T →F
. I9: T →T* F
.. F
I12: T →T*F .
(
I5 : F →
E→ .. ( E)
E+T
F → (E)
F→ d (
5
..
E→ T E d
..
6
.
T→ T*F
T→
F→
F→
.. F
(E)
d
T
F
3
I10: F →(E )
E →E +T
)
+
I13: F →(E)
4 8
d
I6: F →d . (
d
5
6