97
Context-Free Grammars and Languages Context-Free Grammars and Languages – p.1/40

Context Free Grammar

Embed Size (px)

DESCRIPTION

CFG slides

Citation preview

Page 1: Context Free Grammar

Context-Free Grammars andLanguages

Context-Free Grammars and Languages – p.1/40

Page 2: Context Free Grammar

Limitations of finite automata

� There are languages, such as

� �� � � � � � � �that cannot

be described (specified) by NFAs or REs

Context-free grammars provide a more powerfulmechanism for language specification

Context-free grammars can describe features thathave a recursive structure making them useful beyondfinite automata

Context-Free Grammars and Languages – p.2/40

Page 3: Context Free Grammar

Limitations of finite automata

� There are languages, such as

� �� � � � � � � �that cannot

be described (specified) by NFAs or REs

� Context-free grammars provide a more powerfulmechanism for language specification

Context-free grammars can describe features thathave a recursive structure making them useful beyondfinite automata

Context-Free Grammars and Languages – p.2/40

Page 4: Context Free Grammar

Limitations of finite automata

� There are languages, such as

� �� � � � � � � �that cannot

be described (specified) by NFAs or REs

� Context-free grammars provide a more powerfulmechanism for language specification

� Context-free grammars can describe features thathave a recursive structure making them useful beyondfinite automata

Context-Free Grammars and Languages – p.2/40

Page 5: Context Free Grammar

Historical notes

� Context-free grammars were first used to study humanlanguages

One way of understanding the relationship betweensyntactic categories (such as noun, verb, preposition,etc) and their respective phrases leads to naturalrecursion

This is because noun phrases may occur inside theverb phrases and vice versa.

Context-Free Grammars and Languages – p.3/40

Page 6: Context Free Grammar

Historical notes

� Context-free grammars were first used to study humanlanguages

� One way of understanding the relationship betweensyntactic categories (such as noun, verb, preposition,etc) and their respective phrases leads to naturalrecursion

This is because noun phrases may occur inside theverb phrases and vice versa.

Context-Free Grammars and Languages – p.3/40

Page 7: Context Free Grammar

Historical notes

� Context-free grammars were first used to study humanlanguages

� One way of understanding the relationship betweensyntactic categories (such as noun, verb, preposition,etc) and their respective phrases leads to naturalrecursion

� This is because noun phrases may occur inside theverb phrases and vice versa.

Context-Free Grammars and Languages – p.3/40

Page 8: Context Free Grammar

Note

Context-free grammars can capture important aspects of

these relationships

Context-Free Grammars and Languages – p.4/40

Page 9: Context Free Grammar

Important application

� Context-free grammars are used as basis for compilerdesign and implementation

Context-free grammars are used as specificationmechanisms for programming languages

Designers of compilers use such grammars toimplement compiler’s components, such a scanners,parsers, and code generators

The implementation of any programming language ispreceded by a context-free grammar that specifies it

Context-Free Grammars and Languages – p.5/40

Page 10: Context Free Grammar

Important application

� Context-free grammars are used as basis for compilerdesign and implementation

� Context-free grammars are used as specificationmechanisms for programming languages

Designers of compilers use such grammars toimplement compiler’s components, such a scanners,parsers, and code generators

The implementation of any programming language ispreceded by a context-free grammar that specifies it

Context-Free Grammars and Languages – p.5/40

Page 11: Context Free Grammar

Important application

� Context-free grammars are used as basis for compilerdesign and implementation

� Context-free grammars are used as specificationmechanisms for programming languages

� Designers of compilers use such grammars toimplement compiler’s components, such a scanners,parsers, and code generators

The implementation of any programming language ispreceded by a context-free grammar that specifies it

Context-Free Grammars and Languages – p.5/40

Page 12: Context Free Grammar

Important application

� Context-free grammars are used as basis for compilerdesign and implementation

� Context-free grammars are used as specificationmechanisms for programming languages

� Designers of compilers use such grammars toimplement compiler’s components, such a scanners,parsers, and code generators

� The implementation of any programming language ispreceded by a context-free grammar that specifies it

Context-Free Grammars and Languages – p.5/40

Page 13: Context Free Grammar

Context-free languages

