Syntax Directed Definition and Syntax directed Translation
description
Transcript of Syntax Directed Definition and Syntax directed Translation
1
Syntax Directed Definition and
Syntax directed Translation
2
Compilation in a Nutshell 1
Source code(character stream)
Lexical analysis
Parsing
Token stream
Abstract syntax tree(AST)
Semantic Analysis
if (b == 0) a = b;
if ( b ) a = b ;0==
if==
b 0
=
a b
if
==
int b int 0
=
int alvalue
int b
boolean
Decorated ASTint
;
;
3
Compilation in a Nutshell 2
Intermediate Code Generation
Optimization
Code generation
if
==
int b int 0
=
int alvalue
int b
boolean int;
CJUMP ==
MEM
fp 8
+
CONST MOVE
0 MEM MEM
fp 4 fp 8
NOP
+ +
CJUMP ==
CONST
MOVE
0 DX
CX
NOP
CX CMP CX, 0
CMOVZ DX,CX
4
Outline
• Syntax-Directed Definitions• Evaluation Orders for SDD’s• Applications of Syntax-Directed
Definition• Syntax-Directed Translation
5
Definitions
• Syntax-directed definition (Attribute Grammar)– Productions with semantic rules
• Ex: EE1+T E.code=E1.code |T.code|’+’
– More readable– Useful for specification
• Syntax-directed translation (Translation Scheme)– Productions with semantic actions
• Ex: EE1+T { print ’+’ }
– More efficient– Useful for implementation
6
Syntax-Directed Definitions• SDD: a context-free grammar with attributes and
rules– Attributes: for grammar symbols– Rules: for productions
• Attributes for nonterminals– Synthesized attribute: attributes that are passed up a
parse tree, i.e., the LHS attribute is computed from the RHS attributes in the production rules
• EE1+T E.val=E1.val+T.val– Inherited attribute: attributes that are passed down a
parse tree, i.e., the RHS attributes are derived from its LHS attributes or other RHS attributes in the production rules
• TF T’ T’.inh=F.val T.val=T’.syn
• Terminals can have synthesized attributes, but not inherited attributes
7
Example
Production Semantic Rules
1) LE n2) EE1+T3) ET4) TT1*F5) TF6) F(E)7) Fdigit
L.val=E.valE.val=E1.val+T.valE.val=T.valT.val=T1.val*F.valT.val=F.valF.val=E.valF.val=digit.lexval
8
Annotated Parse Tree for 3*5+4 n
digit.lexval=3
F.val=3
T.val=3 *
digit.lexval=5
F.val=5
T.val=15
E.val=15 +
digit.lexval=4
F.val=4
T.val=4
E.val=19 n
L
Prod. Semantic Rules L→En 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
9
Class Exercise
S (L)|aL L,S|S• Write an SDD to print the number of
parenthesis pair for an input string.• Write an SDD to print the maximum
parenthesis depth.
10
Outline
• Syntax-Directed Definitions• Evaluation Orders for SDD’s• Applications of Syntax-Directed
Definition• Syntax-Directed Translation
11
Example attribute grammarExample attribute grammar
A grammar to evaluate signed binary numbersProduction Semantic Rules
1 NUM ::= SIGN LIST LIST.pos 0NUM.val if SIGN.neg then -LIST.val else LIST.val
2 SIGN ::= + SIGN.neg false3 SIGN ::= - SIGN.neg true4 LIST ::= BIT BIT.pos LIST.pos
LIST.val BIT.val5 LIST0 ::= LIST1 BIT LIST1.pos LIST0.pos + 1
BIT.pos LIST0.posLIST0.val LIST1.val + BIT.val
6 BIT ::= 0 BIT.val 07 BIT ::= 1 BIT.val 2BIT.pos
POS, VAL, and NEG are attributes of the non-terminal (node) they are attached to
POS, VAL, and NEG are attributes of the non-terminal (node) they are attached to
12
ExampleExampleProduction Semantic Rules
5 LIST0 ::= LIST1 BIT LIST1.pos LIST0.pos + 1BIT.pos LIST0.posLIST0.val LIST1.val + BIT.val
pos
pos
pos
valval
val
LIST0
LIST1BIT
Note:• semantic rules define a partial dependency graph• structure can be used to derive characteristics of generated total dependency graphs
13
Attribute grammarsAttribute grammarsThe attribute dependency graph
• nodes represent attributes• edges represent the flow of values• graph is specific to parse tree• size is related to parse tree's size• can be built alongside parse tree
The dependency graph must be acyclic
Evaluation order• Topological sort of the dependency graph to order
attributes• Topological order: a linear ordering of the nodes of a directed
acyclic graph such that each node comes before all nodes to which it has outbound edges
• using this order, evaluate the rulesThis order depends on both the grammar and the
input string
14
Example attribute grammarExample attribute grammarExample Parse tree for -101
NUM
LIST
BIT
val
LIST
LIST BIT
BIT
negposval
posval
posval
posval
posval
posval
SIGN
- 1 0 1
15
Example grammar Example grammar dependency graphdependency graph
• val and neg are synthesized attributes• pos is an inherited attribute
LIST0.pos is an inherited attribute with an empty dependency set.
NUM
LIST0
BIT2
val
LIST1
LIST2BIT1
BIT0
negposvalSIGN
- 1 0 1
posval
posval
posval
posval
posval
0
16
Attribute grammarsAttribute grammarsA topological order for the example
1. SIGN.neg2. LIST0.pos
3. LIST1.pos
4. LIST2.pos
5. BIT0.pos
6. BIT1.pos
7. BIT2.pos
8. BIT0.val
9. LIST2.val
10. BIT1.val
11. LIST1.val
12. BIT2.val
13. LIST0.val14. NUM.val
Evaluate in this orderYields NUM.val: -5
NUM
LIST0
BIT2
val
LIST1
LIST2BIT1
BIT0
negposvalSIGN
- 1 0 1
posval
posval
posval
posval
posval
0
17
Example grammar final Example grammar final resultresult
NUM
LIST0
BIT2
val :-5
LIST1
LIST2BIT1
BIT0
neg : TSIGN
- 1 0 1
pos: 2
val: 4
0
pos: 2
val: 4
pos: 1
val: 4
pos: 1
val: 0
pos: 0val: 5
pos: 0val: 1
The evaluation process is also called
decorating theparse tree
18
S-Attributed SDD
• An SDD is S-attributed if every attribute is synthesized– We can evaluate its attributes in any
bottom-up order of the nodes of the parse tree• A postorder traversal• Postorder(N) {
for (each child C of N, from the left) postorder(C); evaluate the attributes associated with node N;}
19
L-Attributed SDD
• L: dependency-graph edges can go from left to right, but not from right to left– Each attribute must be either:
• Synthesized, or• Inherited, but with the rules limited as follows.
Suppose production AX1X2…Xn and inherited attribute Xi.a computed by a rule which may only use:
– Inherited attributes associated with head A– Either inherited or synthesized attributes associated
with X1X2…Xi-1
– Either inherited or synthesized attributes associated with Xi, in a way that there’s no cycle
20
L-Attributed example
Production Semantic Rules
1) TF T’
2) T’*F T1’
3) T’
4) Fdigit
T’.inh=F.valT.val=T’.synT1’.inh=T.inh*F.valT’.syn=T1’.synT’.syn=T’.inh
F.val=digit.lexval
21
Example: Annotated parse tree for 3*5
T.val=15
F.Val=3 T’.inh=3T’.syn=15
F.val=5 T1’.inh=15T1’.syn=15
digit.lexval=5
*digit.lexval=3
22
L-Attributed example
• Any SDD containing the following production and rules cannot be L-attributed.
A B C A.s = B.b; B.i = f(C.c, A.s)
23
Outline
• Syntax-Directed Definitions• Evaluation Orders for SDD’s• Applications of Syntax-Directed
Definitions• Syntax-Directed Translation
24
Applications of Syntax-Directed Definitions
• Type checking• Intermediate-code generation• Construction of abstract syntax trees
25
Abstract Syntax Tree
• An abstract syntax tree is the procedure’s parse tree with the nodes for most non-terminal symbols removed
E.g., “a + 3 * b”
26
Creating the AST
++ 5
1 +
2 +
3 4
(1 + 2 + (3 + 4)) + 5S
E + S
( S ) E
E + S 5
E + S1
2 E
( S )
E + S
E3 4
27
Construction of AST-- Example: S-Attributed
GrammarProduction Semantic Rules1) EE1+T2) EE1-T3) ET4) T(E)5) Tid6) Tnum
E.node=new Node(‘+’, E1.node, T.node)E.node=new Node(‘-’, E1.node, T.node)E.node=T.nodeT.node=E.nodeT.node=new Leaf(id, id.entry)T.node=new Leaf(num, num.val)• Ex: a-4+c
28
a - 4 + c AST construction
E nptr T nptr
E nptr
To entry for a
E
T nptr
id
- T nptr id
id-
id num 4
+
To entry for c
29
Class Exercise
• Build the parse tree and abstract parse tree for ((a)+(b))
30
Outline
• Syntax-Directed Definitions• Evaluation Orders for SDD’s• Applications of Syntax-Directed
Definition• Syntax-Directed Translation
31
Syntax-Directed Translation
• SDT: a context-free grammar with semantic actions embedded with production bodies– Complementary to SDD– Actions performed by a preorder traversal
of the parse tree– Can be used to implement two classes of
SDD’s• LR-parsable grammar: S-attributed SDD• LL-parsable grammar: L-attributed SDD
32
Postfix Translation
• Postfix SDT’s: SDT’s with all actions at the right ends of the production bodies– Ex:
• LE n { print(E.val); }EE1+T { E.val=E1.val+T.val; }ET { E.val=T.val; }TT1*F { T.val=T1.val*F.val; }TF { T.val=F.val; }F(E) { F.val=E.val; }Fdigit { F.val=digit.lexval; }
33
Parser-Stack Implementation of Postfix SDT’s
• Postfix SDT’s can be implemented during LR parsing by executing the actions when reductions occur– To place the attributes along with the
grammar symbols on the stack (Fig. 5.19)
34
5.4.2 Parser-Stack Implementation of Postfix
SDT’s• Example 5.15:
– Rewrite the actions of the desk-calculator so that they manipulate the parser stack explicitly.
35
SDT’s with Actions inside Productions
• Actions may be placed at any position in the body of productions– They are performed immediately after
all symbols to its left are processed– BX {a} Y
• a is done after we have recognized X• In bottom-up parse, perform a as soon as X
appears on top of the stack• In top-down parse, perform a just before we
expand Y
36
SDT’s for L-Attributed SDD
• Rules for turning L-attributed SDD into SDT– Embed the actions that computes the
inherited attributes for nonterminal A immediately before A in the body of production
– Place the actions that computes a synthesized attribute for the head of a production at the end of the body
37
Example
• Ex: grammar for boxes in typesetting language Eqn– BB1B2|B1 sub B2|(B1)|text
38
SDD for Typesetting Boxes
Production Semantic Rules1) SB2) BB1B2
3) BB1 sub B2
4) B(B1)
5) B text
B.ps=10B1.ps=B.ps,B2.ps=B.psB.ht=max(B1.ht,B2.ht)B.dp=max(B1.dp,B2.dp)
B1.ps=B.ps,B2.ps=0.7*B.psB.ht=max(B1.ht,B2.ht-0.25*B.ps)B.dp=max(B1.dp,B2.dp+0.25*B.ps)
B1.ps=B.ps, B.ht=B1.ht, B.dp=B1.dp
B.ht=getHt(B.ps,text.lexval)B.dp=getDp(B.ps,text.lecval)
39
SDT for Typesetting Boxes