0% found this document useful (0 votes)
376 views32 pages

BKS Unit II-S and L-Attributed SDDs

The document discusses syntax analysis and syntax directed translation. It covers topics like context free grammars, different parsing techniques including top-down parsing, bottom-up parsing, operator precedence parsing and LR parsers. It also discusses syntax directed definitions, their evaluation using synthesized and inherited attributes, and construction of syntax trees. Specifically, it describes S-attributed definitions which use only synthesized attributes and L-attributed definitions which allow use of both synthesized and inherited attributes restricted to left siblings and parents. It provides examples of annotated parse trees and dependency graphs to illustrate the evaluation of attributes.

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)
376 views32 pages

BKS Unit II-S and L-Attributed SDDs

The document discusses syntax analysis and syntax directed translation. It covers topics like context free grammars, different parsing techniques including top-down parsing, bottom-up parsing, operator precedence parsing and LR parsers. It also discusses syntax directed definitions, their evaluation using synthesized and inherited attributes, and construction of syntax trees. Specifically, it describes S-attributed definitions which use only synthesized attributes and L-attributed definitions which allow use of both synthesized and inherited attributes restricted to left siblings and parents. It provides examples of annotated parse trees and dependency graphs to illustrate the evaluation of attributes.

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
You are on page 1/ 32

Learn CD: 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 CD: From B K Sharma

S-attributed Definition(or SDD):


Definition: An S-Attributed Definition is an SDD that
uses only synthesized attributes.
If an SDD uses only synthesized attributes,
it is called as S-attributed SDD.
Learn CD: From B K Sharma

S-attributed Definition(or SDD):


Evaluation Order

Semantic rules in a S-Attributed Definition can be


evaluated by a bottom-up, or PostOrder(Left-Right-
Root) traversal of the parse-tree.
S-attributed SDDs are evaluated in bottom-up parsing,
as the values of the parent nodes depend upon the
values of the child nodes.
Semantic actions are placed in rightmost place of RHS.
Learn CD: From B K Sharma

L-attributed Definition(or SDD) :


Definition: A L-Attributed Definition(L for Left-to -
right) is an SDD that uses both
synthesized and inherited attributes with a
restriction that inherited attribute can
inherit values from left siblings only or
parent or both.

The inherited attribute T’ gets its values from its left


siblings F.
Similarly T1’ gets its values from its parent’s T’ and its
left sibling F
Learn CD: From B K Sharma

L-attributed Definition(or SDD) :

L-Attributed

L for Left-to-right:
Dependency graph edges go from left-to-right.
Learn CD: From B K Sharma

Not L-attributed SDD :

B.i depends on its right sibling C’s attribute ,C.c


Learn CD: From B K Sharma

Not L-attributed SDD :


For example:

A -> XYZ {Y.s = A.s, Y.s = X.s, Y.s = Z.s}

is not an L-attributed grammar since Y.s = A.s and


Y.s = X.s are allowed but Y.s = Z.s violates the L-
attributed SDD definition as attributed is inheriting
the value from its right sibling.
Learn CD: From B K Sharma

L-Attributed Definitions
Evaluation Order
Attributes in L-attributed SDDs are evaluated by
depth-first and left-to-right parsing manner.

Inherited attributes in L-Attributed Definitions can be


computed by a PreOrder(Root-Left-Right) traversal of
the parse-tree.

Semantic actions are placed anywhere in RHS.


Learn CD: From B K Sharma

L-Attributed Definitions

A.a

X1.x X2.x

Shown: dependences of inherited attributes


Learn CD: From B K Sharma

L-Attributed Definitions
L-attributed definitions allow for a natural order of
evaluating attributes: depth-first and left to right

A  X Y X.i:=A.i A A.s:=Y.s X.i := A.i


Y.i := X.s
X Y A.s := Y.s

Y.i:=X.s

Note: every S-attributed syntax-directed definition is


also L-attributed
Learn CD: From B K Sharma

S and L-Attributed Definitions

Example – Consider the given below SDT.


P1: S → MN {S.val= M.val + N.val}

P2: M → PQ {M.val = P.val * Q.val and P.val =Q.val}

Select the correct option:

A. Both P1 and P2 are S attributed.


B. P1 is S attributed and P2 is L-attributed.
C. P1 is L attributed but P2 is not L-attributed.
D. None of the above
Learn CD: From B K Sharma

S and L-Attributed Definitions


The correct answer is option C as, In P1, S is a
synthesized attribute and in L-attribute definition
synthesized is allowed.
So P1 follows the L-attributed definition.

But P2 doesn’t follow L-attributed definition as P is


depending on Q which is RHS to it.
Learn CD: From B K Sharma