� The collection of languages specified by context-freegrammars are called context-free languages

Context-free languages include regular languages andmany others

Here we will study the formal concepts of context-freegrammars and context-free languages

Context-Free Grammars and Languages – p.6/40

Page 14: Context Free Grammar

Context-free languages

� The collection of languages specified by context-freegrammars are called context-free languages

� Context-free languages include regular languages andmany others

Here we will study the formal concepts of context-freegrammars and context-free languages

Context-Free Grammars and Languages – p.6/40

Page 15: Context Free Grammar

Context-free languages

� The collection of languages specified by context-freegrammars are called context-free languages

� Context-free languages include regular languages andmany others

� Here we will study the formal concepts of context-freegrammars and context-free languages

Context-Free Grammars and Languages – p.6/40

Page 16: Context Free Grammar

Notations

� Abbreviate the phrase context-free grammar to CFG.

Abbreviate the phrase context-free language to CFL.

Abbreviate the concept of a CFG specification rule tothe tuple where stands for left handside and stands for right hand side.

Context-Free Grammars and Languages – p.7/40

Page 17: Context Free Grammar

Notations

� Abbreviate the phrase context-free grammar to CFG.

� Abbreviate the phrase context-free language to CFL.

Abbreviate the concept of a CFG specification rule tothe tuple where stands for left handside and stands for right hand side.

Context-Free Grammars and Languages – p.7/40

Page 18: Context Free Grammar

Notations

� Abbreviate the phrase context-free grammar to CFG.

� Abbreviate the phrase context-free language to CFL.

� Abbreviate the concept of a CFG specification rule tothe tuple

� ��� � �� ��� where� � � stands for left hand

side and� ��� stands for right hand side.

Context-Free Grammars and Languages – p.7/40

Page 19: Context Free Grammar

More on specification rules

� The

� � � of a specification rule is also called variableand is denoted by capital letters

The of a specification rule is also called aspecification pattern and consists of a string ofvariables and constants

The variables that occur in a specification pattern arealso called nonterminal symbols; the constants thatoccur in a specification pattern are also called terminalsymbols

Context-Free Grammars and Languages – p.8/40

Page 20: Context Free Grammar

More on specification rules

� The

� � � of a specification rule is also called variableand is denoted by capital letters

� The� ��� of a specification rule is also called aspecification pattern and consists of a string ofvariables and constants

The variables that occur in a specification pattern arealso called nonterminal symbols; the constants thatoccur in a specification pattern are also called terminalsymbols

Context-Free Grammars and Languages – p.8/40

Page 21: Context Free Grammar

More on specification rules

� The

� � � of a specification rule is also called variableand is denoted by capital letters

� The� ��� of a specification rule is also called aspecification pattern and consists of a string ofvariables and constants

� The variables that occur in a specification pattern arealso called nonterminal symbols; the constants thatoccur in a specification pattern are also called terminalsymbols

Context-Free Grammars and Languages – p.8/40

Page 22: Context Free Grammar

CFG: Informal

� A CFG grammar consists of a collection ofspecification rules where one variable is designated asstart symbol or axiom

Example: the CFG has the following specificationrules:

Context-Free Grammars and Languages – p.9/40

Page 23: Context Free Grammar

CFG: Informal

� A CFG grammar consists of a collection ofspecification rules where one variable is designated asstart symbol or axiom

� Example: the CFG

��� has the following specification

rules:

Context-Free Grammars and Languages – p.9/40

Page 24: Context Free Grammar

CFG: Informal

� A CFG grammar consists of a collection ofspecification rules where one variable is designated asstart symbol or axiom

� Example: the CFG

��� has the following specification

rules:

� � � � � �

� � � �

� � � �

Context-Free Grammars and Languages – p.9/40

Page 25: Context Free Grammar

Note

� Nonterminals of CFG

�� are

� ��

� �

and�

is the axiom

Terminals of CFG are

Context-Free Grammars and Languages – p.10/40

Page 26: Context Free Grammar

Note

� Nonterminals of CFG

�� are

� ��

� �

and�

is the axiom

� Terminals of CFG

�� are

� ��

��

� �

Context-Free Grammars and Languages – p.10/40

Page 27: Context Free Grammar

