0% found this document useful (0 votes)
40 views11 pages

Lecture 15

This document discusses syntax directed translation, which is a phase of compilers that uses an abstract syntax tree to produce an interpreter code or intermediate code. It associates semantic rules with grammar productions to specify how attributes are evaluated. During parsing, both parsing and semantic actions are performed. Examples show how grammar productions are associated with semantic rules to evaluate expressions, and how bottom-up parsing performs the semantic actions during reduction.

Uploaded by

abdul rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views11 pages

Lecture 15

This document discusses syntax directed translation, which is a phase of compilers that uses an abstract syntax tree to produce an interpreter code or intermediate code. It associates semantic rules with grammar productions to specify how attributes are evaluated. During parsing, both parsing and semantic actions are performed. Examples show how grammar productions are associated with semantic rules to evaluate expressions, and how bottom-up parsing performs the semantic actions during reduction.

Uploaded by

abdul rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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