Context-Free Grammars and Languages Context-Free Grammars and - - PowerPoint PPT Presentation

context free grammars and languages
SMART_READER_LITE
LIVE PREVIEW

Context-Free Grammars and Languages Context-Free Grammars and - - PowerPoint PPT Presentation

Context-Free Grammars and Languages Context-Free Grammars and Languages p.1/40 Limitations of finite automata There are languages, such as that cannot be described (specified) by NFAs


slide-1
SLIDE 1

Context-Free Grammars and Languages

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

slide-2
SLIDE 2

Limitations of finite automata

  • There are languages, such as
✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟

that cannot be described (specified) by NFAs or REs

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

slide-3
SLIDE 3

Limitations of finite automata

  • There are languages, such as
✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟

that cannot be described (specified) by NFAs or REs

  • Context-free grammars provide a more powerful

mechanism for language specification

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

slide-4
SLIDE 4

Limitations of finite automata

  • There are languages, such as
✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟

that cannot be described (specified) by NFAs or REs

  • Context-free grammars provide a more powerful

mechanism for language specification

  • Context-free grammars can describe features that

have a recursive structure making them useful beyond finite automata

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

slide-5
SLIDE 5

Historical notes

  • Context-free grammars were first used to study human

languages

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

slide-6
SLIDE 6

Historical notes

  • Context-free grammars were first used to study human

languages

  • One way of understanding the relationship between

syntactic categories (such as noun, verb, preposition, etc) and their respective phrases leads to natural recursion

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

slide-7
SLIDE 7

Historical notes

  • Context-free grammars were first used to study human

languages

  • One way of understanding the relationship between

syntactic categories (such as noun, verb, preposition, etc) and their respective phrases leads to natural recursion

  • This is because noun phrases may occur inside the

verb phrases and vice versa.

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

slide-8
SLIDE 8

Note

Context-free grammars can capture important aspects of these relationships

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

slide-9
SLIDE 9

Important application

  • Context-free grammars are used as basis for compiler

design and implementation

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

slide-10
SLIDE 10

Important application

  • Context-free grammars are used as basis for compiler

design and implementation

  • Context-free grammars are used as specification

mechanisms for programming languages

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

slide-11
SLIDE 11

Important application

  • Context-free grammars are used as basis for compiler

design and implementation

  • Context-free grammars are used as specification

mechanisms for programming languages

  • Designers of compilers use such grammars to

implement compiler’s components, such a scanners, parsers, and code generators

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

slide-12
SLIDE 12

Important application

  • Context-free grammars are used as basis for compiler

design and implementation

  • Context-free grammars are used as specification

mechanisms for programming languages

  • Designers of compilers use such grammars to

implement compiler’s components, such a scanners, parsers, and code generators

  • The implementation of any programming language is

preceded by a context-free grammar that specifies it

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

slide-13
SLIDE 13

Context-free languages

  • The collection of languages specified by context-free

grammars are called context-free languages

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

slide-14
SLIDE 14

Context-free languages

  • The collection of languages specified by context-free

grammars are called context-free languages

  • Context-free languages include regular languages and

many others

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

slide-15
SLIDE 15

Context-free languages

  • The collection of languages specified by context-free

grammars are called context-free languages

  • Context-free languages include regular languages and

many others

  • Here we will study the formal concepts of context-free

grammars and context-free languages

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

slide-16
SLIDE 16

Notations

  • Abbreviate the phrase context-free grammar to CFG.

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

slide-17
SLIDE 17

Notations

  • Abbreviate the phrase context-free grammar to CFG.
  • Abbreviate the phrase context-free language to CFL.

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

slide-18
SLIDE 18

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 to

the tuple

  • ✁✄✂
☎ ✆ ✝ ✁✄✂

where

stands for left hand side and

✝ ✁✄✂

stands for right hand side.

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

slide-19
SLIDE 19

More on specification rules

  • The
  • f a specification rule is also called variable

