2. • 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.
Topic :Parsing and Parse Tree
3. • Parse tree is the graphical representation of symbol. The symbol can be terminal
or non-terminal.
• In parsing, the string is derived using the start symbol. The root of the parse tree
is that start symbol.
• It is the graphical representation of symbol that can be terminals or non-
terminals.
• Parse tree follows the precedence of operators. The deepest sub-tree traversed
first. So, the operator in the parent node has less precedence over the operator in
the sub-tree.
Parse Tree
4. •All leaf nodes have to be terminals.
• All interior nodes have to be non-terminals.
• In-order traversal gives original input string.
Rules To Draw Parse Tree
5. • Example 1: Let us take an example of Grammar (Production Rules).
• The input string is “sab”, then the Parse Tree is:
Examples Of Parse Tree
6. • Example-2: Let us take another example of Grammar (Production Rules).
• The input string is “acbd”,
then the Parse Tree is as follows:
Examples Of Parse Tree
7. • 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 to the grammar.
• The advantages of using parse trees rather than semantic actions:
you’ll make multiple passes over the info without having to re-parse
the input.
Use Of Parse Tree
8. • A grammar is said to be ambiguous if there exists more than one leftmost
derivation or more than one rightmost derivative or more than one parse
tree for the given input string. If the grammar is not ambiguous then it is
called unambiguous.
Ambiguity
9. • Example 1:
• For the string aabb, the above grammar generates two parse trees:
• If the grammar has ambiguity then it is not good for a compiler construction. No method can
automatically detect and remove the ambiguity but you can remove ambiguity by re-writing the
whole grammar without ambiguity.
Examples Of Ambiguity
10. • Derivations mean replacing a given string’s non-terminal by the right-hand side of
the production rule. The sequence of applications of rules that makes the
completed string of terminals from the starting symbol is known as derivation. The
parse tree is the pictorial representation of derivations. Therefore, it is also known
as derivation trees. The derivation tree is independent of the other in which
productions are used.
• A parse tree is an ordered tree in which nodes are labeled with the left side of the
productions and in which the children of a node define its equivalent right parse
tree also known as syntax tree, generation tree, or production tree.
Derivation Tree
12. • Example 1:
• Solution:
• Yield = Left to Right Ordering of Leaves = aa bb aa
Derivation Tree Example
13. • The process of deriving a string by expanding the
leftmost non-terminal at each step is called as leftmost
derivation.
• The geometrical representation of leftmost derivation is
called as a leftmost derivation tree.
Leftmost Derivation Tree
14. • Example 1:
• Solution:
• Leftmost Derivation
• Let us consider a string w = aaabbabbba
• Now, let us derive the string w using leftmost derivation.
Leftmost Derivation Tree
16. • The process of deriving a string by expanding the
rightmost non-terminal at each step is called
as rightmost derivation.
• The geometrical representation of rightmost
derivation is called as a rightmost derivation
tree.
Rightmost Derivation Tree
17. • Example 1:
• Solution:
• Rightmost Derivation
• Let us consider a string w = aaabbabbba
• Now, let us derive the string w using rightmost derivation.
Rightmost Derivation Tree