More terminology

� The specification rules of a CFG are also calledproductions or substitution rules

Nonterminals used in the specification rules defining aCFG may be strings

Terminals in the specification rules defining a CFG areconstant strings

Context-Free Grammars and Languages – p.11/40

Page 28: Context Free Grammar

More terminology

� The specification rules of a CFG are also calledproductions or substitution rules

� Nonterminals used in the specification rules defining aCFG may be strings

Terminals in the specification rules defining a CFG areconstant strings

Context-Free Grammars and Languages – p.11/40

Page 29: Context Free Grammar

More terminology

� The specification rules of a CFG are also calledproductions or substitution rules

� Nonterminals used in the specification rules defining aCFG may be strings

� Terminals in the specification rules defining a CFG areconstant strings

Context-Free Grammars and Languages – p.11/40

Page 30: Context Free Grammar

Terminals

� Terminals used in CFG specification rules areanalogous to the input alphabet of an automaton

Example terminals used in CFG-s are letters of analphabet, numbers, special symbols, and strings ofsuch elements.

Strings used to denote terminals in CFG specificationrules are quoted

Context-Free Grammars and Languages – p.12/40

Page 31: Context Free Grammar

Terminals

� Terminals used in CFG specification rules areanalogous to the input alphabet of an automaton

� Example terminals used in CFG-s are letters of analphabet, numbers, special symbols, and strings ofsuch elements.

Strings used to denote terminals in CFG specificationrules are quoted

Context-Free Grammars and Languages – p.12/40

Page 32: Context Free Grammar

Terminals

� Terminals used in CFG specification rules areanalogous to the input alphabet of an automaton

� Example terminals used in CFG-s are letters of analphabet, numbers, special symbols, and strings ofsuch elements.

� Strings used to denote terminals in CFG specificationrules are quoted

Context-Free Grammars and Languages – p.12/40

Page 33: Context Free Grammar

Language specification

A CFG is used as a language specification mechanism bygenerating each string of the language in following manner:

1. Write down the start variable; it is the of one of thespecification rules,the top rule, unless specified otherwise

2. Find a variable that is written down and a rule whose is thatvariable. Replace the written down variable with the of thatrule

3. Repeat step 2 until no variables remain in the string thusgenerated

Context-Free Grammars and Languages – p.13/40

Page 34: Context Free Grammar

Language specification

A CFG is used as a language specification mechanism bygenerating each string of the language in following manner:

1. Write down the start variable; it is the

� ��� of one of thespecification rules,the top rule, unless specified otherwise

2. Find a variable that is written down and a rule whose is thatvariable. Replace the written down variable with the of thatrule

3. Repeat step 2 until no variables remain in the string thusgenerated

Context-Free Grammars and Languages – p.13/40

Page 35: Context Free Grammar

Language specification

A CFG is used as a language specification mechanism bygenerating each string of the language in following manner:

1. Write down the start variable; it is the

� ��� of one of thespecification rules,the top rule, unless specified otherwise

2. Find a variable that is written down and a rule whose

� �� is thatvariable. Replace the written down variable with the �

��� of thatrule

3. Repeat step 2 until no variables remain in the string thusgenerated

Context-Free Grammars and Languages – p.13/40

Page 36: Context Free Grammar

Language specification

A CFG is used as a language specification mechanism bygenerating each string of the language in following manner:

1. Write down the start variable; it is the

� ��� of one of thespecification rules,the top rule, unless specified otherwise

2. Find a variable that is written down and a rule whose

� �� is thatvariable. Replace the written down variable with the �

��� of thatrule

3. Repeat step 2 until no variables remain in the string thusgenerated

Context-Free Grammars and Languages – p.13/40

Page 37: Context Free Grammar

Example string generation

Using CFG

��� we can generate the string 000#111 as

follows:

A

0A1

00A11

000A111

000B111

000#111

Note: The sequence of substitutions used to obtain a string

using a CFG is called a derivation and may be represented

by a tree called a derivation tree or a parse tree

Context-Free Grammars and Languages – p.14/40

Page 38: Context Free Grammar

Example string generation

Using CFG

��� we can generate the string 000#111 as

follows:

A

0A1

00A11

000A111

000B111

