Systems Software Charles E Hughes COP 3402 Fall

  • Slides: 18
Download presentation
Systems Software Charles E. Hughes COP 3402 – Fall 2011 Notes for Weeks 11

Systems Software Charles E. Hughes COP 3402 – Fall 2011 Notes for Weeks 11 & 12

Assign#3 Help Days#20

Assign#3 Help Days#20

Syntax Directed Translation Days#21, 22

Syntax Directed Translation Days#21, 22

Syntax Directed Defn. (SDD) • A CFG with attributes and rules • Example: –

Syntax Directed Defn. (SDD) • A CFG with attributes and rules • Example: – Production – E E 1 + T Semantic Rule E. code = E 1. code || T. code || ‘+’ • In above || is concatenation of strings • The example shows synthesized attributes – ones that flow up the parse tree 10/18/2021 © UCF EECS 4

SDD in Bison • Bison/Yacc has built-in notion of attributes referred to as $$,

SDD in Bison • Bison/Yacc has built-in notion of attributes referred to as $$, $1, etc. • In Bison, you can declare type YYSTYPE to override default of int used for yylval • Often one uses a union and the type can be referenced by union type tag $<tag>$ or $<tag>1 etc. • If you do not set $$ then default is $$ = $1 10/18/2021 © UCF EECS 5

Attributes • Attributes are synthesized when they are defined at a node labeled A

Attributes • Attributes are synthesized when they are defined at a node labeled A using attributes of the node and its children. • Attributes are inherited when they are defined at node labeled A using attributes of the node, its parent and its children (also typically allows younger siblings to pass along values to older ones. ) • Terminals are only allowed to have synthesized attributes. 10/18/2021 © UCF EECS 6

S- and L-sttributed • An SDD with only synthesized attributes is called Sattributed. S-attributed

S- and L-sttributed • An SDD with only synthesized attributes is called Sattributed. S-attributed are often used for bottom-up where they can be evaluated on the fly. • An SDD with mixed synthesized and inherited attributes is called L-attributed if it can be evaluated left-to-right and usually pre-order. L-attributed are often used for topdown. • Under certain circumstances (the right kind of dependencies) both types can be evaluated in one pass. 10/18/2021 © UCF EECS 7

S-attributed <E> <E 1> + <T> <E> <E 1> – <T> <E> <T> <T

S-attributed <E> <E 1> + <T> <E> <E 1> – <T> <E> <T> <T 1> * <F> <T> <T 1> / <F> <T> <F> – <F 1> <F> ( <E> ) <F> id <F> unsigned_integer 10/18/2021 E. val : = E 1. val + T. val {$$=$1+$3; } E. val : = E 1. val – T. val {$$ = $1 -$3; } E. val : = T. val {$$ = $1; } T. val : = T 1. val * F. val {$$ = $1*$3; } T. val : = T 1. val / F. val {$$ = $1/$3; } T. val : = F. val {$$ = $1; } F. val : = – F 1. val {$$ = -$1; } F. val : = E. val {$$ = $2; } F. val : = id. entry {$$ = $1. entry; } F. val : = unsigned_integer. val {$$=$1; } © UCF EECS 8

Annotated Parse Tree • Adds attributes to nodes • Also called attributed or decorated

Annotated Parse Tree • Adds attributes to nodes • Also called attributed or decorated • S-attributed evaluates up the tree (typically postorder traversal but any bottom-up works) • L-attributed evaluates pre-order and left to right. The pre-order allows attributes to flow from parent; left-to-right allows younger children to pass values along. 10/18/2021 © UCF EECS 9

S-attributed Evaluation • (1+3)*2 10/18/2021 © UCF EECS 10

S-attributed Evaluation • (1+3)*2 10/18/2021 © UCF EECS 10

L-attributed <E> <T> <TT> + <T> <TT 1> <TT> – <T> <TT 1> <TT>

