Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax Directed Definition and Syntax directed Translation

Similar presentations


Presentation on theme: "Syntax Directed Definition and Syntax directed Translation"— Presentation transcript:

1 Syntax Directed Definition and Syntax directed Translation

2 Compilation in a Nutshell 1
Source code (character stream) if (b == 0) a = b; Lexical analysis Token stream if ( b == ) a = b ; Parsing if == = ; Abstract syntax tree (AST) b a b if Semantic Analysis boolean int == = ; Decorated AST int b int 0 int a lvalue int b

3 Compilation in a Nutshell 2
if boolean int == = ; int b int 0 int a lvalue int b Intermediate Code Generation CJUMP == MEM CONST MOVE NOP Optimization + MEM MEM fp 8 + + fp 4 fp 8 CJUMP == Code generation CX CONST MOVE NOP CMP CX, 0 CMOVZ DX,CX 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: EE1+T E.code=E1.code |T.code|’+’ More readable Useful for specification Syntax-directed translation (Translation Scheme) Productions with semantic actions Ex: EE1+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 EE1+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 TF T’ T’.inh=F.val T.val=T’.syn Terminals can have synthesized attributes, but not inherited attributes

7 Example Production Semantic Rules
1) LE n 2) EE1+T 3) ET 4) TT1*F 5) TF 6) F(E) 7) Fdigit L.val=E.val E.val=E1.val+T.val E.val=T.val T.val=T1.val*F.val T.val=F.val F.val=E.val F.val=digit.lexval

8 Annotated Parse Tree for 3*5+4 n
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 Annotated Parse Tree for 3*5+4 n L E.val=19 n E.val=15 + T.val=4 T.val=15 F.val=4 T.val=3 F.val=5 digit.lexval=4 * F.val=3 digit.lexval=5 digit.lexval=3

9 Class Exercise S (L)|a L 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 grammar
A grammar to evaluate signed binary numbers POS, VAL, and NEG are attributes of the non-terminal (node) they are attached to

12 Example LIST0 BIT LIST1 Note:
pos val LIST0 LIST1 BIT Note: semantic rules define a partial dependency graph structure can be used to derive characteristics of generated total dependency graphs

13 Attribute grammars The attribute dependency graph Evaluation order
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 rules This order depends on both the grammar and the input string

14 Example attribute grammar
Example Parse tree for -101 NUM val SIGN pos val LIST neg pos val pos val LIST BIT pos val LIST BIT pos val pos val BIT - 1 1

15 Example grammar dependency graph
NUM val SIGN pos val LIST0 neg pos val pos val LIST1 BIT2 val and neg are synthesized attributes pos is an inherited attribute LIST0.pos is an inherited attribute with an empty dependency set. pos val pos val LIST2 BIT1 pos val BIT0 - 1 1

16 Attribute grammars - Evaluate in this order Yields NUM.val: -5
A topological order for the example 1. SIGN.neg 2. 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.val 14. NUM.val NUM val SIGN pos val LIST0 neg pos val pos val LIST1 BIT2 pos val pos val LIST2 BIT1 pos val BIT0 - 1 1 Evaluate in this order Yields NUM.val: -5

17 Example grammar final result
NUM val :-5 pos: 0 val: 5 SIGN LIST0 neg : T LIST1 pos: 1 val: 4 pos: 0 val: 1 BIT2 pos: 2 val: 4 LIST2 BIT1 pos: 1 val: 0 BIT0 pos: 2 val: 4 The evaluation process is also called decorating the parse tree - 1 1

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 AX1X2…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) TF T’ 2) T’*F T1’ 3) T’ 4) Fdigit T’.inh=F.val T.val=T’.syn T1’.inh=T.inh*F.val T’.syn=T1’.syn T’.syn=T’.inh F.val=digit.lexval

21 Example: Annotated parse tree for 3*5
T.val=15 F.Val=3 T’.inh=3 T’.syn=15 digit.lexval=3 T1’.inh=15 T1’.syn=15 * F.val=5 digit.lexval=5

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 (1 + 2 + (3 + 4)) + 5 S E + S ( S ) E + + 5 E + S 5 1

27 Construction of AST -- Example: S-Attributed Grammar
Production Semantic Rules 1) EE1+T 2) EE1-T 3) ET 4) T(E) 5) Tid 6) Tnum E.node=new Node(‘+’, E1.node, T.node) E.node=new Node(‘-’, E1.node, T.node) E.node=T.node T.node=E.node T.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 E nptr T nptr T nptr E - id T nptr id +
To entry for c id num 4 id To entry for a

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: LE n { 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; }

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 BX {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
BB1B2|B1 sub B2|(B1)|text

38 SDD for Typesetting Boxes
Production Semantic Rules 1) SB 2) BB1B2 3) BB1 sub B ) B(B1) 5) B text B.ps=10 B1.ps=B.ps,B2.ps=B.ps B.ht=max(B1.ht,B2.ht) B.dp=max(B1.dp,B2.dp) B1.ps=B.ps,B2.ps=0.7*B.ps B.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


Download ppt "Syntax Directed Definition and Syntax directed Translation"

Similar presentations


Ads by Google