Professional Documents
Culture Documents
Introduction To Grammars and Parsing Techniques PDF
Introduction To Grammars and Parsing Techniques PDF
Grammars and
Parsing Techniques
Paul Klint
p o s i t i o n : = i n i t i a l + r a t e * 6 0
[a-z0-9]*
[a-z]
Start End
p o s i t i o n : = i n i t i a l + r a t e * 6 0
Expression
Expression
Expression expression
Expression Expression
Identifier
identifier Identifier Identifier Number
p o s i t i o n : = i n i t i a l + r a t e * 6 0
Introduction to Grammars and Parsing Techniques 20
Ambiguity:
one sentence, but several trees
Expression
Expression
Expression Expression
1 + 2 * 3 1 + 2 * 3
Expression Term
Term Term
1 + 2 * 3
l1 -> r1
l2 -> r2
...
ln -> rn
Source text
Recognizer Yes or No
l1 -> r1
l2 -> r2
...
ln -> rn
Source text
Parse tree or Errors
Parser
Expression Expression
Expression Expression
4 * 5 + 6 1 + 2 * 3
Term(){ if(isdigit(lookahead)){
Match(lookahead);
} else
Error();
} Introduction to Grammars and Parsing Techniques 32
A Trickier Case: Backtracking
● Example
● S -> aSbS | aS | c
● Naive approach (input ac):
● Try aSbS, but this fails hence error
● Backtracking approach:
● First try aSbS but this fails
● Go back to initial input position and try aS, this
succeeds.
Syntax of L
Parser
Generator
L
L text
Parser tree
for L