0% found this document useful (0 votes)
20 views

compiler-design-module-4-syntax-directed-translation-set-1

The document discusses Syntax Directed Translation (SDT) in compiler design, detailing syntax rules for expressions and statements, along with the concepts of Syntax Directed Definitions (SDD) and Syntax Directed Translation Schemes (SDTS). It explains the process of translating infix expressions to postfix notation and outlines parsing techniques, including top-down and predictive parsing. Additionally, it highlights the importance of semantic actions during parsing and the requirements for grammars suitable for recursive descent parsing.

Uploaded by

JEAN ROGER
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

compiler-design-module-4-syntax-directed-translation-set-1

The document discusses Syntax Directed Translation (SDT) in compiler design, detailing syntax rules for expressions and statements, along with the concepts of Syntax Directed Definitions (SDD) and Syntax Directed Translation Schemes (SDTS). It explains the process of translating infix expressions to postfix notation and outlines parsing techniques, including top-down and predictive parsing. Additionally, it highlights the importance of semantic actions during parsing and the requirements for grammars suitable for recursive descent parsing.

Uploaded by

JEAN ROGER
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

lOMoARcPSD|28573576

Compiler Design - Module 4 - Syntax Directed Translation -


Set 1
Compiler Design (Dr. A.P.J. Abdul Kalam Technical University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Downloaded by JEAN ROGER ([email protected])


lOMoARcPSD|28573576

Warning 

These contents are not made as per the KTU syllabus,


It gave most of the syllabus prescribed contents,
you can use it as a reference

Downloaded by JEAN ROGER ([email protected])


lOMoARcPSD|28573576

MODULE 2 - SYNTAX-DIRECTED TRANSLATION

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

MODULE-3 TYPE CHECKING

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

• Syntax of full expressions


operator associative precedence

+,- left 1 low


*,/ left 2 heigh

• expr → expr + term | expr - term | term


term → term * factor | term / factor | factor
factor → digit | ( expr )
digit → 0 | 1 | … | 9

• Syntax of statements
o stmt → id = expr ;
| if ( expr ) stmt ;
| if ( expr ) stmt else stmt ;
| while ( expr ) stmt ;
expr → expr + term | expr - term | term
term → term * factor | term / factor | factor
factor → digit | ( expr )
digit → 0 | 1 | … | 9
2.3 SYNTAX-DIRECTED TRANSLATION(SDT)
A formalism for specifying translations for programming language constructs.
( attributes of a construct: type, string, location, etc)
• Syntax directed definition(SDD) for the translation of constructs
• Syntax directed translation scheme(SDTS) for specifying translation
Postfix notation for an expression E
• If E is a variable or constant, then the postfix nation for E is E itself ( E.t≡E ).
• if E is an expression of the form E1 op E2 where op is a binary operator
o E1' is the postfix of E1,
o E2' is the postfix of E2
o then E1' E2' op is the postfix for E1 op E2
• if E is (E1), and E1' is a postfix
then E1' is the postfix for E

eg) 9-5+2⇒95-2+

9 - (5 + 2) ⇒ 9 5 2 + -

Syntax-Directed Definition(SDD) for translation


• SDD is a set of semantic rules predefined for each productions respectively for
translation.
• A translation is an input-output mapping procedure for translation of an input X,
o construct a parse tree for X.
o synthesize attributes over the parse tree.

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

 Suppose a node n in parse tree is labeled by X and X.a denotes the value
of attribute a of X at that node.
 compute X's attributes X.a using the semantic rules associated with X.

Example 2.6. SDD for infix to postfix translation

Fig 2.5. Syntax-directed definition for infix to postfix translation.

An example of synthesized attributes for input X=9-5+2

Fig 2.6. Attribute values at nodes in a parse tree.

Syntax-directed Translation Schemes(SDTS)


• A translation scheme is a context-free grammar in which program fragments called
translation actions are embedded within the right sides of the production.
productions(postfix) SDD for postfix to SDTS
infix notation
list → list + term list.t = list.t || term.t || "+" list → list + term
{print("+")}

• {print("+");} : translation(semantic) action.


• SDTS generates an output for each sentence x generated by underlying grammar by
executing actions in the order they appear during depth-first traversal of a parse tree for x.

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

1. Design translation schemes(SDTS) for translation


2. Translate :
a) parse the input string x and
b) emit the action result encountered during the depth-first traversal of parse tree.

Fig 2.7. Example of a depth-first traversal of a tree. Fig 2.8. An extra leaf is constructed for a semantic action.

Example 2.8.
• SDD vs. SDTS for infix to postfix translation.

productions SDD SDTS


