Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.

Similar presentations


Presentation on theme: "Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015."— Presentation transcript:

1 Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015

2 Outline Overview Bottom-Up Syntax-Directed Translation Top-Down Syntax-Directed Translation Abstract Syntax Trees AST Design and Construction AST Structures for Left and Right Values Design Patterns for ASTs

3 Overview Syntax-directed translation –The work performed by a compiler while parsing –Syntactic actions –Semantic actions

4 Semantic Actions and Values Semantic actions –Associated code sequence that will execute when the production is applied Semantic values –For production A -> X1…Xn, a semantic value for each symbol Terminals: values originate from the scanner Nonterminals: to compute a value for A based on the values assigned to X1…Xn

5

6 Synthesized and Inherited Attributes Synthesized attributes –Attributes flow from the leaves of a derivation tree toward its root –Ex.: evaluating expressions (Fig. 7.1) Inherited attributes –Attribute values pass from parent to child –Ex.: counting the position of each x in a string (Fig. 7.2) Syntax-directed translation of most programming languages requires both

7

8 Bottom-Up Syntax-Directed Translation A->X1…Xn –For yacc and Bison Xi: $i A: $0 –For JavaCUP X:val Two stacks in bottom-up parser – Syntactic stack (parse stack): to manipulate terminal and nonterminal symbols (in Chap. 6) – Semantic stack : to manipulate semantic values of these symbols

9 Example Ex.: 4 3 1 $ –(Fig. 7.3) Semantic values for nonterminal symbols: computed by semantic actions Semantic values for terminal symbols: established by the scanner Ex.: o 4 3 1 $ –Base-8 (octal): (Fig. 7.4)

10 PRINT

11

12 Drawbacks of the grammar in Fig. 7.4(a): –Nonterminal Digs generates a string of decimal digits, even in cases of base-8 Inconvenient to enforce the restriction –Semantic actions are only allowed on reductions Consider the parse tree in Fig. 7.4(b), multiplication by 10 or 8 is unknown at that point –Problem: the information required at a semantic action is not available from below

13 A number of approaches for remedying this problem –Rule cloning –Forcing semantic actions –Aggressive grammar restructuring When a grammar is modified, it cannot in general be proved that the language was not changed –Grammar changes should be planned and carried out in small steps –After each step, regression tests should ensure the parser accepts and rejects the proper sets of strings

14 Rule Cloning A similar sequence of input symbols should be treated differently depending on the context –Ex.: (Fig. 7.5) –Redundancy in productions

15

16 Forcing Semantic Actions Introducing unit productions of the form A  X –Semantic actions can be associated with the reduction of A  X –If a semantic action is desired between two symbols Xm and Xn, a production of the form A  can be introduced –Ex.: (Fig. 7.6)

17

18 Another example for assigning the base with the symbol x –(Fig. 7.7)

19

20 Aggressive Grammar Restructuring Reasons to avoid using global variables –Grammar rules are often invoked recursively, and the global variables can introduce unwanted interactions –Global variables can make semantic actions difficult to write and maintain –Global variables may require setting or resetting

21 More robust solution –Sketch the parse tree without global variables –Revise the grammar to achieve the desired parse tree –Verify the revised grammar is still suitable for parser construction (e.g. LALR(1)) –Verify the revised grammar still generates the same language –(Fig. 7.8) Keep the base in the semantic values Propagate the value up the parse tree

22

23 Top-Down Syntax-Directed Translation Using the recursive-descent parsers Semantic actions can be written directly into the parser –Ex.: Lisp-like expressions (Fig. 7.9) ( plus 31 ( prod 10 2 20 ) ) $ Inherited values: parameters passed into a method Synthesized values: returned by methods –(Fig. 7.10)

24

25

26 Abstract Syntax Trees The central data structure for all post- parsing activities –AST must be concise –AST must be sufficiently flexible Concrete vs. abstract trees –(Fig. 7.3 & 7.4) –(Fig. 7.11)

27

28 An Efficient AST Data Structure Considering –AST is typically constructed bottom-up –Lists of siblings are typically generated by recursive rules –Some AST nodes have a fixed number of children, but some may require an arbitrarily large number of children (Fig. 7.12)

29

30 Each node is of constant size –Each node points to its next (right) sibling, and also its leftmost sibling –Each node n points to its leftmost child –Each node points to its parent

31 Infrastructure for Creating ASTs Methods to create and manage AST nodes (Fig. 7.13) –MakeNode(t) MakeNode(int n), MakeNode(Symbol s), MakeNode(Operator o), MakeNode() –X.MakeSiblings(y) –X.AdoptChildren(y) –MakeFamily(op, kid1, … kidn)

32

33 AST Design and Construction Important forces that influence the design of an AST –It should be possible to unparse (i.e. reconstitute) an AST AST must hold sufficient information –The implementation of an AST should be decoupled from the essential information represented within the AST interoperability –Different views from different phases of a compiler, no single class hierarchy for all purposes Class structure of AST nodes is designed for the expediency of constructing the AST

34 Process of the design of an appropriate AST structure –An unambiguous grammar for L is devised –An AST for L is devised –Semantic actions are placed in the grammar to construct the AST –Passes of the compiler are designed –Ex. (Fig. 7.14)

35

36 Design Assignment statements –Fig. 7.15(a) If statements –Fig. 7.15(c) While statements –Fig. 7.15(d) Block of statements –Fig. 7.15(e) Plus operations –Fig. 7.15(b)

37

38

39 Construction Semantic actions are added into the parse to construct the AST structure –(Fig. 7.17) –Example: (Fig. 7.18 & 7.19) if (x + y) { while (z) z = z +1 od; x = 8 } else z = 7 fi $

40

41

42

43 AST Structure for Left and Right Values Identifier –Value –Location (address) –Ex: x=y y: value of y ( right value, or R-value ) x: location of x ( left value, or L-value ) –Ex: *p = 0; x = &p;

44 Given the location of an identifier, we can always find the value by indirection –But we cannot find an identifier’s location from its value We always regard an identifier as representing its location –Explicit deref nodes will be placed to show exactly where indirection occurs –(Fig. 7.20, 7.21, & 7.22)

45

46

47 Design Patterns for ASTs (omitted)

48

49

50

51

52 Thanks for Your Attention!


Download ppt "Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015."

Similar presentations


Ads by Google