0% found this document useful (0 votes)
56 views12 pages

Syntax-Directed Translation: ASU Textbook Chapter 5

This document discusses syntax-directed translation, which is a compilation process driven by syntax. It attaches semantic attributes to the nodes of a parse tree based on grammar productions. Syntax-directed definitions specify attributes for grammar symbols and semantic rules to compute attribute values. There are synthesized attributes computed from children and inherited attributes computed from parents. Attribute grammars are grammars with syntax-directed definitions that cannot have side effects. The order of evaluating attributes must respect dependencies between attributes. Bottom-up and top-down evaluation algorithms are designed for grammars with inherited attributes. Implementation involves marking a stack to indicate the current production during parsing.

Uploaded by

Naveen Prakash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views12 pages

Syntax-Directed Translation: ASU Textbook Chapter 5

This document discusses syntax-directed translation, which is a compilation process driven by syntax. It attaches semantic attributes to the nodes of a parse tree based on grammar productions. Syntax-directed definitions specify attributes for grammar symbols and semantic rules to compute attribute values. There are synthesized attributes computed from children and inherited attributes computed from parents. Attribute grammars are grammars with syntax-directed definitions that cannot have side effects. The order of evaluating attributes must respect dependencies between attributes. Bottom-up and top-down evaluation algorithms are designed for grammars with inherited attributes. Implementation involves marking a stack to indicate the current production during parsing.

Uploaded by

Naveen Prakash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Syntax-Directed Translation

ASU Textbook Chapter 5

Tsan-sheng Hsu

[email protected]

http://www.iis.sinica.edu.tw/~tshsu

1
What is syntax-directed translation?
Definition:
• The compilation process is driven by the syntax.
• The semantic routines perform interpretation based on the syntax
structure.
• Attaching attributes to the grammar symbols.

• Values for attributes are computed by semantic rules associated with


the grammar productions.

Compiler notes #4, Tsan-sheng Hsu, IIS 2


Example: Syntax-directed translation
Example in a parse tree:
• Annotate the parse tree by attaching semantic attributes to the nodes
of the parse tree.
• Generate code by visiting nodes in the parse tree in a given order.
• Input: y := 3 ∗ x + z

:= :=

id + id +
(y)
* id * id
(z)
const id const id
(3) (x)
parse tree annotated parse tree

Compiler notes #4, Tsan-sheng Hsu, IIS 3


Syntax-directed definitions (1/2)
Each grammar symbol is associated with a set of attributes.
• Synthesized attribute : values computed from its children or associ-
ated with the meaning of the tokens.
• Inherited attribute : values computed from parent and/or siblings.
Format for writing syntax-directed definitions.
Production Semantic rules
L→E print(E.val)
E → E1 + T E.val := E1.val + T.val
E→T E.val := T.val
T → T1 ∗ F T.val := T1.val ∗ F.val
T →F T.val := F.val
F → (E) F.val := E.val
F → digit F.val := digit.lexval
. E.val is one of the attributes of E .
. To avoid confusion, recursively defined nonterminals are numbered on the
LHS.

Compiler notes #4, Tsan-sheng Hsu, IIS 4


Syntax-directed definitions (2/2)
It is always possible to rewrite syntax-directed definitions using
only synthesized attributes, but the one with inherited attributes
is easier to understand.
• Use inherited attributes to keep track of the type of a list of variable
declarations.
. int i, j
. D → TL . D → L id

• Reconstruct the tree: . T → int | char . L → L id, | T


. L → L, id | id . T → int | char

D D
T L L j

int L , j L i ,

T
i
int

Compiler notes #4, Tsan-sheng Hsu, IIS 5


Attribute grammars (1/2)
Attribute grammar: a grammar with syntax-directed definitions
such that functions used cannot have side effects .
• Side effect: change values of others not related to the return values of
functions themselves.
S-attributed definition : a syntax-directed definition that uses
synthesized attributed only.
• A parse tree can be represented using a directed graph.
• A post-order traverse of the parse tree can properly evaluate gram-
mars with S-attributed definitions.
• Bottom-up evaluation.

Compiler notes #4, Tsan-sheng Hsu, IIS 6


