1 lecture 29 context free grammars –examples of “real-life” grammars –definition of a...

18
1 Lecture 29 • Context Free Grammars – Examples of “real-life” grammars – Definition of a grammar G – Deriving strings and defining L(G) • Context-Free Language definition

Upload: ezra-chambers

Post on 13-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

1

Lecture 29

• Context Free Grammars– Examples of “real-life” grammars– Definition of a grammar G– Deriving strings and defining L(G)

• Context-Free Language definition

Page 2: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

2

Grammars

Examples of “real-life” grammars

Page 3: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

3

English Grammar• The underdog Spartans shock the world.

– Diagram this sentence• The underdog Spartans shock the world.

– <noun phrase> <verb phrase> <pct>

• The underdog Spartans shock the world.– <article> <adj> <noun> <verb><object><pct>

• Can we generalize these rules?– <sentence> --> <noun phrase> <verb phrase> <pct>| ...– <noun phrase> --> <article> <adj> <noun> | <noun> | ...– <verb phrase> --> <verb> <object> | <verb> <adverb> | ...– <noun> --> Spartans | man | boy | woman | ...– <verb> --> shock | run | catch | ...– ...

Page 4: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

4

“Deriving” a legal sentence• Grammar rules:

– <sentence> --> <noun phrase> <verb phrase> <pct>| ...– <noun phrase> --> <article> <adj> <noun> | <noun> | ...– <verb phrase> --> <verb> <object> | <verb> <adverb> | ...– <noun> --> Spartans | man | boy | woman | ...– <verb> --> shock | run | catch | ...– ...

• <sentence> ==> <noun phrase> <verb phrase> <pct>– ==> <article> <adj> <noun> <verb phrase> <pct>– ==> <article> <adj> <noun> <verb> <object> <pct>– ==> ...

Page 5: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

5

C++ Grammar• points = td*6 + xp + x2p*2 + fg*3 + safety * 2;

– Diagram this statement• points = td*6 + xp + x2p*2 + fg*3 + safety * 2;

– <variable> = <expression> ;– ...

• Can we generalize these rules?– <statement> --> <assignment statement> | <if-then statement> | ...

– <assignment statement> --> <variable> = <expression>;

– <variable> --> points | td | xp | x2p | fg | safety | ...

– <expression> --> <expression> + <expression> | <variable> | ...

– ...

Page 6: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

6

“Deriving” a legal statement• Grammar Rules:

– <statement> --> <assignment statement> | <if-then statement> | ...

– <assignment statement> --> <variable> = <expression>;

– <variable> --> points | td | xp | x2p | fg | safety | ...

– <expression> --> <expression> + <expression> | <variable> | ...

– ...

• <statement> ==> <assignment statement>– ==> <variable> = <expression> ;

– ==> points = <expression>;

– ==> points = <expression> + <expression>;

– ...

Page 7: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

7

Context-Free Grammars

Definition

Page 8: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

8

Definition

• A context-free grammar G = (V, , S, P)– V: finite set of variables (nonterminals)

– : finite set of characters (terminals)

– S: start variable• element of V

• role is similar to that of q0 for an FSA or NFA

– P: finite set of grammar rules or production rules• Syntax of a production

• variable --> string of variables and terminals

Page 9: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

9

English Context-Free Grammar

• ECFG = (V, , S, P)– V = {<sentence>, <noun phrase>, <verb phrase>, ... }

• people sometimes use < > to delimit variables

• In this course, we generally will use capital letters to denote variables

– = {a, b, c, ..., z, ;, ,, ., ...}

– S = <sentence>

– P = { <sentence> --> <noun phrase> <verb phrase> <pct>, <noun phrase> --> <article> <adj> <noun>, ...}

Page 10: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

10

{aibi | i>0} CFG

• ABG = (V, , S, P)– V = {S}

– = {a, b}

– S = S

– P = {S --> aSb, S --> ab} or S --> aSb | ab• second format saves some space

Page 11: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

11

Context-Free Grammars

Deriving strings, defining L(G), and defining context-free languages

Page 12: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

12

Defining -->, ==> notation• First: --> notation

– This is used to define the productions of a grammar• S --> aSb | ab

• Second: ==>G notation

– This is used to denote the application of a production rule from a grammar G

• S ==>ABG aSb ==>ABG aaSbb ==>ABG aaabbb

– We say that string S derives string aSb (in one step)

– We say that string aSb derives string aaSbb (in one step)

– We say that string aaSbb derives string aaabbb (in one step)

• We often omit the subscript G when the intended grammar is unambiguous

Page 13: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

13

Defining ==> continued

• Third: ==>kG notation

– This is used to denote k applications of production rules from a grammar G

• S ==>2ABG aaSbb

– We say that string S derives string aaSbb in two steps

• aSb ==>2ABG aaabbb

– We say that string aSb derives string aaabbb in two steps

• We often omit the subscript G when the intended grammar is unambiguous

Page 14: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

14

• Fourth: ==>*G notation

– This is used to denote 0 or more applications of production rules from a grammar G

• S ==>*ABG S

– We say that string S derives string S in 0 or more steps

• S ==>*ABG aaSbb

– We say that string S derives string aaSbb in 0 or more steps

• aSb ==>*ABG aaSbb

– We say that string aSb derives string aaSbb in 0 or more steps

• aSb ==>*ABG aaabbb

– We say that string aSb derives string aaabbb in 0 or more steps

• We often omit the subscript G when the intended grammar is unambiguous

Defining ==> continued

Page 15: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

15

• Derivation of a string x– The complete step by step derivation of a string x from

the start variable S

– Key fact: each step in a derivation makes only one application of a production rule from G

– Example: Derivation of string aaabbb using ABG• S ==>ABG aSb ==>ABG aaSbb ==>ABG aaabbb

– Example 2: AG= (V, , S, P) where P = S -->SS | a• Deriving string aaa

• S ==> SS ==> Sa ==> SSa ==> aSa ==> aaa

Defining derivations

Page 16: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

16

• Generating strings– If S ==>G

* x, then grammar G generates string x• Note G generates strings which contain terminals and nonterminals

– aSb contains nonterminals and terminals– S contains only nonterminals– aaabbb contains only terminals

• L(G)– The set of strings over generated by grammar G

• Note we only consider terminal strings generated by G

– {aibi | i > 0} = L(ABG)– {ai | i > 0} = L(AG)

Defining L(G)

Page 17: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

17

• Context-Free Languages– A language L is a context-free language (CFL) iff there

exists a CFG G such that L(G) = L

• Results so far– {ai | i > 0} is a CFL

• One CFG G such that L(G) = this language is AG

• Note this language is also regular

– {aibi | i > 0} is a CFL• One CFG G such that L(G) = this language is ABG

• Note this language is NOT regular

Context-Free Languages

Page 18: 1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language

18

Example

• Let BAL = the set of strings over {(,)} in which the parentheses are balanced

• Prove that BAL is a CFL– To prove this, you need to come up with a CFG BALG

such that L(BALG) = BAL• BALG = (V, , S, P)

– V = {S}

– = {(, )}

– S = S

– P = ?

• Give derivations of ((( ))) and ( )(( )) with your grammar