You are on page 1of 11

SYNTAX DIRECTED

TRANSLATION
Phases of a Compiler
1. Lexical Analyzer (Scanner)
Takes source Program and Converts into tokens
2. Syntax Analyzer (Parser)
Takes tokens and constructs a parse tree.
3. Semantic Analyzer
Takes a parse tree and constructs an abstract syntax
tree with attributes.
Phases of a Compiler- Contd
4. Syntax Directed Translation
Takes an abstract syntax tree and produces an
Interpreter code (Translation output)
5. Intermediate-code Generator
Takes an abstract syntax tree and produces un-
optimized Intermediate code.
Abstract Tree & Parse Tree
Introduction

 We can associate information with a language


construct by attaching attributes to the grammar
symbols.
 A syntax directed definition specifies the values of
attributes by associating semantic rules with the
grammar productions.
Production Semantic Rule
E->E1+T E.code=E1.code||T.code||’+’

• We may alternatively insert the semantic actions inside the grammar


E -> E1+T {print ‘+’}
Introduction

 In SDT, every non-terminal can get 0 or more attributes.


(depends on type of attribute).
 In semantic rule an attribute is represented as VAL and it can
be string, number, memory location…………….
 It is used to evaluate the order of semantic rules.
Examples
 Grammar + Semantic Rules = SDT

 While parsing we do the parsing action, alongwith


the semantic action
Example of SDT

Production Semantic Rules


1) E -> E + T E.val = E.val + T.val
2) E -> T E.val = T.val
3) T -> T * F T.val = T.val * F.val
4) T -> F T.val = F.val
5) F -> num F.val = digit.lexval
2+3*4
Top down – Add semantic action
Bottom Up Parser
 Whenever they reduce, they perform the action

You might also like