Context-Free Grammars: Variables
Context-Free Grammars: Variables
Text Char
!
Studs !
</ROLL>
Body.
✦ Head is a variable.
✦ Body is a string of zero or more variables
and/or terminals.
Start Symbol = variable that represents \the
language."
Notation: G = (V; ; P; S ) = (variables,
terminals, productions, start symbol).
Example
A simpler example generates strings of 0's and 1's
such that each block of 0's is followed by at least
as many 1's.
S AS
! j
A 0A1 A1 01
! j j
A .!
Aside: Notation
a; b; : : : are terminals; : : :; y; z are strings of
terminals.
Greek letters are strings of variables and/or
terminals, often called sentential forms.
A; B; : : : are variables.
: : :; Y; Z are variables or terminals.
S is typically the start symbol.
Leftmost/Rightmost Derivations
We have a choice of variable to replace at each
step.
✦ Derivations may appear dierent only
because we make the same replacements
in a dierent order.
✦ To avoid such dierences, we may restrict
the choice.
A leftmost derivation always replaces the
leftmost variable in a sentential form.
✦ Yields left-sentential forms.
Rightmost dened analogously.
) , , etc., used to indicate derivations are
lm rm
)
leftmost or rightmost.
2
Example
S lm AS lm A1S lm 011S lm 011AS lm
) ) ) ) )
S rm AS rm AAS rm AA rm A0A1 rm
) ) ) ) )
Derivation Trees
Nodes = variables, terminals, or .
✦ Variables at interior nodes; terminals and
at leaves.
✦ A leaf can be only if it is the only child
of its parent.
A node and its children from the left must
form the head and body of a production.
Example
S
A S
A 1 A S
0 1 0 A 1
0 1
A rm w.
)
4
Subderivation from A is: A A1 011
) )
0A1 0011
)