42
Context Free Grammar Context Free Grammar  A context free grammar defines a language.  A context free grammar defines a language.  The language is described by a set of The language is described by a set of sequence of tokens. sequence of tokens.  Any sequence of tokens that can be derived  Any sequence of tokens that can be derived using the grammar is valid (syntactically using the grammar is valid (syntactically  valid)  valid) CFGs have been used as a basis of the CFGs have been used as a basis of the syntax analysis phase of compilation syntax analysis phase of compilation

context free grammar181007800

Embed Size (px)

Citation preview

Page 1: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 1/42

Context Free GrammarContext Free Grammar

 A context free grammar defines a language. A context free grammar defines a language.

 The language is described by a set of  The language is described by a set of 

sequence of tokens.sequence of tokens.  Any sequence of tokens that can be derived Any sequence of tokens that can be derived

using the grammar is valid (syntactically using the grammar is valid (syntactically 

 valid) valid) CFGs have been used as a basis of theCFGs have been used as a basis of the

syntax analysis phase of compilationsyntax analysis phase of compilation

Page 2: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 2/42

Context Free GrammarContext Free Grammar

Context-free grammars play a central role in the

description and design of programming 

languages and compilers. They are also used for analyzing the syntax of 

natural languages.

Page 3: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 3/42

Example to understand the ContextExample to understand the Context

Free GrammarFree Grammar A Palindrome is a string that reads the A Palindrome is a string that reads the

same forward and backward i.e. w = w same forward and backward i.e. w = w R R 

Eg 001100, 01110, 100001, or 101101101Eg 001100, 01110, 100001, or 101101101are the palindrome strings.are the palindrome strings.

Let us denote this language by LLet us denote this language by Lpalpal

 This can be written as L This can be written as Lpalpal ={w | w = w ={w | w = w R R }}

Page 4: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 4/42

Class AssignmentClass Assignment

Use Pumping Lemma to prove that LUse Pumping Lemma to prove that Lpalpal isis

not regularnot regular

Page 5: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 5/42

SolutionSolution

Let LLet Lpalpal be regular (assume)be regular (assume)

 Then Pumping Lemma states that there Then Pumping Lemma states that there

exists strings x,y, z such that w=xyzexists strings x,y, z such that w=xyz Since w= 0Since w= 0nn1010nn is a palindrome and isis a palindrome and is

assumed to be regular, so w=xyzassumed to be regular, so w=xyz

LetLet x=0000««..k times k<nx=0000««..k times k<ny= 0000««(ny= 0000««(n--k) timesk) times

z= 1000000«. ¶n· zeroesz= 1000000«. ¶n· zeroes

Page 6: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 6/42

SolutionSolution

Here y can be pumped which means y canHere y can be pumped which means y can

repeat 0 or more number of times. Let usrepeat 0 or more number of times. Let us

say 0 timessay 0 times Then xz should also belong to L Then xz should also belong to Lpalpal that isthat is

xz is also a palindrome which is axz is also a palindrome which is a

contradictioncontradiction Hence LHence Lpalpal is not regular.is not regular.

Page 7: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 7/42

R ules to define Palindrome stringsR ules to define Palindrome strings

PP

PP 00

PP 11

PP 0P00P0

PP

1P11P1

B A SIS RULE

INDUCTION  RULE

Page 8: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 8/42

Context Free GrammarContext Free Grammar

 A context free grammar is a formal A context free grammar is a formal

notation for expressing recursivenotation for expressing recursive

definitions of languagesdefinitions of languages A formal language is context free if there is A formal language is context free if there is

a context free grammar that generates ita context free grammar that generates it

CFGs are powerful enough to describe theCFGs are powerful enough to describe thesyntax of most programming languagessyntax of most programming languages

Page 9: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 9/42

Context Free GrammarContext Free Grammar

Not all formal languages are context freeNot all formal languages are context free

Informally, a Context Free Grammar is simply aInformally, a Context Free Grammar is simply a

set of rewriting rules or productions (referset of rewriting rules or productions (referprevious example)previous example)

Page 10: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 10/42

Context Free GrammarContext Free Grammar

 A CFG G is defined by its four components A CFG G is defined by its four components

G= (V,T,P,S)G= (V,T,P,S)

 Where  Where V: set of non V: set of non--terminal variablesterminal variables T: set of terminals T: set of terminals

