




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Top-down parsersTypes of parsing
(^) start at the root of derivation tree and fill in
(^) picks a production and tries to match the input
(^) may require backtracking
(^) some grammars are backtrack-free
predictive
Bottom-up parsers
(^) start at the leaves and fill in
(^) start in a state valid for legal first tokens
(^ as input is consumed, change state to encode possibilities recognize valid prefixes
(^) use a stack to store both state and sentential forms
CMSC 430
Lecture 4, Page 1
tree matches the input string. To build a parse, it repeats the following steps until the fringe of the parsethe start symbol or goal symbol of the grammar. A top-down parser starts with the root of the parse tree. It is labeled with Top-down parsing
, select a production with
(^) on its
(^) lhs
(^) and for each
symbol on its
(^) rhs
, construct the appropriate child.
The key is selecting the right production in step 1.
⇒
should be guided by input string
CMSC 430
Lecture 4, Page 2
1 This is a grammar for simple expressions:^ Example grammar
< goal
>
expr
expr
expr
term
expr
term
term
term
term
factor
term
factor
factor
factor
number
id
Consider parsing the input string
(^) x - 2 * y
CMSC 430
Lecture 4, Page 3
One possible parse for Backtracking parse example
(^) x - 2 * y
Prod’n Sentential form
Input
goal
>
x - 2 * y
expr
x - 2 * y
expr
term
x - 2 * y
term
term
x - 2 * y
factor
term
x - 2 * y
id > (^) - (^) < term
x - 2 * y
id > (^) - (^) < term
x ↑
id > (^) - (^) < term
x -
2 * y
id > (^) - (^) < factor
x -
2 * y
id > (^) - (^) < num
>
x -
2 * y
id > (^) - (^) < num
>
x - 2
id > (^) - (^) < term
x -
2 * y
id > (^) - (^) < term
factor
x -
2 * y
id > (^) - (^) < factor
factor
x -
2 * y
id > (^) - (^) < num
> (^) * (^) < factor
x -
2 * y
id > (^) - (^) < num
> (^) * (^) < factor
x - 2
id > (^) - (^) < num
> (^) * (^) < factor
x - 2 *
y
id > (^) - (^) < num
> (^) * (^) < id >
x - 2 *
y
id > (^) - (^) < num
> (^) * (^) < id >
x - 2 * y
CMSC 430
Lecture 4, Page 4
Another possible parse forExample
(^) x - 2 * y
Prod’n Sentential form
Input
goal
>
x - 2 * y
expr
x - 2 * y
expr
term
x - 2 * y
expr
term
term
x - 2 * y
expr
term
term
term
x - 2 * y
expr
term
term
term
x - 2 * y
x - 2 * y
CMSC 430 This isn’t a good property for a parser to have.^ If the parser makes the wrong choices, the expansion doesn’t terminate.
Lecture 4, Page 5
Formally, Top-down parsers cannot handle left-recursion in a grammar. Left recursion
a grammar is
(^) left recursive
(^) if
∃ (^) A
such that
(^) a derivation
Aα
(^) for some string
(^) α .
CMSC 430^ Our simple expression grammar is left recursive.
Lecture 4, Page 6
Consider the grammar fragment: To remove left recursion, we can transform the grammar. Eliminating left recursion
foo >
::=
foo > α
β
where
(^) α (^) and
(^) β (^) do not start with
foo
> .
We can rewrite this as:
foo >
::=
β <
bar
>
bar
>
::=
α <
bar
>
where
bar
> (^) is a new non-terminal.
CMSC 430^ This fragment contains no left recursion.
Lecture 4, Page 7
<^ Our expression grammar contains two cases of left recursion^ Example expr
expr
term
expr
term
term
term
term
factor
term
factor
factor
<^ Applying the transformation gives expr
term
expr
′ >
expr
′ >
::=
term
expr
′ >
term
expr
′ >
term
factor
term
′ >
term
′ >
::=
factor
term
′ >
factor
term
′ >
CMSC 430
Lecture 4, Page 8
1 Example grammarRecursive Descent Parsing
a (^) A
b
(^) c
Helpers match(x) tok; // current token
if (tok != x) error();
tok = getToken();
Parser S()
if (tok == a) match(a); A();
else if (tok == b) match(b);
else error();
S(); match(c);
CMSC 430
Lecture 4, Page 13
Basic idea Predictive Parsing
For any two productions
α | β , we would like a distinct way of
choosing the correct production to expand.
For some FIRST sets
(^) rhs
(^) α
∈
G , define
(^) FIRST
α ) as the set of tokens that appear as the
first symbol in some string derived from
(^) α .
That is,
(^) x
∈ (^) FIRST
α ) (^) iff (^) α
⇒ ∗ xγ
for some
(^) γ .
Whenever two productions LL(1) property
α
and
(^) A
::=
β both appear in the
grammar, we would like
α ) ∩
F IRST
β )
=
Pursuing this idea leads toone symbol! This would allow the parser to make a correct choice with a lookahead of only
(^) predictive
(^) LL(1) parsers.
CMSC 430
Lecture 4, Page 14
Sometimes, we can transform a grammar to have this property. What if a grammar does not have the LL(1) property? Left Factoring
For each non-terminal
(^) find the longest prefix
α (^) common to two or more of its alternatives.
if (^) α
(^) , then replace all of the
(^) productions
αβ 1 | αβ 2 | · · ·
αβ n | γ
with
αL
γ
β (^1) | β 2 | · · ·
β n
where
(^) is a new non-terminal.
non-terminal have a common prefix.Repeat until no two alternatives for a single
CMSC 430
Lecture 4, Page 15
Consider a Example
(^) right-recursive
version of the expression grammar:
goal
expr
expr
term
expr
term
expr
term
term
factor
term
factor
term
factor
factor
number
id
To choose between productions 2, 3, & 4, the parser must see past the
(^) number
or (^) id
(^) and look at the
, or
(^) / .
FIRST
FIRST
FIRST
This grammar
(^) fails
the test.
Note:
This grammar is right-associative.
CMSC 430
Lecture 4, Page 16
<^ There are two nonterminals that must be left factored:Example expr
term
expr
term
expr
term
term
factor
term
factor
term
factor
<^ Applying the transformation gives us: expr
term
expr
′ >
expr
′ >
::=
expr
expr
term
factor
term
′ >
term
′ >
::=
term
term
CMSC 430
Lecture 4, Page 17
Substituting back into the grammar yields Example 1 < goal
expr
expr
term
expr
′ >
expr
′ >
::=
expr
expr
term
factor
term
′ >
term
′ >
::=
term
term
factor
number
id
Note: Now, selection requires only a single token lookahead.
This grammar is still right-associative.
CMSC 430
Lecture 4, Page 18
Example:
Sentential form
Input
—
< goal
>
↑ x - 2 * y
1 < expr
>
↑ x - 2 * y
2 < term
> <
expr
′ >
↑ x - 2 * y
6 < factor
> <
term
′ > <
expr
′ >
↑ x - 2 * y
11
< id > <
term
′ > <
expr
′ >
↑ x - 2 * y
—
< id > <
term
′ > <
expr
′ >
x ↑
9 < id > <
expr
′ >
x ↑
4 < id > (^) - (^) < expr
>
x ↑
—
< id > (^) - (^) < expr
>
x -
↑ 2 * y
2 < id > (^) - (^) < term
> <
expr
′ >
x -
↑ 2 * y
6 < id > (^) - (^) < factor
> <
term
′ > <
expr
′ >
x -
↑ 2 * y
10
< id > (^) - (^) < num
> <
term
′ > <
expr
′ >
x -
↑ 2 * y
—
< id > (^) - (^) < num
> <
term
′ > <
expr
′ >
x - 2
↑
7 < id > (^) - (^) < num
> (^) * (^) < term
> <
expr
′ >
x -
↑
—
< id > (^) - (^) < num
> (^) * (^) < term
> <
expr
′ >
x -2 *
↑ y
6 < id > (^) - (^) < num
> (^) * (^) < factor
> <
term
′ > <
expr
′ > x -2 *
↑ y
11
< id > (^) - (^) < num
> (^) * (^) < id > <
expr
′ >
x -2 *
↑ y
—
< id > (^) - (^) < num
> (^) * (^) < id > <
term
′ > <
expr
′ >
x -2 * y
↑
9 < id > (^) - (^) < num
> (^) * (^) < id > <
expr
′ >
x -2 * y
↑
5 < id > (^) - (^) < num
> (^) * (^) < id >
x -2 * y
↑
CMSC 430^ The next symbol determined each choice correctly.
Lecture 4, Page 19
Question: Generality
By (^) eliminating left recursion
(^) and
(^) left factoring
, can we transform an
parsed with a single token lookahead?arbitrary context free grammar to a form where it can be predictively
Answer:
conditions.undecidable whether an equivalent grammar exists that does meet our Given a context free grammar that doesn’t meet our conditions, it is
Many
(^) context free languages
do not have such a grammar.
a n 0 b n | n ≥
1 } ⋃ { a n 1 b 2 n | n ≥
1 }
CMSC 430
Lecture 4, Page 20
The
(^) FOLLOW
(^) construction
rule
FOLLOW
goal
eof
eof
}
expr
eof
eof
}
expr
′
eof
eof
}
term
eof
eof,+,-
term
′
eof,+,-
eof,+,-
factor
eof,+,-
eof,+,-,*,/
CMSC 430
Lecture 4, Page 25
Using
(^) FIRST
and
(^) FOLLOW
For each production To build a predicative recursive-descent parser:
(^) α (^) and lookahead
(^) token
(^) expand
(^) using production if
(^) token
(^) FIRST
(α )
(^) if (^) (^) ∈ (^) FIRST
α ) expand
(^) using production if
(^) token
(^) FOLLOW
(^) all other tokens return
(^) error
If multiple choices, the grammar is not
(^) LL(1)
(^) (predicative).
id
num
/
eof
〈 goal
〉
g (^) → (^) e
g (^) → (^) e
〈expr
〉
e (^) → (^) te ′ e (^) → (^) te ′ – – – – –
〈expr
〉′
e ′ → (^) + e e ′ → (^) - e
e ′ → (^)
〈term
〉 t → (^) f t ′ t → (^) f t ′ – – – – –
〈term
〉′^
t′ →^ (^)
t′ →^ (^)
t′ →^ (^) * t t′ →^ (^) / t t′ →^ (^)
〈 factor
〉 f (^) → (^) id
f (^) → (^) num
CMSC 430
Lecture 4, Page 26
Features LL(1) grammars
(^) input parsed from left to right
(^) leftmost derivation
(^) one token lookahead
Definition
A grammar
(^) is (^) LL(1)
(^) if and only if, for all non-terminals
, each distinct
pair of productions
(^) β (^) and
(^) γ satisfy the condition
FIRST
β ) ⋂ FIRST
(γ ) =
(^) ∅
A grammar
(^) is (^) LL
(1) if and only if for each set of productions
(^) α (^1) | α (^2) | · · · |
(^) α n
(^) FIRST
α 1 ) , FIRST
α 2 ) , · · · (^) , (^) FIRST
α n ) are all pairwise disjoint
(^) α i ⇒ ∗ , then
(^) FIRST
α j ) ⋂ FOLLOW
, for all 1
(^) j ≤ (^) n, i
j .
If (^) G (^) is (^) –free, condition 1 is sufficient.
CMSC 430
Lecture 4, Page 27
Provable facts about LL(1) grammars
LL(1)
(^) grammars:
(^) no left recursive grammar is
(^) LL(1)
(^) no ambiguous grammar is
(^) LL(1)
(^) LL(1)
parsers operate in linear time
(^) an
(^) –free grammar where each alternative expansion for
(^) begins with a
distinct terminal is a
(^) simple
(^) LL(1)
(^) grammar
Not all grammars are
LL(1)
(^) aS
(^) | (^) a
is not
(^) LL(1)
FIRST
aS ) =
(^) FIRST
(a ) =
(^) { a }
(^) aS
′
′ ::=
(^) aS
′ |
accepts the same language and is
(^) LL(1)
CMSC 430
Lecture 4, Page 28
LL(1)LL grammars
(^) grammars
(^) may need to rewrite grammar (left recursion, left factoring)
(^) resulting grammar larger, less maintainable
LL(k)
(^) grammars
(^) k -token lookahead, more powerful than
(^) LL(1)
(^) grammars
S^ example: ::= (^) ac
(^) | (^) abc
(^) is (^) LL(2)
Not all grammars are
LL(k)
S^ example: ::= (^) a (^) bi j
where
(^) i (^) ≥ (^) j
(^) equivalent to dangling else problem
(^) problem - must choose production after
(^) k tokens of lookahead
CMSC 430^ Bottom-up parsers avoid this problem
Lecture 4, Page 29