Attribute grammars (2/2)
13 L 14
E.val = 19 return
8 12
9
E.val = 15 + T.val = 4
7 11
T.val = 15
3 6 F.val = 4
4 10
T.val = 3 * F.val = 5
2 digit.lexval = 4
5
F.val = 3 digit.lexval = 5
1
digit.lexval = 3

Example of an S-attributed definition: 3 ∗ 5 + 4 return


L-attributed definition :
• Each attribute in each semantic rule for the production A → X1 · · · Xn is
either a synthesized attribute or an inherited attribute Xj depends only
on the inherited attribute of A and/or the attributes of X1, . . . , Xj−1.
• Independent of the evaluation order.
• Every S-attributed definition is an L-attributed definition.

Compiler notes #4, Tsan-sheng Hsu, IIS 7


Order of evaluation
Order of evaluating attributes is important.
General rule for ordering:
• Dependency graph :
. If attribute b needs attributes a and c, then a and c must be evaluated
before b.
. Represented as a directed graph without cycles.
. Topologically order nodes in the dependency graph as n1, n2, . . . , nk
such that there is no path from ni to nj with i > j .

:= :=

id + id +
(y) (y)
* id * id
(z) (z)
const id const id
(3) (x) (3) (x)

Compiler notes #4, Tsan-sheng Hsu, IIS 8


Orders for L-attributed definitions
For grammars with L-attributed definitions, special evaluation
algorithms must be designed.
Bottom-up evaluation of L-attributed grammars.
• Can handle all LL(1) grammars and most LR(1) grammars.
• All translation actions are taken at the right end of the production.
Key observation: when a bottom-up parser reduces by the
production A → XY , by removing X and Y from the top of the
stack and replacing them by A, X.s (the synthesized attribute
of X) is on the top of the stack and thus can be used to
compute Y.in (the inherited attribute of Y ).

Compiler notes #4, Tsan-sheng Hsu, IIS 9


Example for L-attributed definitions
• D → T {L.in := T.type} L
• T → int {T.type := integer}
• T → real {T.type := real}
• L → {L1.in := L.in} L1, id {addtype(id.entry, L.in)}
• L → id {addtype(id.entry, L.in)}

Parsing and dependency graph:


input stack production used
int p, q, r
p, q, r int
p, q, r T T → int
2 D 10
in
, q, r T p
type T L
, q, r T L L → id 7 9
1 in
q, r T L ,
int L , 8 r
,r T L ,q 4 6
,r T L L → L, id 5
in L , q
r T L ,
T L ,r 3
T L L → L, id p
D D → TL

Compiler notes #4, Tsan-sheng Hsu, IIS 10


Implementation
Information contained in the stack can be used by replacing
special markers to mark the production we are currently in.
production semantic rules S
S → aAC C.in := A.s
• Example 1: S → bABC C.in := A.s
C→c C.s := · · · b A B C
··· ··· .s .in
Same rule for the first two productions. It is difficult to tell which one
and to find the position of A in the stack in each case.
production semantic rules S
S → aAC C.in := A.s
S → bABM C M.in := A.s; b B M C
• Example 2:
A
C.in := M.s
C→c C.s := · · · .s .in .s .in
M → M.s := M.in ε
··· ···
A is always one place below in the stack.
Markers can also be used to perform error checking and other
intermediate semantic actions.
Compiler notes #4, Tsan-sheng Hsu, IIS 11
Limitation
Limitation of syntax-directed definitions: Without using global
data to create side effects, some of the semantic actions cannot
be performed.
Example:
• Checking whether a variable is defined before its usage.
• Checking the type and storage address of a variable.
• Checking whether a variable is used or not.
Need to use a symbol table: global data to show side effects of
semantic actions.
YACC can be used to implement syntax-directed translations.
Common approach:
• A program with too many global variables is difficult to understand and
maintain.
• Restrict the usage of global variables to essential items and use them
as objects.
. Symbol table.
. Labels for GOTO’s.
. Forwarded declarations.
• Use syntax-directed definitions as much as you can.

Compiler notes #4, Tsan-sheng Hsu, IIS 12

You might also like