0% found this document useful (0 votes)
257 views35 pages

BKS Unit II-Syntax Directed Definitions New

The document discusses Unit II of the course "Learn Compiler Design: From B K Sharma". Unit II covers syntax analysis and syntax directed translation. It includes topics like context-free grammars, parsing techniques (top-down, bottom-up, precedence), parser generators, syntax trees, attribute grammars, synthesized and inherited attributes, and semantic analysis. Syntax directed definitions and syntax directed translations are used to embed semantic rules and actions into a grammar to perform semantic analysis on a parse tree.

Uploaded by

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

BKS Unit II-Syntax Directed Definitions New

The document discusses Unit II of the course "Learn Compiler Design: From B K Sharma". Unit II covers syntax analysis and syntax directed translation. It includes topics like context-free grammars, parsing techniques (top-down, bottom-up, precedence), parser generators, syntax trees, attribute grammars, synthesized and inherited attributes, and semantic analysis. Syntax directed definitions and syntax directed translations are used to embed semantic rules and actions into a grammar to perform semantic analysis on a parse tree.

Uploaded by

Shivam Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Learn Compiler Design: From B K Sharma

Unit II

Syntax Analysis and Syntax Directed


Translation
Learn Compiler Design: From B K Sharma

Unit II: Syllabus


• Syntax Analysis and Syntax Directed
Translation
• Syntax Analysis:
– CFGs
– Top Down Parsing
– Brute Force Approach
– Recursive Descent Parsing
– Transformation on the grammars
Learn Compiler Design: From B. K. Sharma
Unit II: Syllabus

– Predictive Parsing
– Bottom Up Parsing
– Operator Precedence Parsing
– LR Parsers
• SLR,
• LALR
• LR
• Parser Generator
Learn Compiler Design: From B K Sharma

Unit II: Syllabus

• Syntax Directed Definitions


– Construction of syntax trees
– Bottom Up Evaluation of S-attributed
definition
– L-attribute Definition
– Top Town translation
– Bottom Up Evaluation of inherited attributes
– Recursive Evaluation
– Analysis of Syntax Directed Definition
Learn Compiler Design: From B K Sharma
The Translation Process: Phases of Compiler
Source Program

1. Lexical Analysis

2. Syntax Analysis

3. Semantic Analysis

4. IR Code Gen

5. Code Optimization

6. Target Code Generation

Target Program
Learn Compiler Design: From B K Sharma
The Translation Process: Phases of Compiler
Source Program

1. Lexical Analysis(Scanner)

2. Syntax Analysis(Parser)

3. Semantic Analysis(Semantic Analyzer)

4. IR Code Gen

5. Code Optimization

6. Target Code Generation

Target Program
Learn Compiler Design: From B K Sharma
The Translation Process: Phases of Compiler
Source Program

1. Lexical Analysis(Scanner)
Tokens
2. Syntax Analysis(Parser)
Parse Tree/ Abstract Syntax Tree
3. Semantic Analysis(Semantic Analyzer)
Annotated Tree
4. IR Code Gen
Intermediate Code
5. Code Optimization
Optimized Code
6. Target Code Generation
Target Code
Target Program
Learn Compiler Design: From B K Sharma
The Translation Process: Phases of Compiler

Abstract Syntax Tree


Parse Tree

Annotated or Decorated or Attributed Syntax Tree


Learn Compiler Design: From B K Sharma
Semantic Analysis
Semantic analysis is the phase where we
collect information about the types of
expressions and check for type related
errors.
Semantic Analysis computes additional
information related to the meaning of the
program once the syntactic structure is
known.
The more information we can collect at
compile time, the less overhead we have at
run time.
Learn Compiler Design: From B K Sharma
Semantic Analysis
Additional Information
Has a variable been declared before used?
What is the type and size of variable?
x is variable or an array?
Does the name x corresponds to variable or
function?
If x is a function, then how many arguments does it
take?
What kind of value, if any, does a function x
return?
What is the type of x + y given the types of x and
y?
Learn Compiler Design: From B K Sharma

Syntax Directed Definition


In syntax directed definition, along with the
grammar symbol we associate attributes and some
informal notations and these notations are called
as semantic rules.
Grammar symbol + Attributes + Semantic
Rules=SDD
Learn Compiler Design: From B K Sharma

Tool: Attribute Grammar


