2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A...

45
2.2 A Simple Syntax- Directed Translator 1

Transcript of 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A...

Page 1: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

1

2.2 A Simple Syntax-Directed Translator

Page 2: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

2

Page 3: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

3

• 2.3 Syntax-Directed Translation• 2.4 Parsing• 2.5 A Translator for Simple Expressions• 2.6 Lexical Analysis• 2.7 Symbol Tables• 2.8 Intermediate Code Generation• 2.9 Summary of Chapter 2

Page 4: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

4

Syntax-Directed Translation• Syntax-directed translation is done by

attaching rules or program fragments to productions in a grammar.

• For example, consider an expression expr generated by the production:

• We can translate expr by exploiting its structure, as in the following pseudo-code:

Page 5: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

5

Concepts of syntax-directed translation

• Attributes.– An attribute is any quantity associated with a programming

construct. Examples of attributes are data types of expressions, the number of instructions in the generated code, or the location of the first instruction in the generated code for a construct , among many other possibilities.

• (Syntax- directed) translation schemes. – 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.

Page 6: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

6

Postfix Notation

• The postfix notation for an expression E can be defined inductively as follows:

Page 7: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

7

Postfix Notation

• Example 2.8 : The postfix notation for (9-5)+2

• What about??9- (5+2)

• Evaluate??

Page 8: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

8

Postfix Notation

• How to evaluate??– The "trick" is to repeatedly scan the postfix string

from the left , until you find an operator. – Then, look to the left for the proper number of

operands, – group this operator with its operands. – Evaluate the operator on the operands, and replace

them by the result.– repeat the process, continuing to the right and

searching for another operator.

Page 9: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

9

Postfix Notation

• Example 2.9 : evaluate the following postfix notation:

952+-3*

Page 10: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

10

Synthesized Attributes

• We associate attributes with nonterminals and terminals. Then, we attach rules to the productions of the grammar; these rules describe how the attributes are computed at those nodes of the parse tree where the production in question is used to relate a node to its children.

Page 11: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

11

Page 12: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

12

Page 13: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

13

Tree Traversals

• Tree traversals will be 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 . It is called "depth first“ because it visits an unvisited child of a node whenever it can, so it visits nodes as far away from the root (as "deep" ) as quickly as it can.

Page 14: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

14

Page 15: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

15

Translation Schemes

• Preorder and Postorder Traversals

Page 16: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

16

Page 17: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

17

Page 18: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

18

Semantic actions

Page 19: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

19

Example

Actions for postfix translating 9-5+2 into 95-2+

Page 20: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

20

2.4 Parsing• Parsing is the process of determining how a

string of terminals can be generated by a grammar.– Top-Down Parsing

Page 21: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

21

steps

Page 22: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

22

Page 23: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

23

Use ϶-Productions

• Predictive Parsing (self study)• When to Use ϶-Productions– predictive parser uses an ϶ -production as a

default when no other production can be used

Page 24: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

24

Page 25: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

25

2.5 A Translator for Simple Expressions

• Using the techniques of the last three sections, we now construct a syntax directed translator– A syntax-directed translation scheme often serves as the

specification for a translator. – The scheme in Fig. 2.21 (repeated from Fig. 2.15) defines

the translation to be performed here

Page 26: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

26

Abstract and Concrete Syntax

• A useful starting point for designing a translator is a data structure called an abstract syntax tree.

• In an abstract syntax tree for an expression, each interior node represents an operator; the children of the node represent the operands of the operator.

Page 27: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

27

Example

Page 28: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

28

Page 29: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

29

syntax tree vs. parse tree

• in the syntax tree, interior nodes represent programming constructs while in the parse tree, the interior nodes represent nonterminals.

Page 30: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

30

Page 31: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

31

2.6 Lexical Analysis• A lexical analyzer reads characters from the input and groups

them into "token objects.“• The lexical analyzer in this section allows numbers, identifiers,

and "white space" (blanks, tabs, and newlines) to appear within expressions.

Page 32: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

32

Reading Ahead

• A lexical analyzer may need to read ahead some characters before it can decide on the token to be returned to the parser.

• For example, a lexical analyzer for C or Java must read ahead after it sees the character >.– If the next character is =, then > is part of the

character sequence >=, the lexeme for the token for the "greater than or equal to" operator.

– Otherwise > itself forms the "greater than" operator, and the lexical analyzer has read one character too many.

Page 33: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

33

Constants

Page 34: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

34

Recognizing Keywords and Identifiers

• Most languages use fixed character strings such as for, do, and if , as punctuation marks or to identify constructs.

• Such character strings are called keywords.• Character strings are also used as identifiers to

name variables, arrays, functions, and the like. • Grammars routinely treat identifiers as terminals to

simplify the parser, which can then expect the same terminal, say id, each time any identifier appears in the input. For example, on input

Page 35: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

35

Recognizing Keywords and Identifiers

• the parser works with the terminal stream id = id + id. The token for id has an attribute that holds the lexeme.

• Writing tokens as tuples, we see that the tuples for the input stream (2.6) are

• Keywords generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier.

• The problem is easier to resolve if keywords are reserved; i.e., if they cannot be used as identifiers. Then, a character string forms an identifier only if it is not a keyword.

Page 36: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

36

2.7 Symbol table

• Symbol tables are data structures that are used by compilers to hold information about source-program constructs.

• The information is collected incrementally by the analysis phases of a compiler and used by the synthesis phases to generate the target code.

• Entries in the symbol table contain information about an identifier such as its character string (or lexeme) , its type, its position in storage, and any other relevant information. Symbol tables typically need to support multiple declarations of the same identifier within a program.

Page 37: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

37

Scope

• Symbol Table Per Scope: The term "scope of identifier x' really refers to the scope of a particular declaration of x.

• Scopes are important, because the same identifier can be declared for different purposes in different parts of a program.

• If blocks can be nested, several declarations of the same identifier can appear within a single block.

Page 38: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

38

Page 39: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

39

2.8 Intermediate Code Generation

• Two Kinds of Intermediate Representations

Page 40: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

40

Construction of Syntax Trees

Page 41: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

41

L-values and R-values

• There is a distinction between the meaning of identifiers on the left and right sides of an assignment. In each of the assignments the right side specifies an integer value, while the left side specifies where the value is to be stored.

Page 42: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

42

Type Checking

Page 43: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

43

Three-Address Code

Page 44: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

44

Example

Page 45: 2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

45

2.9 Summary o f Chapter 2