Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.

Similar presentations


Presentation on theme: "Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End."— Presentation transcript:

1

2 Overview of Previous Lesson(s)

3 Over View 3 Model of a Compiler Front End

4 Over View..  Analysis is organized around the "syntax" of the language to be compiled.  The syntax of a programming language describes the proper form of its programs.  The semantics of the language defines the programs actual meaning. 4

5 Over View...  Context Free Grammar is used to specify the syntax of the language.  A grammar describes the hierarchical structure of most programming language constructs.  It has components  A set of tokens (terminal symbols)  A set of nonterminals  A set of productions  A designated start symbol 5

6 Over View...  Given a CF grammar determining the set of all strings generated by the grammar is known as derivation.  Begin with the start symbol  In each step, replace one nonterminal in the current sentential form with one of the right-hand sides of a production for that nonterminal 6

7 Over View...  Derivation for 9 – 5 + 2 listStart Symbol  list + digitP-1  list - digit + digitP-2  digit - digit + digitP-3  9 - digit + digitP-4  9 - 5 + digitP-4  9 - 5 + 2P-4  This is an leftmost derivation, because we replaced the leftmost nonterminal. 7

8 Over View...  Parsing is the problem of taking a string of terminals and figuring out how to derive it from the start symbol of the grammar.  Given a CFG, a parse tree according to the grammar is a tree with the following properties:  The root is labeled by the start symbol.  Each leaf is labeled by a terminal or by ɛ.  Each interior node is labeled by a nonterminal.  If A  X1 X2 … Xn is a production, then node A has immediate children X1, X2, …, Xn where Xi is a (non)terminal or . 8

9 Over View... 9 Parse tree of the string 9-5+2 using grammar G list digit 9-5+2 list digit The sequence of leafs is called the yield of the parse tree

10 Over View… 10 string 9-5+2 9-5+2 Ambiguity: Two Parse Trees for 9 – 5 + 2

11 11

12 Contents  Attributes  Translation Schemes  Postfix Notation  Synthesized Attributes  Tree Traversals  Translation Schemes  Preorder and Postorder Traversals 12

13 Syntax-Directed Translation  Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar.  Ex. consider an expression expr generated by the production Expr -> expr 1 + term  Translating expr by exploiting its structure, got the following pseudo-code: translate expr 1 ; translate term; handle +; 13

14 Syntax-Directed Translation..  Syntax directed translation introduces two new concepts Attributes and Translation schemes.  An attribute is any quantity associated with a programming construct.  Examples of attributes are  Data types of expressions,  Number of instructions in the generated code,  The location of the first instruction in the generated code. 14

15 Syntax-Directed Translation...  A translation scheme is a notation for attaching program fragments to the productions of a grammar.  The program fragments are executed when the production is used during syntax analysis.  Combined result of all these fragment executions, produces the translation of the program. 15

16 Postfix Notation  The postfix notation for an expression E can be defined inductively as follows:  If E is a variable or constant, then the postfix notation for E is E itself.  If E is an expression of the form El op E2, where op is any binary operator, then the postfix notation for E is E 1 ’ E 2 ’ op, where E 1 ’ and E 2 ’ are the postfix notations for El and E2, respectively.  If E is a parenthesized expression of the form (E1), then the postfix notation for E is the same as the postfix notation for E1. 16

17 Postfix Notation..  Ex (9 - 5) + 2 Translations of 9, 5 and 2 are constant themselves by Rule 1 9 – 5 is 95- by Rule 2 (9-5) is the same by Rule 3 Now we have to apply Rule 2 So E 1 represents (9-5), E 2 represents 2, op is + We got 95-2+ by Rule 2 17

18 Postfix Notation...  Another Ex 9 – (5 + 2) 18

19 Synthesized Attributes  A syntax-directed definition associates  With each grammar symbol, a set of attributes, and  With each production, a set of semantic rules for computing the values of the attributes associated with the symbols appearing in the production.  A parse tree showing the attribute values at each node is called an annotated parse tree. 19

20 Synthesized Attributes..  Suppose a node N in a parse tree is labeled by the grammar symbol X then X.a is used to denote the value of attribute a of X at that node. 20

21 Synthesized Attributes...  An attribute is said to be synthesized if its value at a parse-tree node N is determined from attribute values at the children of N and at N itself.  Synthesized attributes can be evaluated during a single bottom-up traversal of a parse tree.  An annotated parse tree is based on the syntax directed definition. 21

22 Synthesized Attributes...  Each non terminal has a string-valued attribute t that represents the postfix notation for the expression generated by that non terminal in a parse tree.  String concatenation operator || 22 Syntax-directed definition for infix to postfix translation

23 Tree Traversals  Tree traversals are used for describing attribute evaluation and for specifying the execution of code fragments in a translation scheme.  A traversal of a tree starts at the root and visits each node of the tree in some order.  A depth-first traversal starts at the root and recursively visits the children of each node in any order, not necessarily from left to right. 23

24 Tree Traversals 24 A depth-first traversal of a tree

25 Translation Schemes  A syntax-directed translation scheme is a notation for specifying a translation by attaching program fragments to productions in a grammar.  Program fragments embedded within production bodies are called semantic actions. rest -> + term { print('+') } rest 1 25 Action to be executed

26 Translation Schemes..  An extra leaf is constructed for the semantic action. 26

27 Translation Schemes...  Actions translating 9-5+2 into 95-2+ 27

28 Translation Schemes...  Actions for translating into postfix notation 28

29 Translation Schemes...  The implementation of a translation scheme must ensure  Semantic actions are performed in the order they would appear during a postorder traversal of a parse tree. 29

30 Preorder and Postorder Traversals  In preorder traversal action is done when we first visit a node.  If the action is done just before we leave a node for the last time, then we say it is a postorder traversal of the tree.  Preorder and postorder traversals define corresponding orderings on nodes, based on when the action at a node would be performed. 30

31 Thank You


Download ppt "Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End."

Similar presentations


Ads by Google