and is denoted by capital letters

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

slide-20
SLIDE 20

More on specification rules

  • The
  • f a specification rule is also called variable

and is denoted by capital letters

  • The
✝ ✁✄✂
  • f a specification rule is also called a

specification pattern and consists of a string of variables and constants

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

slide-21
SLIDE 21

More on specification rules

  • The
  • f a specification rule is also called variable

and is denoted by capital letters

  • The
✝ ✁✄✂
  • f a specification rule is also called a

specification pattern and consists of a string of variables and constants

  • The variables that occur in a specification pattern are

also called nonterminal symbols; the constants that

  • ccur in a specification pattern are also called terminal

symbols

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

slide-22
SLIDE 22

CFG: Informal

  • A CFG grammar consists of a collection of

specification rules where one variable is designated as start symbol or axiom

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

slide-23
SLIDE 23

CFG: Informal

  • A CFG grammar consists of a collection of

specification rules where one variable is designated as start symbol or axiom

  • Example: the CFG
✂✁

has the following specification rules:

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

slide-24
SLIDE 24

CFG: Informal

  • A CFG grammar consists of a collection of

specification rules where one variable is designated as start symbol or axiom

  • Example: the CFG
✂✁

has the following specification rules:

✆ ✂
✆ ✁ ✁ ☎ ✆ ✂

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

slide-25
SLIDE 25

Note

  • Nonterminals of CFG

are

and

  • is the axiom

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

slide-26
SLIDE 26

Note

  • Nonterminals of CFG

are

and

  • is the axiom
  • Terminals of CFG

are

✁ ✂

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

slide-27
SLIDE 27

More terminology

  • The specification rules of a CFG are also called

productions or substitution rules

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

slide-28
SLIDE 28

More terminology

  • The specification rules of a CFG are also called

productions or substitution rules

  • Nonterminals used in the specification rules defining a

CFG may be strings

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

slide-29
SLIDE 29

More terminology

  • The specification rules of a CFG are also called

productions or substitution rules

  • Nonterminals used in the specification rules defining a

CFG may be strings

  • Terminals in the specification rules defining a CFG are

constant strings

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

slide-30
SLIDE 30

Terminals

  • Terminals used in CFG specification rules are

analogous to the input alphabet of an automaton

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

slide-31
SLIDE 31

Terminals

  • Terminals used in CFG specification rules are

analogous to the input alphabet of an automaton

  • Example terminals used in CFG-s are letters of an

alphabet, numbers, special symbols, and strings of such elements.

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

slide-32
SLIDE 32

Terminals

  • Terminals used in CFG specification rules are

analogous to the input alphabet of an automaton

  • Example terminals used in CFG-s are letters of an

alphabet, numbers, special symbols, and strings of such elements.

  • Strings used to denote terminals in CFG specification

rules are quoted

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

slide-33
SLIDE 33

Language specification

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

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

slide-34
SLIDE 34

Language specification

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

  • 1. Write down the start variable; it is the
  • ✁✄✂
  • f one of the

specifi cation rules,the top rule, unless specifi ed otherwise

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

slide-35
SLIDE 35

Language specification

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

  • 1. Write down the start variable; it is the
  • ✁✄✂
  • f one of the

specifi cation rules,the top rule, unless specifi ed otherwise

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

is that

  • variable. Replace the written down variable with the
  • ✁✄✂
  • f that

rule

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

slide-36
SLIDE 36

Language specification

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

  • 1. Write down the start variable; it is the
  • ✁✄✂
  • f one of the

specifi cation rules,the top rule, unless specifi ed otherwise

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

is that

  • variable. Replace the written down variable with the
  • ✁✄✂
  • f that

rule

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

generated

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

slide-37
SLIDE 37

Example string generation

Using CFG

✂✁

we can generate the string 000#111 as follows: A

  • 0A1
  • 00A11
  • 000A111
  • 000B111
  • 000#111

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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

Example derivation tree