S and L-Attributed Definitions

Note – If a definition is S-attributed, then it is also


L-attributed but NOT vice-versa.
Learn CD: From B K Sharma

Evaluation Order:
Once a parse tree is constructed, attributes can be
computed (evaluated) according to the defined semantic
rules.
The question is: in what order?
Learn CD: From B K Sharma

Dependency Graph

A dependency graph can be built to guide the


evaluation of semantic rules.

Dependency Graphs are the most general technique


used to evaluate syntax directed definitions with both
synthesized and inherited attributes.
A Dependency Graph shows the interdependencies
among the attributes of the various nodes of a parse-
tree.
Learn CD: From B K Sharma

Dependency Graph

There is a node for each attribute.

If attribute b depends on an attribute c there is a link


from the node for c to the node for b (b ← c).
Dependency Rule: If an attribute b depends on an
attribute c, then we need to fire the semantic rule
for c first and then the semantic rule for b.
Learn CD: From B K Sharma

Acyclic Dependency Graphs for Parse Trees

A.a
A  X Y A.a := f(X.x, Y.y)
X.x Y.y

A.a
X.x := f(A.a, Y.y)
X.x Y.y

Direction of A.a
Y.y := f(A.a, X.x)
value dependence X.x Y.y
Learn CD: From B K Sharma

Dependency Graph: Example


A  XY A.a := f( X.x, Y.y)
A

X Y
X.i :=g(A.a,Y.y)

X Y
Learn CD: From B K Sharma

Dependency Graphs with Cycles?


Edges in the dependency graph determine the
evaluation order for attribute values

Dependency graphs cannot be cyclic

A.a A.a := f(X.x)


X.x := f(Y.y)
X.x Y.y Y.y := f(A.a)
Error: cyclic dependence
Learn CD: From B K Sharma

Dependency Graph: Example


D  T L L.in := T.type
T  int T.type := integer;
T  float T.type := float;
L  L1, id L1.in = L.in; addtype(id.entry, L.in)
L  id addtype(id.entry,L.in)
D
T L
L
integer id
L id
id
Synthesized: T.type, id.entry Inherited: L.in
Learn CD: From B K Sharma

Example: Annotated Parse Tree

D
T.type = ‘real’ L.in = ‘real’

real L.in = ‘real’ , id3.entry

L.in = ‘real’ , id2.entry

id1.entry
Learn CD: From B K Sharma

Example: Annotated Parse Tree


with Dependency Graph

D
T.type = ‘real’ L.in = ‘real’

real L.in = ‘real’ , id3.entry

L.in = ‘real’ , id2.entry

id1.entry
Learn CD: From B K Sharma

Annotated Parse Tree


Question : Consider the following grammar and give the
syntax directed definitions to construct parse tree. For
the input expression 4* 7 + 1 *2 construct an
annotated parse tree according to your-syntax directed
definition:
E→ E*T | T
T→ 'I'*F | F
F→ digit
Learn CD: From B K Sharma

Inherited Attributes: Example


The annotated parse-tree for the input real id1, id2,
id3 is:
Learn CD: From B K Sharma

Inherited Attributes: Example

The annotated parse-tree for the input real id1, id2,


id3 is:
Learn CD: From B K Sharma

Inherited Attributes: Example

The annotated parse-tree for the input real id1, id2,


id3 is:
Learn CD: From B K Sharma

Inherited Attributes: Example

The annotated parse-tree for the input real id1, id2,


id3 is:

L.in is then inherited top-down the tree by the other


L-nodes.
At each L-node the procedure addtype inserts into the
symbol table the type of the identifier.
Learn CD: From B K Sharma

Dependency Graph
Evaluation Order
The evaluation order of semantic rules depends from a
Topological Sort derived from the dependency graph.

Any topological sort of a dependency graph gives a


valid order to evaluate the semantic rules.

Topological Sort: of a directed acyclic graph(DAG) is


any ordering m1,m2,...,mk such that if mi → mj is an
edge in the dependency graph then mi < mj (means mi
appears before mj
Learn CD: From B K Sharma

Example: Parse Tree with


Topologically Sorted Actions
Topological sort:
1. Get id1.entry
D
2. Get id2.entry
T1.type = ‘real’ 4 5 L1.in = ‘real’ 6 3. Get id3.entry

real 7 L2.in = ‘real’ 8 , 3 id3.entry 4. T1.type=‘real’


5. L1.in=T1.type
9 L3.in = ‘real’ 10 , 2 id2.entry 6. addtype(id3.entry, L1.in)

1 id1.entry 7. L2.in=L1.in

8. addtype(id2.entry, L2.in)
9. L3.in=L2.in
10. addtype(id1.entry, L3.in)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy