You are on page 1of 41

ALL the ROUGH pages included

from lesson 1,2,3,4,5… are not


included in the paper.
Overview
of
Previous Lesson(s)
Over View

Model of a Compiler Front End

3
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
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
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
Over View...
 Derivation for 9 – 5 + 2

list Start Symbol


 list + digit P-1
 list - digit + digit P-2
 digit - digit + digit P-3
 9 - digit + digit P-4
 9 - 5 + digit P-4
9-5+2 P-4

 This is an leftmost derivation, because we replaced


the leftmost nonterminal.

7
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
Over View...
Parse tree of the string 9-5+2 using grammar G

list

list digit

list digit

digit
The sequence of
9 - 5 + 2 leafs is called the
yield of the parse tree

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

string string

string string string

string string string string string

9 - 5 + 2 9 - 5 + 2

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

12
Syntax-Directed Translation
 Syntax-directed translation is done by attaching rules or program
fragments to productions in a grammar.

 Example. consider an expression expr generated by the production


Expr -> expr1 + term

 Translating expr by exploiting its structure, got the following


pseudo-code:
translate expr1;
translate term;
handle +;

13
ROUGH
 Syntax Directed Translation are augmented rules to the grammar
that facilitate semantic analysis. SDT involves passing information
bottom-up and/or top-down the parse tree in form of attributes
attached to the nodes. Syntax directed translation rules use
 1) lexical values of nodes,
 2) constants &
 3) attributes associated to the non-terminals in their definitions.

 Augmented: having been made greater in size or value

14
Syntax-Directed Translation..
 Syntax directed translation introduces two new concepts
1-Attributes and 2-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.

15
ROUGH
 Attribute Grammar
 Attribute grammar is a special form of context-free grammar where some
additional information (attributes) are appended to one or more of its non-
terminals in order to provide context-sensitive information. Each attribute has
well-defined domain of values, such as integer, float, character, string, and
expressions.

 Attribute grammar is a medium to provide semantics to the context-free


grammar and it can help specify the syntax and semantics of a programming
language. Attribute grammar (when viewed as a parse-tree) can pass values or
information among the nodes of a tree.
 example
 Production: E → E + T Semantic rule: { E.value = E.value + T.value }

 Here, the values of non-terminals E and T are added together and


the result is copied to the non-terminal E.
16
ROUGH
 Attributes are associated with grammar symbols and rules with
productions
 Attributes may be of many kinds: numbers, types, table
references, strings, etc.

17
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.

18
ROUGH
 The Syntax directed translation scheme is a context -free
grammar.
 The syntax directed translation scheme is used to evaluate the
order of semantic rules.
 In translation scheme, the semantic rules are embedded within
the right side of the productions.
 The position at which an action is to be executed is shown by
enclosed between braces. It is written within the right side of the
production.

19
 Syntax direct translation is implemented by constructing a parse
tree and performing the actions in a left to right depth first
order.

 SDT is implementing by parse the input and produce a parse tree


as a result.

20
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 E1’ E2’ op, where E1’ and E2’
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.

21
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 E1 represents (9-5) , E2 represents 2 , op is +
We got 95-2+ by Rule 2

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

23
ROUGH
 Semantic attributes : may be assigned to their values from
their domain at the time of parsing and evaluated at the time of
assignment or conditions. Based on the way the attributes get
their values, they can be broadly divided into two categories :
synthesized attributes and inherited attributes.

24
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.

25
ROUGH
 Synthesized Attributes 
 These attributes get values from the attribute values of their child
nodes. To illustrate, assume the following production:
 S → ABC
 If S is taking values from its child nodes (A,B,C), then it is said to
be a synthesized attribute, as the values of ABC are synthesized
to S.

 As in another example (E → E + T), the parent node E gets its


value from its child node.
 Synthesized attributes never take values from their parent nodes
or any sibling nodes.
26
ROUGH
 Inherited attributes
 In contrast to synthesized attributes, inherited attributes can take
values from parent and/or siblings. As in the following
production,
 S → ABC
 A can get values from S, B and C. B can take values from S, A, and
C. Likewise, C can take values from S, A, and B.

27
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.

28
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.

29
ROUGH
 AN ANNOTATED PARSE TREE is a parse tree showing the values of
the attributes at each node. The process of computing the
attribute values at the nodes is called annotating or decorating
the parse tree

30
ROUGH
Synthesized attributes
These attributes get values from the attribute values
of their child nodes. To illustrate, assume the following
production:
S → ABC
If S is taking values from its child nodes (A,B,C), then
it is said to be a synthesized attribute, as the values of
ABC are synthesized to S.
As in example (E → E + T), the parent node E gets its
value from its child node. Synthesized attributes never
take values from their parent nodes or any sibling
nodes.

31
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 ||

Syntax-directed definition for infix to postfix translation


32
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 .

 Traversal is a process to visit all the nodes of a tree and may


print their values too. Because, all nodes are connected via
edges (links) we always start from the root (head) node. That
is, we cannot randomly access a node in a tree.

33
Tree Traversals
A depth-first traversal of a tree

34
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('+') } rest1

Action to be executed
A common method of syntax-directed translation is translating a string
into a sequence of actions by attaching one such action to each rule
of a grammar. Thus, parsing a string of the grammar produces a
sequence of rule applications. SDT provides a simple way to
attach semantics to any such syntax.
35
Translation Schemes..

 An extra leaf is constructed for the semantic action.

36
Translation Schemes...

 Actions translating 9-5+2 into 95-2+

37
Translation Schemes...

 Actions for translating into postfix notation

38
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.

39
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.

40
Thank You

You might also like