Академический Документы
Профессиональный Документы
Культура Документы
Formalism
Derivations
Backus-Naur Form
Left- and Rightmost Derivations
Informal Comments
A
Basic
S -> 01
S -> 0S1
Basis:
01 is in the language.
Induction: if w is in the language, then so
is 0w1.
CFG Formalism
Terminals
Productions
A
A, B, C, are variables.
a, b, c, are terminals.
, X, Y, Z are either terminals or variables.
, w, x, y, z are strings of terminals only.
, , , are strings of terminals and/or variables.
Here
Derivations Intuition
We
Derivations Formalism
We
10
Iterated Derivation
=>*
11
12
Sentential Forms
Any
13
Language of a Grammar
If
Example:
> 0S1.
L(G) = {0n1n | n > 0}.
Note: is a legitimate
right side.
14
Context-Free Languages
24
25
Leftmost Derivations
Say
wA =>lm w if w is a string of
terminals only and A -> is a production.
Also, =>*lm if becomes by a
sequence of 0 or more =>lm steps.
26
Balanced-parentheses
grammmar:
S -> SS | (S) | ()
S =>lm SS =>lm (S)S =>lm (())S =>lm (())()
Thus, S =>*lm (())()
S => SS => S() => (S)() => (())() is a derivation, but
not a leftmost derivation.
27
Rightmost Derivations
Say
Aw =>rm w if w is a string of
terminals only and A -> is a production.
Also, =>*rm if becomes by a
sequence of 0 or more =>rm steps.
28
grammmar:
S -> SS | (S) | ()
S =>rm SS =>rm S() =>rm (S)() =>rm (())()
Thus, S =>*rm (())()
S => SS => SSS => S()S => ()()S => ()()() is neither a
rightmost nor a leftmost derivation.
29
Parse Trees
Definitions
Relationship to Left- and Rightmost Derivations
Ambiguity in Grammars
30
31
Parse Trees
Parse
Root:
32
33
S
(
)
)
43
Ambiguous Grammars
A
44
Example
Continued
S
S
S
45
46
47
Ambiguity is a Property of
Grammars, not Languages
For
R -> ) | (RR
R generates strings that
have one more right paren
than left.
48
R -> ) | (RR
Construct
49
Remaining Input:
(())()
Steps of leftmost
derivation:
B
Next
symbol
B -> (RB |
R -> ) | (RR
50
Remaining Input:
())()
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(RB
R -> ) | (RR
51
Remaining Input:
))()
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(RB
((RRB
R -> ) | (RR
52
Remaining Input:
)()
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(RB
((RRB
(()RB
R -> ) | (RR
53
Remaining Input:
()
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(RB
((RRB
(()RB
(())B
R -> ) | (RR
54
Remaining Input:
)
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(())(RB
(RB
((RRB
(()RB
(())B
R -> ) | (RR
55
Remaining Input:
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(())(RB
(RB
(())()B
((RRB
(()RB
(())B
R -> ) | (RR
56
Remaining Input:
Next
symbol
B -> (RB |
Steps of leftmost
derivation:
B
(())(RB
(RB
(())()B
((RRB
(())()
(()RB
(())B
R -> ) | (RR
57
LL(1) Grammars
As
58
Most