000#111

Note: The sequence of substitutions used to obtain a string

using a CFG is called a derivation and may be represented

by a tree called a derivation tree or a parse tree

Context-Free Grammars and Languages – p.14/40

Page 39: Context Free Grammar

Example derivation tree

The derivation tree of the string 000#111 using CFG

��� is

in Figure 1

#

B

A

A

0 1

A

0 1

A

0 1

Figure 1: Derivation tree for 000#111

Context-Free Grammars and Languages – p.15/40

Page 40: Context Free Grammar

Note

� All strings of terminals generated in this way constitutethe language specified by the grammar

We write for the language generated by thegrammar . Thus, .

The language generated by a context-free grammar iscalled a Context-Free Language, CFL.

Context-Free Grammars and Languages – p.16/40

Page 41: Context Free Grammar

Note

� All strings of terminals generated in this way constitutethe language specified by the grammar

� We write

� � � �

for the language generated by thegrammar

. Thus,

� � ��

�� �� � � � � � � � �

.

The language generated by a context-free grammar iscalled a Context-Free Language, CFL.

Context-Free Grammars and Languages – p.16/40

Page 42: Context Free Grammar

Note

� All strings of terminals generated in this way constitutethe language specified by the grammar

� We write

� � � �

for the language generated by thegrammar

. Thus,

� � ��

�� �� � � � � � � � �

.

� The language generated by a context-free grammar iscalled a Context-Free Language, CFL.

Context-Free Grammars and Languages – p.16/40

Page 43: Context Free Grammar

More notations

� To distinguish nonterminal from terminal strings weoften enclose nonterminals in angular parentheses,

��

, and terminals in quotes “,".

If two or more rules have the same , as in theexample and , we may compact themusing the form where is usedwith the meaning of an “or".

Context-Free Grammars and Languages – p.17/40

Page 44: Context Free Grammar

More notations

� To distinguish nonterminal from terminal strings weoften enclose nonterminals in angular parentheses,

��

, and terminals in quotes “,".

� If two or more rules have the same

� ��� , as in theexample

� � � � �

and

� � �, we may compact them

using the form

� � � �� � � � �� ����

�� � �

� � � � where

is usedwith the meaning of an “or".

Context-Free Grammars and Languages – p.17/40

Page 45: Context Free Grammar

Example compaction

The rules

� �

and may be written as

� � �

.

Context-Free Grammars and Languages – p.18/40

Page 46: Context Free Grammar

CFG �

The CFG � specifies a fragment of English

�� � �� � � � � � � ��� � � ���� �� � �� � � � � ��� � � �

� ��� � � ���� �� � � ��� ��� � �� � � � ��� � � � �� � � � ���� � � �

�� � � � � ���� �� � � ��� � � � � �� � � � � � � � � � �� � � � ���� � � �

� �� � � � ���� �� � � �� � � � � � � ��� � �

� � � ��� � � �� � � ��� � � � � ��� � �

� ��� � � � � � � � � � � �� �� � � � � � � � � � ���� �� �

�� � � ��� � � � � � � ��

� ��� � � �� �"! � � ��# � � $ � �

�� � � � � � � � �� �� � � % � �� � � � �

� �� � � � $ � � �

Context-Free Grammars and Languages – p.19/40

Page 47: Context Free Grammar

Note

� The CFG

�� has ten variables (capitalized and in

angular brackets) and 9 terminals (written in thestandard English alphabet) plus a space character

Also, the CFG has 18 rules

Examples strings that belongs to are:

a boy sees

the boy sees a flower

a girl with a flower likes the boy

Context-Free Grammars and Languages – p.20/40

Page 48: Context Free Grammar

Note

� The CFG

�� has ten variables (capitalized and in

angular brackets) and 9 terminals (written in thestandard English alphabet) plus a space character

� Also, the CFG

�� has 18 rules

Examples strings that belongs to are:

a boy sees

the boy sees a flower

a girl with a flower likes the boy

Context-Free Grammars and Languages – p.20/40

Page 49: Context Free Grammar

Note

� The CFG

�� has ten variables (capitalized and in

angular brackets) and 9 terminals (written in thestandard English alphabet) plus a space character

� Also, the CFG

