COSC 341 Generative grammars - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

COSC 341 Generative grammars

Description:

A grammar is notation containing such clues. Grammars consist of rules (or productions) ... In a context-free grammar, the rules have the form ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 9
Provided by: MOL52
Category:

less

Transcript and Presenter's Notes

Title: COSC 341 Generative grammars


1
COSC 341 Generative grammars
  • Are all languages defined by regular expressions?
  • Increasingly complex languages over a, b
  • L an n 0
  • L anbm n, m 0
  • Even-Even the set of strings with an even
    number of as and an even number of bs
  • L anbn n 0
  • Equal strings with as many as as bs.
  • Some of these have regular expressions
  • a defines an n 0 ,
  • ab defines anbm n, m 0
  • (aa?bb?(ab?ba)(aa?bb)(ab?ba)) defines the
    language Even-Even
  • But no regular expression defines anbn n 0
    or Equal (as we shall see in Ch 7).

2
Grammars
Recursive definitions dont show the difference
between anbm n, m 0 and anbn n 0,
i.e. dont give us an easy way to see that the
former can be defined by a regular expression. A
grammar is notation containing such
clues. Grammars consist of rules (or
productions) S ? a S S ? ? Using the rules
we can generate (or derive) strings of terminals
(i.e. grammatical sentences) S ? a S ? aa
S ? aaa S ? aaa So this grammar defines
(generates) the set of strings L an n 0
?, a, aa, aaa, .
3
Examples of grammars
  • Take ? a, b .
  • L ? with regular expression (a?b) has the
    grammar
  • L anbm n, m 0 with regular expression
    ab has the grammar
  • L anbn n 0 with no regular expression
    has the grammar

4
Derivation trees
To show how a string is generated by a grammar,
we can draw a picture Grammar S ? a S ? ? S ?
a S ? aa S ? aaa S ?
aaa S a S a S a S ?
5
Some puzzles
Is string aabb generated by each of the following
grammars? How? G1 S ? aSb ? ab G2 S ? aS ? bS
? a G3 S ? aB ? bA A ? a ? aS ? bAA B ? b ?
bS ? aBB
6
Two kinds of grammars
In a context-free grammar, the rules have the
form one nonterminal ? finite string of
terminals or non-terminals. Chomsky
(1958) proved languages defined by regular
expressions can all be generated by CFGs in which
the rules satisfy one additional constraint
there is at most one non-terminal on the
righthand side, and if so, it is in rightmost
position. So a regular grammar has rules only of
the form A ? aB A ? a A ? ?. For example S ?
aS ? ? is a regular grammar which generates the
language that could also be defined by the
regular expression a.
7
Playing with regular grammars
  • Let ? a, b .
  • L the set of all strings containing the
    substring aa.
  • L has regular expression (a?b)aa(a?b).
  • Can we find a regular grammar for L?
  • G

8
Combining grammars
Suppose we have regular grammars G1 and G2 for
languages L1 and L2. Is there an easy way to
build a regular grammar for the bigger language
L1 ? L2? Yep. Stick S ? S1 S2 in front, and
then just throw in all the rules of G1 and
G2. Example G1 S1 ? aX X ? aX ? G2 S2
? bY Y ? bY ? Grammar G for L1?L2 S ? S1
S2 S1 ? aX X ? aX ? S2 ? bY Y ?
bY ?
Write a Comment
User Comments (0)
About PowerShow.com