Title: Grammar
1Grammar
2Application of a production ?A????? in a
derivation step ?i ? ?i1
3Formal grammars (1/3)
- Example Let G1 have N A, B, C, T a, b,
c and the set of productions - ? ? A CB ? BC
- A ? aABC bB ? bb
- A ? abC bC ? bc
- cC ? cc
- The reader should convince himself that the word
akbkck is in L(G1) for all k ? 1 and that only
these words are in L(G1). That is, - L(G1) akbkck k ? 1.
4Formal grammars (2/3)
- Example Grammar G2 is a modification of G1
- G2 ? ? A CB ? BC
- A ? aABC bB ? bb
- A ? abC bC ? b
- The reader may verify that L(G2) akbk k ?
1. Note that the last rule, bC ? b, erases all
the C's from the derivation, and that only this
production removes the nonterminal C from
sentential forms.
5Formal grammars (3/3)
- Example A simpler grammar that generates akbk
k ? 1 is the grammar G3 - G3 ? ? S
- S ? aSb
- S ? ab
- A derivation of a3b3 is
- ? ? S ? aSb ? aaSbb ? aaabbb
- The reader may verify that L(G3) akbk k ?
1.
6Type Format of Productions Remarks
0 fA?? f? ? Unrestricted Substitution Rules
1 fA?? f? ?, ??? ??? Context Sensitive Context Free Right Linear Left Linear
2 A ??, ??? ??? Context Sensitive Context Free Right Linear Left Linear
3 A?aB A?a ??? A?Ba A ?a ??? Context Sensitive Context Free Right Linear Left Linear
Contracting
Noncon- tracting
Regular
The four types of formal grammars
7Context-Sensitive Grammars(Type1)
Unrestricted Grammars(Type0)
- Definition A context-sensitive grammar G
(N,T,P,?) is a formal grammar in which all
productions are of the form - fA??f??, ?? ?
- The grammar may also contain the production ?
??, if G is a context-sensitive (type1) grammar,
then L(G) is a context-sensitive (type1) language.
8Context-Free Grammars (Type2)
- Definition A context-free grammar G(N,T,P,?)
is a formal grammar in which all productions are
of the form - A??
- The grammar may also contain the production ?
??. If G is a context-free (type2) grammar, then
L(G) is a context-free (type2) language.
A?N?? ??(N?T)-?
9Regular Grammars (Type3) (1/2)
- Definition A production of the form
- A?aB or A?a
- is called a right linear production. A
production of the form - A?Ba or A?a
- is a left linear production. A formal grammar is
right linear if it contains only right linear
productions, and is left linear if it contains
only left linear production ? ??. Left and right
linear grammars are also known as regular
grammars. If G is a regular (type3) grammar, then
L(G) is a regular (type3) language.
A?N?? B?N a?T
A?N?? B?N a?T
10Regular Grammars (Type3) (2/2)
- Example A left linear grammar G1 and a right
linear grammar G2 have productions as follows - G1 G2
- The reader may verify that
- L(G1) (10)11(01)L(G2)
? ? 1B ? ? 1 A ? 1B B ? 0A A ? 1
? ? B1 ? ? 1 A ? B1 B ? A0 A ? 1
11Ambiguity (1/2)
- Example Consider the context-free grammar
- G ? ? S
- S ? SS
- S ? ab
- We see that the derivations correspond to
different tree diagrams. The grammar G is
ambiguous with respect to the sentence ababab if
the tree diagrams were used as the basis for
assigning meaning to the derived string, mistaken
interpretation could result.
12Ambiguity (2/2)
- Definition A context-free grammar is ambiguous
if and only if it generates some sentence by two
or more distinct leftmost derivations.