�� has 18 rules

� Examples strings that belongs to

� � ��

are:

a boy sees

the boy sees a flower

a girl with a flower likes the boy

Context-Free Grammars and Languages – p.20/40

Page 50: Context Free Grammar

Example derivation with �

� � � �� � � � � � � � �� � � �� �� � �� � �

� � �� �� �

� � ��� �� � � �� � �� � �

� �� �

� ��� � ��� �� � � �� � � �� � �

� � �� �� �

� � � � � � �� � �� � �

� �� �

� ��� � � � �� � �

� � � �

� ��� � ��� � � �� �

� ��� � � � �� �

� ��� �� � �

Context-Free Grammars and Languages – p.21/40

Page 51: Context Free Grammar

Formal definition of a CFG

A context-free grammar is a 4-tuple

���

��

��

� �where:

1. is a finite set of strings called the variables ornonterminals

2. is a finite set of strings, disjoint from , calledterminals

3. is a finite set of rules (or specification rules) of theform , where ,

4. is the start variable (or grammar axiom)

Context-Free Grammars and Languages – p.22/40

Page 52: Context Free Grammar

Formal definition of a CFG

A context-free grammar is a 4-tuple

���

��

��

� �where:

1.

is a finite set of strings called the variables ornonterminals

2. is a finite set of strings, disjoint from , calledterminals

3. is a finite set of rules (or specification rules) of theform , where ,

4. is the start variable (or grammar axiom)

Context-Free Grammars and Languages – p.22/40

Page 53: Context Free Grammar

Formal definition of a CFG

A context-free grammar is a 4-tuple

���

��

��

� �where:

1.

is a finite set of strings called the variables ornonterminals

2.

is a finite set of strings, disjoint from

, calledterminals

3. is a finite set of rules (or specification rules) of theform , where ,

4. is the start variable (or grammar axiom)

Context-Free Grammars and Languages – p.22/40

Page 54: Context Free Grammar

Formal definition of a CFG

A context-free grammar is a 4-tuple

���

��

��

� �where:

1.

is a finite set of strings called the variables ornonterminals

2.

is a finite set of strings, disjoint from

, calledterminals

3.

is a finite set of rules (or specification rules) of theform

� � � �� � � , where

� ��� � �

,���� � �� � � ��

4. is the start variable (or grammar axiom)

Context-Free Grammars and Languages – p.22/40

Page 55: Context Free Grammar

Formal definition of a CFG

A context-free grammar is a 4-tuple

���

��

��

� �where:

1.

is a finite set of strings called the variables ornonterminals

2.

is a finite set of strings, disjoint from

, calledterminals

3.

is a finite set of rules (or specification rules) of theform

� � � �� � � , where

� ��� � �

,���� � �� � � ��

4.

� � �

is the start variable (or grammar axiom)

Context-Free Grammars and Languages – p.22/40

Page 56: Context Free Grammar

Example CFG grammar

� � ��

��

� ��

��

�� �

where is:

� � � � �

� � �

� � �

Context-Free Grammars and Languages – p.23/40

Page 57: Context Free Grammar

Direct derivation

� If ��

��

� � �� � � ��

(i.e., are strings of variables andterminals) and

� � � � � �

(i.e., is a rule of thegrammar) then we say that � � � yields � � �, written

� � � � � � �

We may also say that is directly derived fromusing the rule

Context-Free Grammars and Languages – p.24/40

Page 58: Context Free Grammar

Direct derivation

� If ��

��

� � �� � � ��

(i.e., are strings of variables andterminals) and

� � � � � �

(i.e., is a rule of thegrammar) then we say that � � � yields � � �, written

� � � � � � �

� We may also say that � � � is directly derived from � � �

using the rule

� � � �

Context-Free Grammars and Languages – p.24/40

Page 59: Context Free Grammar

Derivation

� We write �� � � if � � � or if a sequence

� ��

��

� � � � �

� % � �� � � ��

exists, for� � �

, and

� � � ��

�� � �

� � % � �

We may also say that is a derivation offrom

Context-Free Grammars and Languages – p.25/40

Page 60: Context Free Grammar

Derivation

� We write �� � � if � � � or if a sequence

� ��

��

� � � � �

