28/08/2024, 13:51 Parse Tree in Compiler Design - GeeksforGeeks
Aptitude Engineering Mathematics Discrete Mathematics Operating System DBMS Computer Networks
Parse Tree in Compiler Design
Last Updated : 14 Jul, 2022
Here we will study the concept and uses of Parse Tree in Compiler Design.
First, let us check out two terms :
Parse : It means to resolve (a sentence) into its component parts and
describe their syntactic roles or simply it is an act of parsing a string or a
text.
Tree: A tree may be a widely used abstract data type that simulates a
hierarchical tree structure, with a root value and sub-trees of youngsters
with a parent node, represented as a group of linked nodes.
Parse Tree:
Parse tree is the hierarchical representation of terminals or non-terminals.
These symbols (terminals or non-terminals) represent the derivation of
the grammar to yield input strings.
In parsing, the string springs using the beginning symbol.
The starting symbol of the grammar must be used as the root of the
Parse Tree.
Leaves of parse tree represent terminals.
Each interior node represents productions of a grammar.
Rules to Draw a Parse Tree:
1. All leaf nodes need to be terminals.
2. All interior nodes need to be non-terminals.
3. In-order traversal gives the original input string.
Example 1: Let us take an example of Grammar (Production Rules).
S -> sAB
A -> a
B -> b
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
The input string is “sab”, then Policy the Parse Tree is:
https://www.geeksforgeeks.org/parse-tree-in-compiler-design/ 1/7
28/08/2024, 13:51 Parse Tree in Compiler Design - GeeksforGeeks
Example-2: Let us take another example of Grammar (Production Rules).
S -> AB
A -> c/aA
B -> d/bB
The input string is “acbd”, then the Parse Tree is as follows:
Uses of Parse Tree:
It helps in making syntax analysis by reflecting the syntax of the input
language.
It uses an in-memory representation of the input with a structure that
conforms
We use cookies to ensuretoyou
the
havegrammar.
the best browsing experience on our website. By using
The
our site, you advantages
acknowledge that youofhave
using parse
read and treesour
understood rather
Cookiethan
Policy semantic
& Privacy actions: you’ll
make multiple passes Policy over the info without having to re-parse the input.
https://www.geeksforgeeks.org/parse-tree-in-compiler-design/ 2/7