Attribute grammar is extension of CFG in which:
Each Grammar symbol is associated with a set of
attributes.
Each production rule is associated with a set of
semantic rules.
Learn Compiler Design: From B K Sharma

Tool: Attribute Grammar


To help in semantic analysis, we have two tools:
1. Syntax-Directed Definitions(SDDs):-
Each Grammar symbol is associated with a set of
attributes.
Each production rule is associated with a set of semantic
rules.

2. Syntax-Directed Translations(SDTs):

Embeds semantic actions within production body.

E→E1 + T {print ‘+’;}


Learn Compiler Design: From B K Sharma

Syntax Directed Translation


Learn Compiler Design: From B K Sharma

Syntax Directed Definition


Annotated Parse Tree
A parse tree showing the values of its attributes is called
annotated parse tree.
Annotated Parse Tree for
3 * 5 + 4 n
Learn Compiler Design: From B K Sharma

Syntax Directed Definition


Annotated Parse Tree
Exercise
Given the following SDD draw the annotated Parse Tree for the
following expression: (3 + 4) (5+6)n
Learn Compiler Design: From B K Sharma

Syntax Directed Definition


Annotated Parse Tree
Exercise: Solution
Given the following SDD draw the annotated Parse Tree for the
following expression: (3 + 4) (5+6)n
Learn Compiler Design: From B K Sharma

Syntax Directed Definition


Annotated Parse Tree
Exercise
Give the annotated parse tree for 1∗2∗3∗(4+5)n
Learn Compiler Design: From B K Sharma

Syntax Directed Definition


Annotated Parse Tree
Exercise
Give the annotated parse tree for (9+8∗(7+6)+5)∗4n
Learn Compiler Design: From B K Sharma

SDD and SDT: Questions

Question : Discuss the role of syntax directed


translation scheme.

Question: Define Syntax Directed Translation.


Construct an annonated parse tree for the expression
(4 * 7 + l) + 2, using the simple desk calculator
grammar.

Question: What is syntax directed translation? How


are semantic actions attached to the productions?
Explain with examples.
Learn CD: From B K Sharma

Semantic Analysis
Attribute:
An attribute has a name and an associated value.

If X is a symbol and ‘a’ is one of its


attributes, then X.a denotes the value of ‘a’ at
a particular parse-tree node labelled X.
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes

1. Synthesized Attributes:

2. Inherited Attributes:
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
1. Synthesized Attributes:
They are computed from the values of the
attributes of the children nodes or itself.

A → B C

val1 val2 val3

Synthesized attributes represent information that is


being passed up the parse tree.
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
1. Synthesized Attributes:
A Synthesized attribute is an attribute of
the non-terminal on the left-hand side of a
production.
The attribute can take value only from its
children (Variables in the RHS of the
production) or itself.
For eg. let’s say A → BC is a production of
a grammar, and A’s attribute is dependent
on B’s attributes or C’s attributes then it
will be synthesized attribute.
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
1. Synthesized Attributes: Annotated Parse Tree for
3 * 5 + 4 n
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
2. Inherited Attributes:
They are computed from the values of
the attributes of both the siblings and
the parent nodes.

A → B C

val
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
2. Inherited Attributes:
An attribute of a non-terminal on the right-
hand side of a production is called an
inherited attribute.
The attribute can take value either from its
parent or from its siblings (variables in the
LHS or RHS of the production) or itself.

For example, let’s say A → BC is a


production of a grammar and B’s attribute is
dependent on A’s attributes or C’s attributes
then it will be inherited attribute.
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
2. Inherited Attributes: Parse Tree
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
2. Inherited Attributes:
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
2. Inherited Attributes: Annotated Parse Tree for 3*5
Learn CD: From B K Sharma

Semantic Analysis
Types of Attributes
2. Inherited Attributes: Annotated Parse Tree for 3*5
Learn CD: From B K Sharma

Semantic Analysis

Question : What are Synthesized and Inherited


Attributes? Explain with examples.
Learn CD: From B K Sharma

Semantic Analysis

Question : Discuss the role of syntax directed


translation scheme.

Question: Define Syntax Directed Translation.


Construct an annotated parse tree for the expression
(4 * 7 + l) + 2, using the simple desk calculator
grammar.
Question: What is syntax directed translation? How
are semantic actions attached to the productions?
Explain with examples.

Question : Give the syntax directed translation


scheme to translate the while control construct.
Learn CD: From B K Sharma

Read PTT named Construction of Syntax Tree and DAG

You might also like