� % � �� � � ��

exists, for� � �

, and

� � � ��

�� � �

� � % � �

� We may also say that � ��

��

� � � � �

� %�

� is a derivation of

� from � �

Context-Free Grammars and Languages – p.25/40

Page 61: Context Free Grammar

Language specified by

If

���

��

��

� �

is a CFG then the language specified by

(or the language of

) is

� � � �

�� � � �� � � � � � �

Context-Free Grammars and Languages – p.26/40

Page 62: Context Free Grammar

Note

� Often we specify a grammar by writing down only itsrules

We can identify the variables as the symbols thatappear only as the of the rules

Terminals are the remaining strings used in the rules

Context-Free Grammars and Languages – p.27/40

Page 63: Context Free Grammar

Note

� Often we specify a grammar by writing down only itsrules

� We can identify the variables as the symbols thatappear only as the

� ��� of the rules

Terminals are the remaining strings used in the rules

Context-Free Grammars and Languages – p.27/40

Page 64: Context Free Grammar

Note

� Often we specify a grammar by writing down only itsrules

� We can identify the variables as the symbols thatappear only as the

� ��� of the rules

� Terminals are the remaining strings used in the rules

Context-Free Grammars and Languages – p.27/40

Page 65: Context Free Grammar

More examples of CFGs

� Consider the grammar:

contains strings such as:

abab, aaabbb, aababb;

Note: if one think at as then we can see thatis the language of all strings of properly nested

parentheses

Context-Free Grammars and Languages – p.28/40

Page 66: Context Free Grammar

More examples of CFGs

� Consider the grammar:

��� �

� � � ��

� ��

� ��

� � � � � � � � � � ��� ��

� �

contains strings such as:

abab, aaabbb, aababb;

Note: if one think at as then we can see thatis the language of all strings of properly nested

parentheses

Context-Free Grammars and Languages – p.28/40

Page 67: Context Free Grammar

More examples of CFGs

� Consider the grammar:

��� �

� � � ��

� ��

� ��

� � � � � � � � � � ��� ��

� �

� � � ��

contains strings such as:

abab, aaabbb, aababb;

Note: if one think at as then we can see thatis the language of all strings of properly nested

parentheses

Context-Free Grammars and Languages – p.28/40

Page 68: Context Free Grammar

More examples of CFGs

� Consider the grammar:

��� �

� � � ��

� ��

� ��

� � � � � � � � � � ��� ��

� �

� � � ��

contains strings such as:

abab, aaabbb, aababb;

Note: if one think at as then we can see thatis the language of all strings of properly nested

parentheses

Context-Free Grammars and Languages – p.28/40

Page 69: Context Free Grammar

More examples of CFGs

� Consider the grammar:

��� �

� � � ��

� ��

� ��

� � � � � � � � � � ��� ��

� �

� � � ��

contains strings such as:

abab, aaabbb, aababb;

� Note: if one think at ��

�as

��

then we can see that

� � ��

is the language of all strings of properly nestedparentheses

Context-Free Grammars and Languages – p.28/40

Page 70: Context Free Grammar

Arithmetic expressions

� Consider the grammar:

�� �

� � ��

��

� ��

� ��

��

��

��

� ��

��

� �

where

is:

is the language of arithmetic expressions

Context-Free Grammars and Languages – p.29/40

Page 71: Context Free Grammar

Arithmetic expressions

� Consider the grammar:

�� �

� � ��

��

� ��

� ��

��

��

��

� ��

��

� �

where

is:

� � � �� � � �

� � � � � � � �

� � � � � � �

is the language of arithmetic expressions

Context-Free Grammars and Languages – p.29/40

Page 72: Context Free Grammar

Arithmetic expressions

� Consider the grammar:

�� �

� � ��

��

� ��

� ��

��

��

��

� ��

��

� �

where

is:

� � � �� � � �

� � � � � � � �

� � � � � � �

� � � ��

is the language of arithmetic expressions

Context-Free Grammars and Languages – p.29/40

Page 73: Context Free Grammar

Note

� The variables and constants in

� � ��

are representedby the terminal �

Arithmetic operations in are addition,represented by +, and multiplication, represented by *

An examples of a derivation using is in Figure 2