The derivation tree of the string 000#111 using CFG

✂✁

is in Figure 1

# B A A 1 A 1 A 1

Figure 1: Derivation tree for 000#111

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

slide-40
SLIDE 40

Note

  • All strings of terminals generated in this way constitute

the language specified by the grammar

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

slide-41
SLIDE 41

Note

  • All strings of terminals generated in this way constitute

the language specified by the grammar

  • We write

for the language generated by the grammar

  • . Thus,
✂ ✄ ✁ ✂ ✄ ✂ ☎ ✄ ✆ ✝ ✞ ✂ ✟

.

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

slide-42
SLIDE 42

Note

  • All strings of terminals generated in this way constitute

the language specified by the grammar

  • We write

for the language generated by the grammar

  • . Thus,
✂ ✄ ✁ ✂ ✄ ✂ ☎ ✄ ✆ ✝ ✞ ✂ ✟

.

  • The language generated by a context-free grammar is

called a Context-Free Language, CFL.

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

slide-43
SLIDE 43

More notations

  • To distinguish nonterminal from terminal strings we
  • ften enclose nonterminals in angular parentheses,

, and terminals in quotes “,".

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

slide-44
SLIDE 44

More notations

  • To distinguish nonterminal from terminal strings we
  • ften enclose nonterminals in angular parentheses,

, and terminals in quotes “,".

  • If two or more rules have the same
  • ✁✄✂

, as in the example

and

, we may compact them using the form

✂ ✆ ✝ ✁ ✂ ✁ ✆ ✝ ✁✄✂
✁ ✁ ✁ ✝ ✁ ✂ ✄

where

is used with the meaning of an “or".

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

slide-45
SLIDE 45

Example compaction

The rules

and may be written as

.

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

slide-46
SLIDE 46

CFG

  • The CFG

specifi es a fragment of English

✂ ✄ ☎ ✆ ✝ ☎ ✆ ✞ ☎ ✟ ✠ ✡ ✂ ✆☞☛✌ ✄ ✍ ✎✑✏✒ ✓✔ ✟ ✂ ✕ ✔ ✏ ✖ ✍ ✎ ✏✒ ✓ ✔ ✟ ✂ ✆☞☛✌ ✄ ✍ ✎✑✏✒ ✓✔ ✟ ✠ ✡ ✂ ✞✘✗ ✆☞☛ ✌ ✄ ✟✙ ✂ ✞ ✗ ✆☞☛✌ ✄ ✟ ✂ ✍ ✏ ✔ ✗ ✍ ✎✑✏✒ ✓ ✔ ✟ ✂ ✕ ✔ ✏ ✖ ✍ ✎✑✏✒ ✓✔ ✟ ✠ ✡ ✂ ✞✘✗ ✕ ✔ ✏ ✖ ✟ ✙ ✂ ✞ ✗ ✕ ✔ ✏ ✖ ✟ ✂ ✍ ✏ ✔ ✗ ✍ ✎✑✏✒ ✓ ✔ ✟ ✂ ✍ ✏ ✔ ✗ ✍ ✎✑✏✒ ✓✔ ✟ ✠ ✡ ✂ ✍ ✏ ✔ ✗ ✟ ✂ ✞ ✗ ✆☞☛✌ ✄ ✟ ✂ ✞ ✗ ✆☞☛✌ ✄ ✟ ✠ ✡ ✂ ✚ ✏ ✛ ✜✣✢ ✤ ✔ ✟ ✂ ✆☞☛✌ ✄ ✟ ✂ ✞✘✗ ✕ ✔ ✏ ✖ ✟ ✠ ✡ ✂ ✕ ✔ ✏ ✖ ✟✙ ✂ ✕ ✔ ✏ ✖ ✟ ✂ ✆ ☛✌ ✄ ✍ ✎✑✏✒ ✓✔ ✟ ✂ ✚ ✏ ✛ ✜✣✢ ✤ ✔ ✟ ✠ ✡ ✒ ✙ ✛ ✎ ✔ ✂ ✆☞☛✌ ✄ ✟ ✠ ✡ ✖ ☛✥ ✙✧✦ ✜ ✏ ✤ ✙ ★ ✤ ☛ ✩ ✔ ✏ ✂ ✕ ✔ ✏ ✖ ✟ ✠ ✡ ✛ ☛✌ ✢ ✎ ✔ ✓ ✙ ✤ ✜ ✪ ✔ ✓ ✙ ✓ ✔ ✔ ✓ ✂ ✍ ✏ ✔ ✗ ✟ ✠ ✡ ✩ ✜ ✛ ✎

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

