syntax-directed translation two approaches are distinguished 1.a syntax-directed definition (sdd)...

11
Syntax-Directed Translation Two approaches are distinguished 1. A syntax-directed definition (SDD) associates a semantic rule with each grammar production, the rule states how attributes are calculated conceptually, (and for SDTs too) each node may have multiple attributes perhaps a struct/record/dictionary is used to group many attributes attributes may be concerned e.g. with data type, numeric value, symbol identification, code fragment, memory address, machine register choice 2. A syntax-directed translation scheme (SDT) uses semantic actions embedded anywhere in the bodies (right hand sides) of productions actions may perform arbitrary computations, such as appending output they are carried out in left-to-right order during parsing. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 1

Upload: andra-dickerson

Post on 13-Jan-2016

252 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Syntax-Directed Translation

Two approaches are distinguished

1. A syntax-directed definition (SDD) associates a semantic rule with each grammar production, the rule states how attributes are calculated conceptually, (and for SDTs too) each node may have multiple attributes

– perhaps a struct/record/dictionary is used to group many attributes attributes may be concerned e.g. with data type, numeric value, symbol

identification, code fragment, memory address, machine register choice

2. A syntax-directed translation scheme (SDT) uses semantic actions embedded anywhere in the bodies (right hand sides) of productions actions may perform arbitrary computations, such as appending output they are carried out in left-to-right order during parsing.

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 1

Page 2: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Compare/Contrast SDD & SDT

• SDDs have a declarative flavour the grammar writer specifies what calculations have to be done the order of evaluation is unspecified

– it must be fixed by the parser (or parser generator) the grammar is more readable

• SDTs have a more procedural flavour the grammar writer specifies both

– the calculations that have to be done

– at what time they must be done the grammar is often more efficiently parsable

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 2

Page 3: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Synthesized and Inherited Attributes

• For a grammar symbol X and its attribute a, use notation “X.a”

• Each attribute X.x at a parse tree node for symbol X may be

synthesized, meaning that its value is obtained

– from attributes of child nodes in the parse tree,

– or from the lexical analyzer,

– or from other attributes of the same node in the parse tree

inherited, meaning that its value is obtained

– from the parent node in the parse tree,

– or from sibling nodes in the parse tree

– v. useful when there is mismatch between grammar

for parsing, and “abstract syntax” for translation

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 3

Page 4: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Restricted classes of grammars

• The most general – least restrictive – kind of syntax-directed translation involves building a parse tree explicitly, then “walking” it carrying out the actions in the productions.

• Two special cases allow actions to be carried out during parsing, with no explicit parse tree having to be built

L-attributed translations, grammars (‘L’ for left-to-right)

– inherited attributes are allowed provided they can be calculated in left-to-right fashion

S-attributed translations, grammars (‘S’ for Synthesized)

– only synthesized attributes are allowed

– easy for a bottom-up parser to handle

• The term “attribute grammar” means a grammar with attributes but its rules or actions may not have side effects

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 4

Page 5: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Syntax-Directed Definitions (SDDs)

• A SDD is a context-free grammar, plus attributes, and rules attached to the productions of the grammar stating how attributes are computed

• Desk calculator’s Arithmetic Expression example:– all attributes in this example are synthesized– val and lexval attribute names are purposely different– subscripts distinguish occurrences of the same grammar symbol– practical rules may also have side-effects

(such as printing, manipulating a symbol table)

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 5

L ::= E \nE ::= E1 + TE ::= TT ::= T1 * FT ::= FF ::= ( E )F ::= digit

L.val = E.valE.val = E1.val + T.valE.val = T.valT.val = T1.val * F.valT.val = F.val}F.val = E.valF.val = digit.lexval

Page 6: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

SDD applied to “9+3*5\n”

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 6

L ::= E \nE ::= E1 + TE ::= TT ::= T1 * FT ::= FF ::= ( E )F ::= digit

L.val=24

E.val=24

E.val=9

T.val=9

F.val=9

digit.lexval=9

T.val=15+

*T.val=3 F.val=5

\n

This is an annotated parse treeNote that no complete parse tree need actually be constructed.

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

Page 7: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

SDD applied to “9+3*5\n”

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 7

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

L.val=24

E.val=24

E.val=9

T.val=9

F.val=9

digit.lexval=9

T.val=15

T.val=3 F.val=5*

+

\n

This is an annotated parse treeNote that no complete parse tree need actually be constructed.

L ::= E \nE ::= E1 + TE ::= TT ::= T1 * FT ::= FF ::= ( E )F ::= digit

Page 8: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Evaluation order

• Values of synthesized attributes may be calculated in any order

• For a S-attributed SDD, any bottom-up order suffices

• Where inherited attributes are allowed, order of evaluation is important it is possible to write rules for which no order of evaluation is possible circularity – NP-hard to detect if a set of rules could give rise to circularity In a L-attributed SDD, dependencies always go left-to-right, so no circularity is

possible

• A “Dependency Graph” depicts the flow of information between attributes in a particular parse tree

• A “topological sort” of this graph produces a possible sequential order of evaluation – if there is one, if the graph is not cyclic

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 8

Page 9: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Usefulness of inherited attributes

• There can be tension between the design of a grammar for parsing purposes, and its use in syntax-directed translation

elimination of left recursion, to allow top-down parsing covering syntactic sugar constructs – mismatch of surface & abstract syntax

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 9

The C syntax int[2][3]reads as“an array of two arrays of three integers”

array

array

3 int

2

T ::= B CB := intB := floatC := [ num ] C1

C :=

T.t=C.t; C.b=B.tB.t=‘integer’B.t=‘float’C.t=array(num.val, C1.t); C1b=C.b)C.t=C.b

Page 10: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Utilising SDDs

• SDDs can strike a balance between side-effect-free “attribute grammars” where any order of evaluation of rule

elements is permitted consistent with dependency graph translation schemes whose actions may contain arbitrary side-effects but must

be evaluated in strict left-to-right order

• This balance may be achieved in either of two ways permit only incidental side-effects which do not have any impact upon the

ordering of evaluation of rule elements somehow constrain the order of evaluation, effectively adding edges to the

dependency graph, so that significant side-effects may be allowed

– eg desk calculator: printing info; compiler: add type info to symbol table

• It is common to use SDD to produce an actual parse tree, which may be used as an intermediate representation for a tree-walking translator

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 10

Page 11: Syntax-Directed Translation Two approaches are distinguished 1.A syntax-directed definition (SDD) associates a semantic rule with each grammar production,

Building a parse tree using a SDD

http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction 11

E := E1 + TE := E1 – TE := TT := ( E )T := idT := num

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(‘ident’, id.entrynumber)T.node= new Leaf(‘number’, num.lexval)

fred – george * 13 =>

-

ident 1 *

ident 2 number 13