Context-Free Grammars and Languages – p.30/40

Page 74: Context Free Grammar

Note

� The variables and constants in

� � ��

are representedby the terminal �

� Arithmetic operations in

� � ��

are addition,represented by +, and multiplication, represented by *

An examples of a derivation using is in Figure 2

Context-Free Grammars and Languages – p.30/40

Page 75: Context Free Grammar

Note

� The variables and constants in

� � ��

are representedby the terminal �

� Arithmetic operations in

� � ��

are addition,represented by +, and multiplication, represented by *

� An examples of a derivation using

�� is in Figure 2

Context-Free Grammars and Languages – p.30/40

Page 76: Context Free Grammar

Example derivation with �E��� � � �

E + T

T T * F

F F a

a a

Figure 2: Derivation tree for a+a*a

Context-Free Grammars and Languages – p.31/40

Page 77: Context Free Grammar

Designing CFGs

� As with the design of automata, the design of CFGsrequires creativity

CFGs are even trickier to construct than finiteautomata because “we are more accustomed toprogramming a machine than we are to specifyprogramming languages"

Context-Free Grammars and Languages – p.32/40

Page 78: Context Free Grammar

Designing CFGs

� As with the design of automata, the design of CFGsrequires creativity

� CFGs are even trickier to construct than finiteautomata because “we are more accustomed toprogramming a machine than we are to specifyprogramming languages"

Context-Free Grammars and Languages – p.32/40

Page 79: Context Free Grammar

Design techniques

� Many CFG are unions of simpler CFGs. Hence thesuggestion is to construct smaller, simpler grammarsfirst and then to join them into a larger grammar

The mechanism of grammar combination consists ofputting all their rules together and adding the new rules

where the variables , ,are the start variables of the individual grammars and

is a new variable

Context-Free Grammars and Languages – p.33/40

Page 80: Context Free Grammar

Design techniques

� Many CFG are unions of simpler CFGs. Hence thesuggestion is to construct smaller, simpler grammarsfirst and then to join them into a larger grammar

� The mechanism of grammar combination consists ofputting all their rules together and adding the new rules� � � �

�� �

��

� � �

� � % where the variables

� �, �� �� �

,are the start variables of the individual grammars and�

is a new variable

Context-Free Grammars and Languages – p.33/40

Page 81: Context Free Grammar

Example grammar design

Design a grammar for the language

� �� � � � � � � � � � � � �� � � � � �

1. Construct the grammar that generates

2. Construct the grammar that generates

3. Put them together adding the rule thus getting

Context-Free Grammars and Languages – p.34/40

Page 82: Context Free Grammar

Example grammar design

Design a grammar for the language

� �� � � � � � � � � � � � �� � � � � �

1. Construct the grammar

��� � � ��

� �� that generates

� � � � � � � �

2. Construct the grammar that generates

3. Put them together adding the rule thus getting

Context-Free Grammars and Languages – p.34/40

Page 83: Context Free Grammar

Example grammar design

Design a grammar for the language

� �� � � � � � � � � � � � �� � � � � �

1. Construct the grammar

��� � � ��

� �� that generates

� � � � � � � �

2. Construct the grammar

��� � � � ��

�� that generates

� � � � � � � �

3. Put them together adding the rule thus getting

Context-Free Grammars and Languages – p.34/40

Page 84: Context Free Grammar

Example grammar design

Design a grammar for the language

� �� � � � � � � � � � � � �� � � � � �

1. Construct the grammar

��� � � ��

� �� that generates

� � � � � � � �

2. Construct the grammar

��� � � � ��

�� that generates

� � � � � � � �

3. Put them together adding the rule� � � ��

� �� thus getting

� � � ��

� ��

�� � � ��

� ��

�� � � � ��

��

Context-Free Grammars and Languages – p.34/40

Page 85: Context Free Grammar

Second design technique

� Constructing a CFG for a regular language is easy ifone can first construct a DFA for that language

Conversion procedure:1. Make a variable for each state of DFA

2. Add the rule to the CFG if is atransition in the DFA

3. Add the rule if is an accept state of the DFA

4. If is the start state of the DFA make the start variable ofthe CFG.

Context-Free Grammars and Languages – p.35/40