slide-47
SLIDE 47

Note

  • The CFG
  • has ten variables (capitalized and in

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

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

slide-48
SLIDE 48

Note

  • The CFG
  • has ten variables (capitalized and in

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

  • Also, the CFG
  • has 18 rules

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

slide-49
SLIDE 49

Note

  • The CFG
  • has ten variables (capitalized and in

angular brackets) and 9 terminals (written in the standard 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

slide-50
SLIDE 50

Example derivation with

✂ ✄ ☎ ✂ ✄ ✆ ✂ ✝ ✞
  • ✄✠✟✡
☛ ☞ ✁ ✌ ✂✍ ✝
☞ ✁
✂✍ ✝ ✞
  • ✆✒✑
✄✠✟✡ ☛ ✝
☞ ✁ ✌ ✂✍ ✝ ✞
✕✗✖
  • ✄✠✟✡
☛ ✝
☞ ✁
✂✍ ✝ ✞ ✌
✟ ✡ ☛ ✝
☞ ✁
✂✍ ✝ ✞ ✌ ✏ ✟✘
☞ ✁ ✌ ✂ ✍ ✝ ✞ ✌ ✏ ✟✘
  • ✆✒✑
✎ ✍
✝ ✞ ✌ ✏ ✟✘
✝ ✞ ✌ ✏ ✟✘ ✂✍ ✍ ✂

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

slide-51
SLIDE 51

Formal definition of a CFG

A context-free grammar is a 4-tuple

where:

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

slide-52
SLIDE 52

Formal definition of a CFG

A context-free grammar is a 4-tuple

where: 1.

  • is a finite set of strings called the variables or

nonterminals

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

slide-53
SLIDE 53

Formal definition of a CFG

A context-free grammar is a 4-tuple

where: 1.

  • is a finite set of strings called the variables or

nonterminals 2.

is a finite set of strings, disjoint from

  • , called

terminals

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

slide-54
SLIDE 54

Formal definition of a CFG

A context-free grammar is a 4-tuple

where: 1.

  • is a finite set of strings called the variables or

nonterminals 2.

is a finite set of strings, disjoint from

  • , called

terminals 3.

is a finite set of rules (or specification rules) of the form

✂ ✆ ✝ ✁ ✂

, where

  • ✁✄✂
  • ,
✝ ✁✄✂
✁ ✂ ✂

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

slide-55
SLIDE 55

Formal definition of a CFG

A context-free grammar is a 4-tuple

where: 1.

  • is a finite set of strings called the variables or

nonterminals 2.

is a finite set of strings, disjoint from

  • , called

terminals 3.

is a finite set of rules (or specification rules) of the form

✂ ✆ ✝ ✁ ✂

, where

  • ✁✄✂
  • ,
✝ ✁✄✂
✁ ✂ ✂

4.

  • is the start variable (or grammar axiom)

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

slide-56
SLIDE 56

Example CFG grammar

✂✄ ☎ ✆ ☎ ✄
✁ ☎ ✆ ☎ ☎ ✝

where is:

✓ ✞ ✟ ✠ ✓
✞ ✟ ✡ ✡ ✞ ✟ ☛

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

slide-57
SLIDE 57

Direct derivation

  • If
✁ ✂ ✂

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

✆ ✂

(i.e., is a rule of the grammar) then we say that

yields

, written

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

slide-58
SLIDE 58

Direct derivation

  • If
✁ ✂ ✂

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

✆ ✂

(i.e., is a rule of the grammar) 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

slide-59
SLIDE 59

Derivation

  • We write

if

  • r if a sequence
✁ ✁
✁ ✂ ✂

exists, for

, and

✁ ✁

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

slide-60
SLIDE 60

Derivation

  • We write

if

  • r if a sequence
✁ ✁
✁ ✂ ✂

exists, for

, and

✁ ✁
  • We may also say that
✁ ✁

is a derivation of

from

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

slide-61
SLIDE 61

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

slide-62
SLIDE 62

Note

  • Often we specify a grammar by writing down only its

rules

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

slide-63
SLIDE 63

Note

  • Often we specify a grammar by writing down only its

rules

  • We can identify the variables as the symbols that

appear only as the

  • ✁✄✂
  • f the rules

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

slide-64
SLIDE 64

Note

  • Often we specify a grammar by writing down only its

rules

  • We can identify the variables as the symbols that

appear only as the

  • ✁✄✂
  • f the rules
  • Terminals are the remaining strings used in the rules

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

slide-65
SLIDE 65

More examples of CFGs

  • Consider the grammar:

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

slide-66
SLIDE 66

More examples of CFGs

  • Consider the grammar:
✁ ✄ ✁ ✁ ✄ ✟
✄ ☎ ✆ ✂ ✄ ✄ ✆ ✄ ✄ ✆✆☎ ✟

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

slide-67
SLIDE 67

More examples of CFGs

  • Consider the grammar:
✁ ✄ ✁ ✁ ✄ ✟
✄ ☎ ✆ ✂ ✄ ✄ ✆ ✄ ✄ ✆✆☎ ✟

contains strings such as:

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

slide-68
SLIDE 68

More examples of CFGs

  • Consider the grammar:
✁ ✄ ✁ ✁ ✄ ✟
✄ ☎ ✆ ✂ ✄ ✄ ✆ ✄ ✄ ✆✆☎ ✟

contains strings such as: abab, aaabbb, aababb;

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

slide-69
SLIDE 69

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 nested parentheses

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

slide-70
SLIDE 70

Arithmetic expressions

  • Consider the grammar:
✁ ✁ ✁

where

is:

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

slide-71
SLIDE 71

Arithmetic expressions

  • Consider the grammar:
✁ ✁ ✁

where

is:

✂ ✞ ✟ ✂
✁ ☎ ☎ ✞ ✟ ☎ ✂ ✄ ✁ ✄ ✄ ✞ ✟ ☎ ✂ ✆ ✁ ✌

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

slide-72
SLIDE 72

Arithmetic expressions

  • Consider the grammar:
✁ ✁ ✁

where

is:

✂ ✞ ✟ ✂
✁ ☎ ☎ ✞ ✟ ☎ ✂ ✄ ✁ ✄ ✄ ✞ ✟ ☎ ✂ ✆ ✁ ✌

is the language of arithmetic expressions

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

slide-73
SLIDE 73

Note

  • The variables and constants in

are represented by the terminal

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

slide-74
SLIDE 74

Note

  • The variables and constants in

are represented by the terminal

  • Arithmetic operations in

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

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

slide-75
SLIDE 75

Note

  • The variables and constants in

are represented by 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

slide-76
SLIDE 76

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

slide-77
SLIDE 77

Designing CFGs

  • As with the design of automata, the design of CFGs

requires creativity

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

slide-78
SLIDE 78

Designing CFGs

  • As with the design of automata, the design of CFGs

requires creativity

  • CFGs are even trickier to construct than finite

automata because “we are more accustomed to programming a machine than we are to specify programming languages"

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

slide-79
SLIDE 79

Design techniques

  • Many CFG are unions of simpler CFGs. Hence the

suggestion is to construct smaller, simpler grammars first and then to join them into a larger grammar

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

slide-80
SLIDE 80

Design techniques

  • Many CFG are unions of simpler CFGs. Hence the

suggestion is to construct smaller, simpler grammars first and then to join them into a larger grammar

  • The mechanism of grammar combination consists of

putting 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

slide-81
SLIDE 81

Example grammar design

Design a grammar for the language

✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟ ✁ ✁ ☎ ✄ ✂ ✄ ✆ ✝ ✞ ✂ ✟

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

slide-82
SLIDE 82

Example grammar design

Design a grammar for the language

✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟ ✁ ✁ ☎ ✄ ✂ ✄ ✆ ✝ ✞ ✂ ✟
  • 1. Construct the grammar
✁✁ ✞ ✟ ✠ ✁

that generates

✄ ✠ ☎
✁ ☛ ✆ ✠ ✝

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

slide-83
SLIDE 83

Example grammar design

Design a grammar for the language

✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟ ✁ ✁ ☎ ✄ ✂ ✄ ✆ ✝ ✞ ✂ ✟
  • 1. Construct the grammar
✁✁ ✞ ✟ ✠ ✁

that generates

✄ ✠ ☎
✁ ☛ ✆ ✠ ✝
  • 2. Construct the grammar
✁✁ ✞ ✟
✁ ✂

that generates

✠ ☎ ✁ ☛ ✆ ✠ ✝

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

slide-84
SLIDE 84

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

slide-85
SLIDE 85

Second design technique

  • Constructing a CFG for a regular language is easy if
  • ne can first construct a DFA for that language

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

slide-86
SLIDE 86

Second design technique

  • Constructing a CFG for a regular language is easy if
  • ne can first construct a DFA for that language
  • Conversion procedure:

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

slide-87
SLIDE 87

Second design technique

  • Constructing a CFG for a regular language is easy if
  • ne can first construct a DFA for that language
  • Conversion procedure:
  • 1. Make a variable
✂✁

for each state

✄ ✁
  • f DFA

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

slide-88
SLIDE 88

Second design technique

  • Constructing a CFG for a regular language is easy if
  • ne can first construct a DFA for that language
  • Conversion procedure:
  • 1. Make a variable
✂✁

for each state

✄ ✁
  • f DFA
  • 2. Add the rule
✞ ✟ ✌ ✁

to the CFG if

✂ ☎ ✄ ✁ ✄ ✌ ✆ ☎ ✄
  • is a

transition in the DFA

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

slide-89
SLIDE 89

Second design technique

  • Constructing a CFG for a regular language is easy if
  • ne can first construct a DFA for that language
  • Conversion procedure:
  • 1. Make a variable
✂✁

for each state

✄ ✁
  • f 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

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

slide-90
SLIDE 90

Second design technique

  • Constructing a CFG for a regular language is easy if
  • ne can first construct a DFA for that language
  • Conversion procedure:
  • 1. Make a variable
✂✁

for each state

✄ ✁
  • f 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

slide-91
SLIDE 91

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

slide-92
SLIDE 92

Third design technique

  • Certain CFLs contain strings with two related

substrings as are

✂ ✄

and

☎ ✄

in

✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟

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

slide-93
SLIDE 93

Third design technique

  • Certain CFLs contain strings with two related

substrings as are

✂ ✄

and

☎ ✄

in

✁ ✂ ✄ ☎ ✄ ✆ ✝ ✞ ✂ ✟
  • Example of relationship: to recognize such a language

a machine would need to remember an unbounded amount of info about one of the substrings

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

slide-94
SLIDE 94

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

slide-95
SLIDE 95

Fourth design technique

  • In a complex language, strings may contain certain

structures that appear recursively

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

slide-96
SLIDE 96

Fourth design technique

  • In a complex language, strings may contain certain

structures that appear recursively

  • Example: in arithmetic expressions any time the

symbol a appear, the entire parenthesized expression may appear.

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

slide-97
SLIDE 97

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