P: set of Production rulesP: set of Production rules

S: start variableS: start variable

Page 11: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 11/42

 Terminals and nonterminals Terminals and nonterminals

Symbols that are to be replaced are calledSymbols that are to be replaced are called

nonterminals nonterminals ,,

Symbols that cannot be replaced are calledSymbols that cannot be replaced are calledterminals terminals 

Example:Example:

1.1. 01A1001A102.2. if ( if ( expressionexpression ) ) statementstatement elseelse statementstatement

Page 12: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 12/42

Defining CFG: an exampleDefining CFG: an example

Example of a palindromeExample of a palindrome

G= ({P},{0,1},A, P)G= ({P},{0,1},A, P)

 Where A is defined by the set of rules Where A is defined by the set of rulesPP

PP 00

PP 11

PP 0P00P0

PP 1P11P1

Page 13: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 13/42

Example from simple EnglishExample from simple English

1.1. <sentence><sentence> <noun phrase> <predicate><noun phrase> <predicate>

2.2. <noun phrase><noun phrase> <article> <noun><article> <noun>

3.3. <predicate><predicate> <verb><verb>

4.4. <article><article> aa

5.5. <article><article> thethe

6.6. <noun><noun> catcat

7.7. <noun><noun> dog dog 8.8. <verb><verb> runsruns

9.9. <verb><verb>  walks walks

Page 14: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 14/42

Derivation of ´the dog walksµDerivation of ´the dog walksµ

<sentence><sentence><noun phrase> <predicate> (P1)<noun phrase> <predicate> (P1)

<noun phrase><verb><noun phrase><verb> (P3)(P3)

<article> <noun> <verb> (P2)<article> <noun> <verb> (P2)

the <noun> <verb> (P5)the <noun> <verb> (P5)

the dog <verb> (P7)the dog <verb> (P7)

the dog walks (P9)the dog walks (P9)

Page 15: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 15/42

Language of the grammarLanguage of the grammar

 All strings that can be derived by applying the All strings that can be derived by applying the

production rules of a grammar belong to theproduction rules of a grammar belong to the

language described by that grammarlanguage described by that grammar

In this exampleIn this example

L={a dog runs, a cat runs, a dog walks, a cat walks,L={a dog runs, a cat runs, a dog walks, a cat walks,

the dog runs, the cat runs, the dog walks, the catthe dog runs, the cat runs, the dog walks, the cat

 walks} walks}

Page 16: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 16/42

CFG for regular expressionsCFG for regular expressions

Let us consider a regular expressionLet us consider a regular expression

(a+b)(a+b+0+1)(a+b)(a+b+0+1)

 Then we can have rules that use two non Then we can have rules that use two non--terminal variables say E and Iterminal variables say E and I

 The CFG for the above language can be The CFG for the above language can be

defined as a fourdefined as a four--tupletuple( ( {I,E},{I,E}, {0,1,a,b,+,*,(,)},{0,1,a,b,+,*,(,)},PP, E), E)

NonNon--terminalterminal

 Variables (V) Variables (V)

 Terminal Terminal

 Variables (T) Variables (T)Set of Set of 

production rulesproduction rules

Start symbolStart symbol

Page 17: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 17/42

Production rules for given GProduction rules for given G

1.1. EE II

2.2. EE E+EE+E

3.3. EE E*EE*E

4.4. EE (E)(E)

5.5. II aa

6.6. II bb

7.7. II IaIa8.8. II IbIb

9.9. II I0I0

10.10. II I1I1

Page 18: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 18/42

Compact notation for productionsCompact notation for productions

 The production rules described above can The production rules described above can

be written as followsbe written as follows

EE I|E+E|E*E|(E)I|E+E|E*E|(E)II a|b|Ia|Ib|I0|I1a|b|Ia|Ib|I0|I1

Page 19: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 19/42

Context Free GrammarContext Free Grammar

 The term "context The term "context--free" expresses the fact thatfree" expresses the fact that

nonnon--terminals can be rewritten without regard toterminals can be rewritten without regard to

the context in which they occur.the context in which they occur.

 A formal language is context A formal language is context--free if somefree if some

contextcontext--free grammar generates it.free grammar generates it.

Page 20: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 20/42

Derivations using a grammarDerivations using a grammar