Page 86: Context Free Grammar

Second design technique

� Constructing a CFG for a regular language is easy ifone can first construct a DFA for that language

� Conversion procedure:

1. Make a variable for each state of DFA

2. Add the rule to the CFG if is atransition in the DFA

3. Add the rule if is an accept state of the DFA

4. If is the start state of the DFA make the start variable ofthe CFG.

Context-Free Grammars and Languages – p.35/40

Page 87: Context Free Grammar

Second design technique

� Constructing a CFG for a regular language is easy ifone can first construct a DFA for that language

� Conversion procedure:1. Make a variable

��� for each state �� of DFA

2. Add the rule to the CFG if is atransition in the DFA

3. Add the rule if is an accept state of the DFA

4. If is the start state of the DFA make the start variable ofthe CFG.

Context-Free Grammars and Languages – p.35/40

Page 88: Context Free Grammar

Second design technique

� Constructing a CFG for a regular language is easy ifone can first construct a DFA for that language

� Conversion procedure:1. Make a variable

��� for each state �� of DFA

2. Add the rule

�� � � ��� to the CFG if

� � �� � �� �� is a

transition in the DFA

3. Add the rule if is an accept state of the DFA

4. If is the start state of the DFA make the start variable ofthe CFG.

Context-Free Grammars and Languages – p.35/40

Page 89: Context Free Grammar

Second design technique

� Constructing a CFG for a regular language is easy ifone can first construct a DFA for that language

� Conversion procedure:1. Make a variable

��� for each state �� of DFA

2. Add the rule

�� � � ��� to the CFG if

� � �� � �� �� is a

transition in the DFA

3. Add the rule

�� � � � if �� is an accept state of the DFA

4. If is the start state of the DFA make the start variable ofthe CFG.

Context-Free Grammars and Languages – p.35/40

Page 90: Context Free Grammar

Second design technique

� Constructing a CFG for a regular language is easy ifone can first construct a DFA for that language

� Conversion procedure:1. Make a variable

��� for each state �� of DFA

2. Add the rule

�� � � ��� to the CFG if

� � �� � �� �� is a

transition in the DFA

3. Add the rule

�� � � � if �� is an accept state of the DFA

4. If ��� is the start state of the DFA make

�� the start variable of

the CFG.

Context-Free Grammars and Languages – p.35/40

Page 91: Context Free Grammar

Note

Verify that CFG constructed by the conversion of a DFA into

a CFG generates the language that the DFA recognizes

Context-Free Grammars and Languages – p.36/40

Page 92: Context Free Grammar

Third design technique

� Certain CFLs contain strings with two relatedsubstrings as are

��

and

� �

in

� �� � � � � � � �

Example of relationship: to recognize such a languagea machine would need to remember an unboundedamount of info about one of the substrings

Context-Free Grammars and Languages – p.37/40

Page 93: Context Free Grammar

Third design technique

� Certain CFLs contain strings with two relatedsubstrings as are

��

and

� �

in

� �� � � � � � � �

� Example of relationship: to recognize such a languagea machine would need to remember an unboundedamount of info about one of the substrings

Context-Free Grammars and Languages – p.37/40

Page 94: Context Free Grammar

Note

A CFG that handles this situation uses a rule of the form

� � � � � � which generates strings wherein the portion

containing �’s corresponds to the portion containing �’s

Context-Free Grammars and Languages – p.38/40

Page 95: Context Free Grammar

Fourth design technique

� In a complex language, strings may contain certainstructures that appear recursively

Example: in arithmetic expressions any time thesymbol a appear, the entire parenthesized expressionmay appear.

Context-Free Grammars and Languages – p.39/40

Page 96: Context Free Grammar

Fourth design technique

� In a complex language, strings may contain certainstructures that appear recursively

� Example: in arithmetic expressions any time thesymbol a appear, the entire parenthesized expressionmay appear.

Context-Free Grammars and Languages – p.39/40

Page 97: Context Free Grammar

Note

To achieve this effect one needs to place the variable gen-

erating the structure (

in case of

��) in the location of the

rule corresponding to where the structure may recursively

appear as in

� � � � � �

in case of

��

Context-Free Grammars and Languages – p.40/40