expr → list + term expr.t = list.t || term.t || "+" expr → list + term
expr → list + term expr.t = list.t || term.t || "-" printf{"+")}
expr → term expr.t = term.t expr → list + term printf{"-")}
term → 0 term.t = "0" expr → term
term → 1 term.t = "1" term → 0 printf{"0")}
… … term → 1 printf{"1")}
term → 9 term.t = "9" …
term → 9 printf{"0")}

• Action translating for input 9-5+2

Fig 2.9. Actions translating 9-5+2 into 95-2+.


1) Parse.
2) Translate.
Do we have to maintain the whole parse tree ?
No, Semantic actions are performed during parsing, and we don't need the nodes (whose
semantic actions done).

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

2.4 PARSING
if token string x ∈ L(G), then parse tree
else error message
Top-Down parsing
1. At node n labeled with nonterminal A, select one of the productions whose left part is
A and construct children of node n with the symbols on the right side of that production.
2. Find the next node at which a sub-tree is to be constructed.
ex. G: type → simple
|↑id
|array [ simple ] of type
simple → integer
|char
|num dotdot num

Fig 2.10. Top-down parsing while scanning the input from left to right.

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

Fig 2.11. Steps in the top-down construction of a parse tree.


• The selection of production for a nonterminal may involve trial-and-error. =>
backtracking

• G : { S->aSb | c | ab }
According to topdown parsing procedure, acb , aabb∈L(G)?
• S/acb⇒aSb/acb⇒aSb/acb⇒aaSbb/acb ⇒ X
(S→aSb) move (S→aSb) backtracking
⇒aSb/acb⇒acb/acb⇒acb/acb⇒acb/acb
(s→c) move move
so, acb∈ L(G)
Is is finished in 7 steps including one backtracking.

• S/aabb⇒aSb/aabb⇒aSb/aabb⇒aaSbb/aabb⇒aaSbb/aabb⇒aaaSbbb/aabb ⇒ X
(S→aSb) move (S→aSb) move (S→aSb) backtracking
⇒aaSbb/aabb⇒aacbb/aabb ⇒ X
(S→c) backtracking
⇒aaSbb/aabb⇒aaabbb/aabb⇒ X
(S→ab) backtracking
⇒aaSbb/aabb⇒ X
backtracking
⇒aSb/aabb⇒acb/aabb
(S→c) bactracking
⇒aSb/aabb⇒aabb/aabb⇒aabb/aabb⇒aabb/aabb⇒aaba/aabb
(S→ab) move move move
so, aabb∈L(G)
but process is too difficult. It needs 18 steps including 5 backtrackings.

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])
lOMoARcPSD|28573576

• procedure of top-down parsing


let a pointed grammar symbol and pointed input symbol be g, a respectively.
o if( g ∈ N ) select and expand a production whose left part equals to g next to
current production.
else if( g = a ) then make g and a be a symbol next to current symbol.
else if( g ≠a ) back tracking
 let the pointed input symbol a be the symbol that moves back to steps
same with the number of current symbols of underlying production
 eliminate the right side symbols of current production and let the pointed
symbol g be the left side symbol of current production.

Predictive parsing (Recursive Decent Parsing,RDP)


• A strategy for the general top-down parsing
Guess a production, see if it matches, if not, backtrack and try another.

• It may fail to recognize correct string in some grammar G and is tedious in processing.

• Predictive parsing
o is a kind of top-down parsing that predicts a production whose derived terminal
symbol is equal to next input symbol while expanding in top-down paring.
o without backtracking.
o Procedure decent parser is a kind of predictive parser that is implemented by
disjoint recursive procedures one procedure for each nonterminal, the procedures
are patterned after the productions.
• procedure of predictive parsing(RDP)
let a pointed grammar symbol and pointed input symbol be g, a respectively.
o if( g ∈ N )
 select next production P whose left symbol equals to g and a set of first
terminal symbols of derivation from the right symbols of the production P
includes a input symbol a.
 expand derivation with that production P.
o else if( g = a ) then make g and a be a symbol next to current symbol.
o else if( g ≠a ) error

• G : { S→aSb | c | ab } => G1 : { S->aS' | c S'->Sb | ab }


According to predictive parsing procedure, acb , aabb∈L(G)?
o S/acb⇒ confused in { S→aSb, S→ab }
o so, a predictive parser requires some restriction in grammar, that is, there should
be only one production whose left part of productions are A and each first
terminal symbol of those productions have unique terminal symbol.
• Requirements for a grammar to be suitable for RDP: For each nonterminal either
1. A → Bα, or
2. A → a1α1 | a2α2 | … | anαn
1) for 1 ≦ i, j ≦ n and i≠ j, ai ≠ aj
2) A ε may also occur if none of ai can follow A in a derivation and if we have A→ε

Studoob.in - Where Learning is Entertainment


Downloaded by JEAN ROGER ([email protected])

You might also like