L-attributed <E> <T> <TT> + <T> <TT 1> <TT> – <T> <TT 1> <TT> e <T> <F> <FT> * <F> <FT 1> <FT> / <F> <FT 1> <FT> e <F> – <F 1> <F> ( <E> ) <F> id <F> unsigned_integer TT. inh : = T. val; E. val : = TT. syn TT 1. inh : = TT. inh + T. val; TT. syn : = TT 1. syn TT 1. inh : = TT. inh – T. val; TT. syn : = TT 1. syn TT. syn : = TT. inh FT. inh : = T. val; E. val : = FT. syn FT 1. inh : = FT. inh F. val; FT. syn : = FT 1. syn FT 1. inh : = FT. inh / F. val; FT. syn : = FT 1. syn FT. syn : = FT. inh F. val : = – F 1. val F. val : = E. val F. val : = id. entry F. val : = unsigned_integer. val Note: inherited attribute of a node can be assigned to synthesized attribute but not vice versa. 10/18/2021 © UCF EECS 11

L-attributed Evaluation • (1+3)*2 (yellow is syn or val; blue is inh) 10/18/2021 ©

L-attributed Evaluation • (1+3)*2 (yellow is syn or val; blue is inh) 10/18/2021 © UCF EECS 12

Declaration Statements <D> <T> <L> <T> int <T> float <L> <L 1> ‘, ’

Declaration Statements <D> <T> <L> <T> int <T> float <L> <L 1> ‘, ’ id <L> id 10/18/2021 L. inh : = T. type : = integer T. type : = float L 1. inh : = L. inh; add. Type(id. entry, L. inh) © UCF EECS 13

Evaluation Order • Any order that maintains dependencies is acceptable in attribute evaluation •

Evaluation Order • Any order that maintains dependencies is acceptable in attribute evaluation • Typical approach is topological sort of dependency graph • Problem: If actions have side effects then one must be careful to not change semantics with varying orders of evaluation 10/18/2021 © UCF EECS 14

S-synthesizing a Syntax Tree <E> <E 1> + <T> <E> <E 1> – <T>

S-synthesizing a Syntax Tree <E> <E 1> + <T> <E> <E 1> – <T> <E> <T> <T 1> * <F> <T> <T 1> / <F> <T> <F> – <F 1> <F> ( <E> ) <F> id <F> num 10/18/2021 E. node : = new Node(‘+’, E 1. node, E. node) E. node : = new Node(‘–’, E 1. node, E. node) E. node : = T. node : = new Node(‘*’, T 1. node, F. node) T. node : = new Node(‘/’, T 1. node, F. node) T. node : = F. node : = new Unary. Node(minus, F 1. node) F. Node : = E. node F. node : = new Leaf. Node(ident, id. entry) F. node : = new Leaf. Node(number, num. val) © UCF EECS 15

L-synthesizing a Syntax tree <E> <T> <TT> + <T> <TT 1> <TT> – <T>

L-synthesizing a Syntax tree <E> <T> <TT> + <T> <TT 1> <TT> – <T> <TT 1> <TT> e <T> <F> <FT> * <F> <FT 1> <FT> / <F> <FT 1> <FT> e <F> – <F 1> <F> ( <E> ) <F> id <F> num 10/18/2021 E. node : = TT. syn; TT. inh : = T. node TT 1. inh : = new Node(‘+’, TT. inh, T. node; TT. syn : = TT 1. syn TT 1. inh : = new Node(‘–’, TT. inh, T. node; TT. syn : = TT 1. syn TT. syn : = TT. inh T. node : = FT. syn; FT. inh : = F. node FT 1. inh : = new Node(‘ ’, FT. inh, F. node; FT. syn : = FT 1. syn TT 1. inh : = new Node(‘/’, TT. inh, F. node; FT. syn : = FT 1. syn FT. syn : = FT. inh F. val : = – F 1. val F. node : = E. node F. node : = new Leaf. Node(ident, id. entry) F. node : = new Leaf. Node(number, num. val) © UCF EECS 16

Flattening of the Syntax Tree • Triples and quads as we defined them are

Flattening of the Syntax Tree • Triples and quads as we defined them are a form of flattening • Triples are compact but hard to move • Quads are wasteful in many cases, but easy to move, e. g. , from inside a loop to precede it when semantics are still correct 10/18/2021 © UCF EECS 17

Indirect Triples • Compromise between triples and quads • Generate triples but have a

Indirect Triples • Compromise between triples and quads • Generate triples but have a separate list that specifies which triples actually are at a particular node position 10/18/2021 © UCF EECS 18