Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Learn Compiler Design: From B K Sharma

Unit II

Synthesized and Inherited Attributes


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

Definition of Attribute and its Types


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.

Types of Attributes

1. Synthesized Attributes:

2. Inherited Attributes:
Learn Compiler Design: From B K Sharma

Synthesized Attributes:
A Synthesized attribute is an attribute of the non-
terminal on the left-hand side of a production.
A → B C
The attribute can take value only from its children
(Variables in the RHS of the production) or itself.

A’s attribute is dependent on B’s attributes or C’s


attributes then it will be synthesized attribute.
Learn Compiler Design: From B K Sharma

Synthesized Attributes:
Synthesized Attributes values are computed from the
values of the attributes of the children nodes or itself.

A → B C

val1 val2 val3

Synthesized attributes represent information is passed up


the parse tree.
Learn Compiler Design: From B K Sharma

Synthesized Attributes:
Annotated Parse Tree for
3 * 5 + 4 n
Learn Compiler Design: From B K Sharma

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.

A → B C
B
B’s attribute is dependent on A’s
attributes or C’s attributes then it will
val be inherited attribute.

Inherited Attributes values are computed from the values


of the attributes of both the siblings and the parent
nodes.
Learn Compiler Design: From B K Sharma

Inherited Attributes:
Parse Tree
Learn Compiler Design: From B K Sharma

Inherited Attributes:
2. Inherited Attributes: Annotated Parse Tree for 3*5
Learn Compiler Design: From B K Sharma

Inherited Attributes:
2. Inherited Attributes: Annotated Parse Tree for 3*5
Learn Compiler Design: From B K Sharma

Synthesized and Inherited Attributes:

Question : What are Synthesized and Inherited


Attributes? Explain with examples.
Learn Compiler Design: From B K Sharma
Learn CD: From B K Sharma
Synthesized and Inherited Attributes:

Read PTT named Construction of Syntax Tree and DAG

You might also like