Context Free Grammars CSCI 432 Computer Science Theory

  • Slides: 14
Download presentation
Context Free Grammars CSCI 432 Computer Science Theory

Context Free Grammars CSCI 432 Computer Science Theory

Why Study CFGs ■ What is "grammar"? ■ What does grammar have to do

Why Study CFGs ■ What is "grammar"? ■ What does grammar have to do with computing? ■ Examples of grammar in computing?

Terminology S → b. A A → a. A A→e Those are three rules

Terminology S → b. A A → a. A A→e Those are three rules or productions. The starting rule is usually the first rule listed. S and A are nonterminals. a and b are terminals. Only a single nonterminal appears on the left side of a rule.

Example S → b. A 1 A → a. A 2 A→e 3 Suppose

Example S → b. A 1 A → a. A 2 A→e 3 Suppose we want to generate the word "baaa" using this grammar. S b. A baaa 1 2 2 2 3 textbook page 70

Non Context Free Grammar a. Ab → acb How the rule for A is

Non Context Free Grammar a. Ab → acb How the rule for A is used depends on whether or not it is surrounded by an a and a b. Thus, that rule is not free of context.

Nondeterminism B → b. B B→b We cannot determine which version of B to

Nondeterminism B → b. B B→b We cannot determine which version of B to use to generate the desired word. We have to try possibilities until we generate the desired word. Thus, using CFGs to generate words is nondeterministic. textbook page 72

Example 1 L(G) = {anbn} S → a. Sb S→e S a. Sb aa.

Example 1 L(G) = {anbn} S → a. Sb S→e S a. Sb aa. Sbb aaa. Sbbb aaabbb So… Can a CFG represent a non-regular language? All regular languages can be represented with a CFG. textbook example 3. 1. 1

Example 2 a*b* S → a. S S → Sb S→e These 3 rules

Example 2 a*b* S → a. S S → Sb S→e These 3 rules could also be written as: S → a. S | Sb | e So how would we generate the word aaabb? aaabb S aaa. Sbb S Sb aa. Sbb aaa. Sbb textbook example 3. 1. 2

Example 3 What does this grammar generate? S → a. Sa | b. Sb

Example 3 What does this grammar generate? S → a. Sa | b. Sb | e For example S a. Sa aa. Saa aab. Sbaa aaba. Sabaa aabaabaa textbook example 3. 1. 3

Example 4 Binary string with even number of 0's. 1 S → 1 S

Example 4 Binary string with even number of 0's. 1 S → 1 S S 2 S → 0 A 0 S 3 S→e 01 A 0 S 011 A 0 S 4 A → 1 A 0111 A 0 S 5 A→e 01110 Try to generate the string 01110 2 4 4 4 3, 5

Practice Binary string with even number of 0's. 1 S → 1 S S

Practice Binary string with even number of 0's. 1 S → 1 S S 2 S → 0 A 0 S 1 S 3 S→e 11 S 110 A 0 S 4 A → 1 A 1101 A 0 S 5 A→e 11010 S Generate the string 11010 1 1 2 4 3 5

Practice Use the following grammar to produce the string "the dog eats the bone".

Practice Use the following grammar to produce the string "the dog eats the bone". 1 2 3 4 5 S → NP VP NP → the N VP → V NP V → eats | buries N → dog | bone

Grammatically Correct ≠ Meaningful We could also use this grammar to generate the string

Grammatically Correct ≠ Meaningful We could also use this grammar to generate the string "the bone buries the dog". 1 2 3 4 5 S → NP VP NP → the N VP → V NP V → eats | buries N → dog | bone

Using Grammars ■ How do we use a grammar to check the validity of

Using Grammars ■ How do we use a grammar to check the validity of data or a source code file? ■ Bad Answer : generate lots of possibilities until you generate a match. Then you know the data matches the grammar. ■ Good Answer : Parsing.