Productions of a CFG are applied to inferProductions of a CFG are applied to infer

that certain strings are in the language of that certain strings are in the language of 

a certain variablea certain variable  There are two approaches There are two approaches

1.1. Recursive inferenceRecursive inference

2.2. DerivationDerivation

Page 21: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 21/42

R ecursive InferenceR ecursive Inference

Strings already known to be in the languageStrings already known to be in the language

are taken and then use the rules from inputare taken and then use the rules from input

to start symbolto start symbol

Page 22: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 22/42

DerivationsDerivations

Derivations use the production rules fromDerivations use the production rules from

head to body, the language of the grammarhead to body, the language of the grammar

is all strings of terminals obtained this way is all strings of terminals obtained this way 

Page 23: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 23/42

InferencesInferences

Consider the grammarConsider the grammar

G= ( G= ( {I,E},{I,E}, {0,1,a,b,+,*,(,)},{0,1,a,b,+,*,(,)},PP, E), E)

 To find out the strings in the language defined To find out the strings in the language definedby G. We infer the following by G. We infer the following 

i.i. Production rule 5 implies that string a is in LProduction rule 5 implies that string a is in L

ii.ii. Production rule 6 implies that string b is in LProduction rule 6 implies that string b is in Liii.iii. Production rule 9 implies that string a0 is in LProduction rule 9 implies that string a0 is in L

(Also uses (i))(Also uses (i))

Page 24: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 24/42

Inferring stringsInferring strings

String String inferredinferred

For languageFor language

of of ProductionProductionusedused

StringsStringsusedused

ii aa II 55

iiii bb II 66iiiiii b0b0 II 99 iiiiiv iv b00b00 II 99 iiiiii v  v aa EE 11 ii

 vi vi b00b00 EE 11 iv iv  vii vii a+b00a+b00 EE 22 v,vi v,vi viii viii (a+b00)(a+b00) EE 44 vii viiixix a*(a+b00)a*(a+b00) EE 33 v,viii v,viii

Page 25: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 25/42

Leftmost DerivationsLeftmost Derivations

 The derivation obtained by replacing the The derivation obtained by replacing the

leftmost variable by one of its production bodiesleftmost variable by one of its production bodies

at each step, is called the leftmost derivationat each step, is called the leftmost derivation

 A leftmost derivation is indicated by a A leftmost derivation is indicated by almlm

Page 26: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 26/42

Derivation of string a*(a+b00)Derivation of string a*(a+b00)

usingL

eftmost derivationusingL

eftmost derivationEE E*EE*E I*EI*E a*Ea*E a*(E)a*(E) a*(E+E)a*(E+E)

a*(I+E)a*(I+E)

a*(a+E)a*(a+E)

a*(a+I)a*(a+I)

a*(a+I0)a*(a+I0)

a*(a+I00)a*(a+I00)

a*(a+b00)a*(a+b00)

lmlm lmlm lmlm lmlm

lmlm lmlm

lmlm

lmlm

lmlm lmlm lmlm

Page 27: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 27/42

R ightmost DerivationsR ightmost Derivations

 The derivation obtained by replacing the The derivation obtained by replacing the

rightmost variable by one of its productionrightmost variable by one of its production

bodies at each step, is called the rightmostbodies at each step, is called the rightmost

derivationderivation

 A rightmost derivation is indicated by a A rightmost derivation is indicated by a rmrm

Page 28: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 28/42

R ightmost DerivationsR ightmost Derivations

EE E*EE*E E*(E)E*(E) E*(E+E)E*(E+E)

E*(E+I)E*(E+I) E*(E+I0)E*(E+I0) E*(E+I00)E*(E+I00)E*(E+b00)E*(E+b00) E*(I+b00)E*(I+b00) E*(a+b00)E*(a+b00)

I*(a+b00)I*(a+b00) a*(a+b00)a*(a+b00)

rmrm

rmrm rmrm rmrm rmrm

rmrm rmrm

rmrm rmrm rmrm

rmrm

Page 29: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 29/42

Language of a GrammarLanguage of a Grammar

If G= (V,T,P,S) is a context free grammar, theIf G= (V,T,P,S) is a context free grammar, the

language (CFL) of G is the set of terminal stringslanguage (CFL) of G is the set of terminal strings

that have derivations from the start symbolthat have derivations from the start symbol

