3-Module 2 - Role of Parser - Parse Tree-02-08-2024
3-Module 2 - Role of Parser - Parse Tree-02-08-2024
S
S Parse tree
represents the
S-S structure of S - S
derivation
S*S-S
S * S a
a*S-S
a*a-S a a
a*a-a Parse tree
Leftmost
Derivation
Rightmost derivation
• A derivation of a string in a grammar is a right most
derivation if at every step the right most non terminal is
replaced.
• It is all called canonical derivation.
• Grammar: SS+S | S-S | S*S | S/S | a Output string: a*a-a
S
S
S*S S * S
S*S-S
a S - S
S*S-a
S*a-a a a
Rightmosta*a-a Parse Tree
Derivation
Exercise: Derivation
1. Perform leftmost derivation and draw parse
tree.
A0A | 𝜖
SA1B
B0B | 1B | 𝜖
Output string: 1001
2. Perform leftmost derivation and draw parse
tree.
S0S1 | 01 Output string: 000111
3. Perform rightmost derivation and draw parse
tree.
EE+E | E*E | id | (E) | -E
bbaababa
Ambiguity
The general rule is, Match each else with the closest
unmatched then."
𝐴→ 𝐴𝛼∨¿𝛽
𝛽 𝐴’
𝛼𝐴𝜖’
E → E + T|T
T → T * F|F
F → (E)|id
E → TE′ E → (T)E′|ε T → FT′ T′ → (F)T′|ε F → id
E → E(T)|T
T → T(F)|F
F → id
E → TE′
E → (T)E′|ε
T → FT′
T′ → (F)T′|ε
F → id
Examples: Left recursion
elimination
EE+T | T
ETE’
E’+TE’ | ε
TT*F | F
TFT’
T’*FT’ | ε
XX%Y | Z
XZX’
X’%YX’ | ε
Exercise: Left recursion
1. AAbd | Aa | a
BBe | b
2. AAB | AC | a | b
3. SA | B
AABC | Acd | a | aa
BBee | b
4. ExpExp+term | Exp-term | term
Left Factoring
Left factoring is a grammar transformation that is useful
for producing a grammar suitable for predictive, or top-
down, parsing. When the choice between
two alternative A-productions is not clear, we may be able
to rewrite the productions to defer the decision until
enough of the input has been seen that we
can make the right choice.
Left factoring
Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive parsing.
Algorithm to left factor a grammar
Input: Grammar G
Output: An equivalent left factored grammar.
Method:
For each non terminal A find the longest prefix common to two or
more of its alternatives. If , i.e., there is a non trivial common prefix,
replace all the productions where represents all alternatives that
do not begin with by
A xByAA’ | a
A’ Є | zA
A aAB | aA |a
A’AB | A | 𝜖
AaA’
A’AA’’ | 𝜖
A’’B | 𝜖
Syntax Error Handling
Common programming errors can occur at many different levels.
Lexical errors include misspellings of identifiers, keywords, or operators
e.g., the use of an identifer elipseSize instead of ellipseSize and
missing quotes around text intended as a string.
Syntactic errors include misplaced semicolons or extra or missing braces;
that is, “{" or “}."
As another example, in C or Java, the appearance of a case statement
without an enclosing switch is a syntactic error
(however, this situation is usually allowed by the parser and caught later
in the processing, as the compiler attempts to generate code).
Semantic errors include type mismatches between operators and
operands,
e.g., the return of a value in a Java method with result type void.
Logical errors can be anything from incorrect reasoning on the part of
the programmer to the use in a C program of the assignment operator =
instead of the comparison operator ==. The program containing = may
be well formed; however, it may not reflect the programmer's intent.
The error handler in a parser has goals that are
simple to state but challenging to realize:
This may allow the parser to make minimal changes in the source
code, but due to the complexity (time and space) of this strategy, it
has not been implemented in practice yet.