Syntax Tree | Compiler Design - Computer Science Engineering (CSE) PDF Download

Introduction

Tree in which each leaf node describes an operand & each interior node an operator. The syntax tree is shortened form of the Parse Tree.

Example 1: Draw Syntax Tree for the string a + b ∗ c − d.
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)Rules for constructing a syntax tree
Each node in a syntax tree can be executed as data with multiple fields. In the node for an operator, one field recognizes the operator and the remaining field includes a pointer to the nodes for the operands. The operator is known as the label of the node. The following functions are used to create the nodes of the syntax tree for the expressions with binary operators. Each function returns a pointer to the recently generated node.

  • mknode (op, left, right): It generates an operator node with label op and two field including pointers to left and right.
  • mkleaf (id, entry): It generates an identifier node with label id and the field including the entry, a pointer to the symbol table entry for the identifier.
  • mkleaf (num, val): It generates a number node with label num and a field including val, the value of the number. For example, construct a syntax tree for an expression a − 4 + c. In this sequence, p1, p2, … . . pare pointers to the symbol table entries for identifier 'a' and 'c' respectively.

p1− mkleaf (id, entry a);

p− mkleaf (num, 4);

p− mknode ( ′−′, p1, p2)

p4 − mkleaf(id, entry c)

p5 − mknode(′+′, p3, p4);

The tree is generated in a bottom-up fashion. The function calls mkleaf (id, entry a) and mkleaf (num 4) construct the leaves for a and 4. The pointers to these nodes are stored using p1 and p2. The call mknodes (′−′, p1, p2) then make the interior node with the leaves for a and 4 as children. The syntax tree will be
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)

Syntax Directed Translation of Syntax Trees
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)

Node (+, 𝐄(𝟏), 𝐕𝐀𝐋, 𝐄(𝟐). 𝐕𝐀𝐋) will create a node labeled +.
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)E(1). VAL &E(2). VAL are left & right children of this node.
Similarly, Node (∗, E(1). VAL, E(2). VAL) will make the syntax as −
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)Function UNARY (−, E(1). VAL)will make a node – (unary minus) & E(1). VAL will be the only child of it.
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)Function LEAF (id) will create a Leaf node with label id.
Syntax Tree | Compiler Design - Computer Science Engineering (CSE)


Example 2: Construct a syntax tree for the expression.
a = b ∗ −c + d

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)


Example 3: Construct a syntax tree for a statement.
If a = b then b = 2 * c

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)


Example 4: Consider the following code. Draw its syntax Tree
If x > 0 then x = 2 * (a + 1) else x = x + 1.

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)


Example 5: Draw syntax tree for following arithmetic expression a * (b + c) – d /2. Also, write given expression in postfix form.

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)Postfix Notation
a b c + * d 2 / -

The document Syntax Tree | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

past year papers

,

ppt

,

pdf

,

Summary

,

Semester Notes

,

MCQs

,

Viva Questions

,

Important questions

,

Free

,

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)

,

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)

,

video lectures

,

Extra Questions

,

study material

,

practice quizzes

,

mock tests for examination

,

Objective type Questions

,

shortcuts and tricks

,

Exam

,

Sample Paper

,

Syntax Tree | Compiler Design - Computer Science Engineering (CSE)

;