L(G)= {string w in T|SL(G)= {string w in T|S w} w}

GG

**

Page 30: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 30/42

PalindromesPalindromes

If LIf Lpalpal is the language and Gis the language and Gpalpal= ({P},{0,1},A, P)= ({P},{0,1},A, P)

 Where A is defined by the set of rules Where A is defined by the set of rules

PP

PP 00

PP 11

PP 0P00P0

PP 1P11P1 The strings derived from these rules form a The strings derived from these rules form a

context free languagecontext free language

Page 31: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 31/42

Sentential Form of a GrammarSentential Form of a Grammar

If G= (V,T,P,S) is a context free grammar,If G= (V,T,P,S) is a context free grammar,

then any string then any string EEin (V U T)* such that Sin (V U T)* such that S

derivesderives EEis a sentential formis a sentential form

Page 32: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 32/42

Class AssignmentClass Assignment

Given production rules of a grammar areGiven production rules of a grammar are

1.1. SS  A1B A1B

2.2.  A A 0A|0A|

3.3. BB 0B|1B|0B|1B|

Give leftmost derivation of stringsGive leftmost derivation of strings

0010100101

10011001

Page 33: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 33/42

Parse treesParse trees

 The derivations obtained by a CFG can be The derivations obtained by a CFG can be

represented using tree structurerepresented using tree structure

 The tree shows how the symbols of a The tree shows how the symbols of aterminal string are grouped into sub stringsterminal string are grouped into sub strings

 These trees are known as ´parse treesµ These trees are known as ´parse treesµ

Page 34: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 34/42

Parse treesParse trees

 The parse trees for G are trees with following  The parse trees for G are trees with following conditionsconditions

1.1. Each interior node is labeled by a variable in V Each interior node is labeled by a variable in V 

2.2. Each leaf is labeled by a variable, or a terminal,Each leaf is labeled by a variable, or a terminal,or an epsilonor an epsilon

3.3. If an interior node is labeled A, and itsIf an interior node is labeled A, and its

children are labeled xchildren are labeled x11, x, x22, x, x33, «.x, «.xk k 

respectively, from the left, thenrespectively, from the left, then

 A A xx11xx22xx33«.x«.xk k  is a production in Pis a production in P

Page 35: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 35/42

ExampleExample

EE

EE EE++

II  A parse tree showing  A parse tree showing 

the derivation of the derivation of 

I+EI+E

Page 36: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 36/42

ExampleExample

Consider the grammar of language of Consider the grammar of language of 

palindrome stringspalindrome strings

Learning how toLearning how toDerive the palindrome 110010011Derive the palindrome 110010011

Draw the parse tree for the aboveDraw the parse tree for the above

derivationderivation

Page 37: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 37/42

SolutionSolution

The grammar has production rulesThe grammar has production rules

1.1. PP

2.2. PP 00

3.3. PP 11

4.4. PP 0P00P0

5.5. PP 1P11P1

Page 38: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 38/42

DerivationDerivation

 Then Then

PP1P11P1 (Rule 5)(Rule 5)

11P1111P11 (Rule 5)(Rule 5) 110P011110P011 (Rule 4)(Rule 4)

1100P00111100P0011 (Rule 4)(Rule 4)

110010011110010011 (Rule 3)(Rule 3)

Page 39: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 39/42

Parse treeParse tree

PP

11 PP 11

PP11 11

PP00 00

11

PP 0000

Page 40: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 40/42

 Y ield of a parse tree Y ield of a parse tree

 The leaves of the parse tree, on The leaves of the parse tree, on

concatenation from left, give a string,concatenation from left, give a string,

called the yield of the treecalled the yield of the tree This is a string derived from the root This is a string derived from the root

 variable variable

 The yield is a terminal string I.e. all leaves The yield is a terminal string I.e. all leavesare labeled either with a terminal or withare labeled either with a terminal or with

Page 41: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 41/42

 Y ield of the parse tree Y ield of the parse tree

Example :PalindromesExample :Palindromes

 Yield is 110010011 Yield is 110010011

Page 42: context free grammar181007800

8/8/2019 context free grammar181007800

http://slidepdf.com/reader/full/context-free-grammar181007800 42/42

Class AssignmentClass Assignment

Draw a parse tree for a*(a+b00)Draw a parse tree for a*(a+b00)