0% found this document useful (0 votes)
278 views20 pages

BKS Unit II-Construction of Syntax Trees and DAG

This document discusses syntax analysis and syntax-directed translation, which are covered in Unit II of the Learn Compiler Design course by B.K. Sharma. The unit covers topics like context-free grammars, different parsing techniques (top-down, bottom-up, precedence), parser generators, and syntax-directed definitions. It discusses how abstract syntax trees and directed acyclic graphs are constructed using syntax-directed definitions to represent expressions and common sub-expressions.

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)
278 views20 pages

BKS Unit II-Construction of Syntax Trees and DAG

This document discusses syntax analysis and syntax-directed translation, which are covered in Unit II of the Learn Compiler Design course by B.K. Sharma. The unit covers topics like context-free grammars, different parsing techniques (top-down, bottom-up, precedence), parser generators, and syntax-directed definitions. It discusses how abstract syntax trees and directed acyclic graphs are constructed using syntax-directed definitions to represent expressions and common sub-expressions.

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/ 20

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

Construction of Syntax Trees


Using Syntax Directed Definition

An Abstract Syntax Tree(AST) is a condensed form of


parse tree useful for representing language constructs.

In a syntax tree, operators and keywords do not


appear as leaves, but rather are associated with the
interior node that would be the parent of those
leaves in the parse tree.
Learn CD: From B K Sharma

Construction of Syntax Trees


a := x + y * 2.5 ;

Parse Tree Syntax Tree


Learn CD: From B K Sharma

Construction of Syntax Trees


for expressions
The following functions are used to create the nodes
of the syntax trees for expressions with binary
operators:
1: mknode (op, left, right): creates an operator
node with label op and two fields containing
pointer to left and right.
2: mkleaf (id, entry): creates an identifier node
with label id and a field containing entry, a
pointer to the symbol table entry for the
identifier.
3: mkleaf (num, val): creates a number node with
label num and a field containing val, the
value of the number.
Learn CD: From B K Sharma

Construction of Syntax Trees


for expressions
Steps in the construction of Syntax Tree for
expression a – 4 + c

(1) p1 := mkleaf (id, entrya)


(2) p2 := mkleaf (num, 4)
(3) p3 := mknode ( ‘-’ , p1, p2)
(4) p4 := mkleaf (id, entryc);
(5) p5 := mknode (‘+’ , p3, p4)
p1, p2,.......p5 are pointers to nodes and entrya and
entryc are pointers to symbol table entries for
identifiers for a and c respectively.
Learn CD: From B K Sharma

Construction of Syntax Trees


for expressions: a – 4 + c
p5
+
p4
p3
- id
p1 p2
To entry for c
id num 4

To entry for a
Syntax Tree for expression a – 4 + c
Learn CD: From B K Sharma

Construction of Syntax Trees


for expressions
The syntax tree is constructed bottom up.

The function calls mkleaf(id, entrya) and mkleaf(num,


4) construct leaf for a and 4.

The pointers to these nodes are saved using p1 and p2.

The call mknode(‘-’, p1, p2) then constructs the interior


node with the leaves for ‘a’ and 4 as children.

After two more steps p5 is left pointing to the root.


Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions

A directed acyclic graph(DAG) for an expression


identifies the common sub-expressions in the
expression.
The difference between DAG and Syntax tree is that
a node in a DAG representing a common sub-expression
has more than one “parent”, in a syntax tree, the
common sub-expression would be represented as a
duplicated subtree.
Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions


Expression a + a *(b-c) + (b-c) * d

Syntax Tree DAG


Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions

A DAG is obtained if the function constructing a node


first checks to see whether an identical node already
exists.

For example, before constructing a new node with


label op and fields with pointers to left and right,
mknode(op, left, right) can check whether such a node
has already been constructed.

If so, mknode(op, left, right) can return a pointer to


the previously constructed node.

The mkleaf can behave similarly.


Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions


Syntax Tree for expression a + a *(b-c) + (b-c) * d

+
*
a *
a - - d

b c b c
Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions


Steps in the construction of DAG for expression a + a
*(b-c) + (b-c) * d
Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions


When the call mkleaf(id, a) is repeated on line 2, the
node constructed by the previous call mkleaf(id, a) is
returned, so p1=p2.

Similarly, the nodes returned on lines 8 and 9 are the


same as those returned on lines 3 and 4 respectively.

Hence, the node returned on line 10 must be the same


one constructed by the call of mknode on line 5.
Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions


DAG for expression a + a * (b-c) + (b-c) *d
Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions

Syntax Tree for


a:=b* -c + b * -c

DAG for
a:=b* -c + b * -c
Learn CD: From B K Sharma

Directed Acyclic Graph for Expressions

Question: How DAG is different from Syntax Tree?


Construct DAG for the following expression (a + b) -
(e - (c + d)).
Learn CD: From B K Sharma

Read PPT named: L and S- Attributed SDD

You might also like