CS

© All Rights Reserved

2 vues

CS

© All Rights Reserved

- Building Cvpr10
- History 6
- r05310501 Formal Languages and Automata Theory
- 2marks AI
- QUESTION BANK FOR COMPILER DESIGN
- 113211687 Chapter 6 Ll k and Lr k Grammars Formal Language Automata Theory
- CD Course File
- PPL-Unit_2
- 9a.pdf
- mca2ndyear_180815
- Creating a DSL with Python
- Compiler Design
- المحاضرة 8 معالجة اللغات الطبيعية
- Syntactic annotation of spontaneous speech--application to call-center.pdf
- Compiler Construction Chapter 6
- StanfordParser
- Bottom Parse
- Syntax and Semantics
- grammar
- null

Vous êtes sur la page 1sur 96

So far ...

A language is a set of strings over an alphabet.

Languages serve two purposes in computing:

(a) communicating instructions or

information

(b) defining valid communications

We have defined languages by:

(i) regular expressions

(ii) finite state automata

Both (i) and (ii) give us exactly the same class

of languages.

What about languages without this

class?

7/21/2017 10:52 AM CS 120 SemesterII-2013 2

Specifying Non-Regular Languages

We have already seen a number of languages that are not regular. In

particular,

{anbn : n 0}, the language of matched round brackets

arithmetic expressions

standard programming languages

The languages above are not regular. However, these languages are all

systematic constructions, and can be clearly and explicitly defined.

Consider L = {anbn : n 0}:

(i) L

(ii) if x L, then axb L

(iii) nothing else is in L

This is a clear and concise specification of L. Can we use it to generate

members of L?

7/21/2017 10:52 AM CS 120 SemesterII-2013 3

Limitation of Finite Automata

There are languages, such as {0n1n|n0} that can not be

described (specified)by NFAs or REs.

mechanism for language specification.

making them useful beyond finite automata.

Historical Notes

CFGs were first used to study human languages.

syntactic categories (such as a noun, verb, preposition etc)

and their respective phrases leads to natural recursion.

phrases and vice-versa.

Applications

CFGs are used as basis for compiler design and

implementation

programming languages

compilers components, such a scanners, parsers, and code

generators.

preceded by a context-free grammar that specifies it

7/21/2017 10:52 AM CS 120 SemesterII-2013 6

Context-Free Languages

The collection of languages specified by CFGs are called

Context-Free Languages(CFLs).

CFLs include Regular Languages and many others.

Notation:

Abbreviate the phrase context-free grammar to CFG

lhs rhs where lhs stands for left hand side and rhs stands for right

hand side.

Specification Rules

The lhs of a specification rule is also called the Variable and

denoted by capital letters.

pattern and consists of variables and constants.

called non-terminal symbols; the constants that occur in a

specification pattern are also called terminal symbols.

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 G1 has the following specification

rules:

A 0A1

A B

B #

For G1, non-terminal symbols are {A,B} and A is the

Axiom. The terminals are {0, 1, #}

More Terminologies

The specification rules of a CFG are also called

productions or substitution rules

Non-terminals used in the specification rules defining a

CFG may be strings.

Terminals in the specification rules defining a CFG are

constant strings.

Terminals used in CFG specification rules are analogous to the

input alphabet of an automaton.

Example terminals used in CFGs are letters of an alphabet,

numbers, special symbols, and strings of such elements.

Strings used to denote terminals in CFG specification rules are

quoted.

7/21/2017 10:52 AM CS 120 SemesterII-2013 10

Language Specification

A CFG is used as a language specification mechanism

by generating each string of the language in the

following manner:

i. Write down the start variable; it is the lhs of one of the

specification rules, the top rule, unless specified otherwise.

ii. Find a variable that is written down and a rule whose lhs is

that variable. Replace the written down variable with the rhs

of that rule.

generated.

7/21/2017 10:52 AM CS 120 SemesterII-2013 11

Example 1: String Generation

Using CFG G1, we can generate the string 000#111 as

follows:

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

Note: The sequence of substations used to obtain a string

using CFG is called a derivation and may be represented

by a tree called a derivation tree or parse tree.

We often specify a grammar by writing down only its

rules. We can identify the variables as the symbols that

appear only as the lhs of he rules.

Terminals are the remaining strings used in the rules.

Derivation Tree

The derivation tree of the string 000#111 using CFG

G1 is as follows:

CFL & CFG

All strings of terminals generated in this way constitute a

language specified by a grammar.

We write L(G) for the language generated by the grammar

G. Thus, L(G1)={0n1n|n0}.

To distinguish non-terminal and terminal strings, we often

enclose non-terminals in angular parentheses, ,, and

terminals in quotes, ,.

If two or more rules have the same lhs, as in the example

A 0A1 and A B, we may compact them using the form

lhs rhs1|rhs2|.rhsn where | is used with the meaning of an

or.

Example: The rules A 0A1 and A B may be written as:

A 0A1|B

7/21/2017 10:52 AM CS 120 SemesterII-2013 14

Example 2:

The CFG G2 specifies a fragment of English

Example 2: GFG G2

The CFG G2 has 10 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 of strings that belongs to L(G2) are:

a boy sees

the boy sees a flower

a girl with a flower likes the boy

Derivation with GFG G2

Formal Definition of a GFG

A Context-Free Grammar is a 4-tuple (V, , R, S) where:

i. V is a set of strings called the variables or non-terminals

ii. is finite set of strings, disjoint from V, called terminals

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

lhs rhs where lhs belongs to V and rhs belongs to (VU )* .

iv. S V is the start variable (grammar axiom)

Example: G1= ({A,B},{0,1,#}, R,A) where:

A 0A1

A B

B #

Direct Derivation

If u,v,w (VU)* (i.e are stings of variables and terminals) and

A wR (i.e is a rule of the grammar) then we say uAv yields uwv ,

written uAv uwv

We may also say that uwv directly derived from uAv using the

rule A w

exists, for k0 and u1 u2 . uk v

Language specified by G:

If G=(V, , R, S) is a CFG then the language specified by G (or the

language of G) is L(G)={w *|S * w}

7/21/2017 10:52 AM CS 120 SemesterII-2013 19

Example 3: GFG G3

Consider the grammar:

G3=({S},{a,b},{S aSb|SS| },S)

L(G3) strings such as:

abab

aaabbb

aababb

Note: if one think of a, b as (,) then we can see that

L(G3) is the language of all strings of properly nested

parentheses.

Example 4: GFG G4

Consider the grammar:

G4 = ({E,T,F},{+,*,(,)}, R,E) where R is:

Example 4: GFG G4

The variables and constants in L(G4) are represented by terminal

the a

Arithmetic operations in L(G4) are addition represented by +,

multiplication represented by *

Examples of derivation (a+a*a) using G4 is as in the figure

below:

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.

Many CFGs 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 consist of putting

all their rules together and adding the new rules S S1|S2| ..

|Sk where the variables are the start variables Si ,1i k of

the individual grammars and S is a new variable.

First Grammar Design

Design a grammar for the language:

{0n1n|n0}U {1n0n|n0}

Construct the grammar S1 0S11| that generates

{0n1n|n0}

Construct the grammar S2 1S20| that generates

{1n0n|n0}

Put them together adding the value S S1|S2 thus getting:

Second Design Technique

Constructing a CFG for a regular language is easy if one can

first construct a DFA for that language.

Conversion procedure:

Make a variable Ri for each state qi of the DFA

Add rule Ri aRj to the CFG if (qi ,a)= qi is a transition in the

DFA

Add rule Ri if qi is an accept state of the DFA

If q0 is the start state of the DFA make R0 the start variable of the

CFG.

Verify that the CFG constructed by the conversion of a

DFA into a CFG generates the language that the DFA

recognizes.

Third Design Technique

Certain CFLs contain strings with two related substrings as

are 0n and 1n in {0n1n|n0}

machine would need to remember an unbounded amount

of info about one of the substrings.

R uRv which generates strings wherein the portion

containing us corresponds to the portion containing vs.

Fourth Design Technique

In a complex language, strings may contain certain

structures that appear recursively.

appear, the entire parenthesized expression may appear.

generating the structure (E in case of G4) in the location of

the rule corresponding to where the structure may

recursively appear as in E E+T in case of G4.

Designing CFG

Can we design CFG for

{0n1n | n 0} U {1n0n | n 0} ?

Designing CFG

S 0S1 |

CFG for the language L2 = {1n0n | n 0}

S 1S0 |

CFG for L1|L2

S S1 | S2

S1 0S11 |

S2 1S20 |

7/21/2017 10:52 AM CS 120 SemesterII-2013 29

Designing CFG

occurrence of 1s

S 00S111 |

Designing CFG

Can we construct the CFG for the language { w | w is a

palindrome } ?

Assume that the alphabet of w is {0,1}

1101011,

Regular Language & CFG

Theorem: Any regular language can be described by a

CFG.

CFL

Regular

Regular Language & CFG

Create a distinct variable Vi for each state qi in D.

Assume that q0 is the start state of D

Add a rule Vi aVj if (qi,a) = qj

Add a rule Vi if qi is an accept state

Then, we can show that the above CFG generates exactly the

same language as D (how to show?)

7/21/2017 10:52 AM CS 120 SemesterII-2013 33

Regular Language & CFG (Example)

DFA 0 1

1

q0 q1

start

0

CFG G = ( {V0, V1}, {0,1}, R, V0 ), where R is

V0 0V0 | 1V1 |

V1 1V1 | 0V0

7/21/2017 10:52 AM CS 120 SemesterII-2013 34

Leftmost Derivation

A derivation which always replace the leftmost

variable in each step is called a leftmost derivation

E.g., Consider the CFG for the properly nested

parentheses ( {S}, {(,)}, R, S ) with rule R: S (

S ) | SS |

Then, S SS (S)S ( )S ( ) ( S )

( ) ( ) is a leftmost derivation

But, S SS S(S) (S)(S) ( ) ( S )

( ) ( ) is not a leftmost derivation

However, we note that both derivations correspond to

the same parse tree

Leftmost & Rightmost Derivations

A derivation which always replace the rightmost variable

in each step is called a rightmost derivation.

S A|AB Sample derivations:

A | a | A b | AA S AB AAB aAB aaB aabB aabb

B b|bc|Bc|bB S AB AbB Abb AAbb Aabb aabb

Always picks leftmost variable.

A A b B

2nd derivation is rightmost.

a a b Always picks rightmost variable.

Ambiguity

Sometimes, a string can have two or more leftmost

derivations!!

E.g., Consider CFG ( {S}, {+,x,a}, R, S) with rules R:

SS+S|SxS|a

The string a + a x a has two leftmost derivations as

follows:

S S + S a + S a +S x S a + a x S

a+axa

S S x S S + S x S a +S x S a + a x S

a+axa

Two parse trees for a + a x a

S S

S + S S x S

a

S x S S + S a

a a a a

Parse Trees

A | a | A b | AA w = aabb this string?

B b|bc|Bc|bB

S S

S ? ?

A

A B A B

A A Infinitely

A A b B A A b many others

A A A b possible.

a a b a A b

a A b

a

a

7/21/2017 10:52 AM CS 120 SemesterII-2013 39

Grammar Ambiguity

If a string has two or more leftmost(or rightmost)

derivations in a CFG G, we say the string is derived

ambiguously in G

A grammar is ambiguous if some strings are derived

ambiguously.

Note that the two leftmost derivations in the previous

example correspond to different parse trees (see previous

slide)

In fact, each leftmost derivation corresponds to a

unique parse tree.

Ambiguity & Disambiguation

Ambiguity is not a good thing, if avoidable.

How to avoid?

Either say the same message in a slightly different language that is

not ambiguous,

Or change the grammar so it is not ambigous

Or cope.

Example: The grammar S SS|if test:S|if test:S else:S|write

is ambiguous. It makes sentences like: if test: if test:write

else:write

But we can write the same message differently:

S SS|if test:S end|if test S: else S end|write.

This makes sentences like :

if test: if test:write end else: write end

if test: if test: write else: write end end.

7/21/2017 10:52 AM CS 120 SemesterII-2013 41

Ambiguity & Disambiguation..

Exp n Exp Term

| Exp + Exp | Term + Exp

| Exp Exp Term n

| n Term

? ?

What is an equivalent

Uses

unambiguous

operator precedence

grammar? left-associativity

Ambiguity & Disambiguation..

What is a general algorithm?

?

None exists!

?

There are CFLs that are inherently ambiguous

Every CFG for this language is ambiguous.

Inherently Ambiguous

Sometimes when we have an ambiguous grammar, we can

find an unambiguous grammar that generates the same

language

However, some language can only be generated by

ambiguous grammar

E.g., { anbncm | n, m 0} [ {anbmcm | n, m 0}

CFL is inherently ambiguous if all grammars for it are

ambiguous.

Parsing

S

S S+M|M S + M

MM*T | T

T (S) | number

Derivation

M M * T

Parsing

Programming

languages 1

T T

are (should be)

designed to make

parsing easy, 2

3

efficient, and

unambiguous.

3 + 2 * 1

7/21/2017 10:52 AM CS 120 SemesterII-2013 45

Easy and Efficient Parsing

building a parser from a description of a

grammar.

parse tree quickly (linear time in the length of

the input).

CFG Simplification

But, CFG simplification & restriction still useful

theoretically & pragmatically.

Simpler grammars are easier to understand.

Simpler grammars can lead to faster parsing.

Restricted forms useful for some parsing algorithms.

Restricted forms can give you more knowledge about

derivations.

CFG Simplification

Cant always eliminate ambiguity.

But, CFG simplification & restriction still useful

theoretically & pragmatically.

Simpler grammars are easier to understand.

Simpler grammars can lead to faster parsing.

Restricted forms useful for some parsing algorithms.

Restricted forms can give you more knowledge about

derivations.

CFG simplification include:

Eliminate useless variables.

Eliminate -productions: A.

Eliminate unit productions: AB.

Eliminate redundant productions.

Trade left- & right-recursion.

7/21/2017 10:52 AM CS 120 SemesterII-2013 48

Killing -Productions

-Productions:

In a given CFG, we call a non-terminal N null able

if there is a production N , or there is a

derivation that starts at N and lead to a .

N

-Productions are undesirable.

We can replace -production with appropriate

non- productions.

49

7/21/2017 10:52 AM CS 120 SemesterII-2013

Killing -Productions

If L is CFL generated by a CFG having -productions, then

there is a different CFG that has no -production and still

generates either the whole language L (if L does not include )

or else generate the language of all the words in L other than .

Replacement Rule.

1. Delete all -Productions.

2. Add the following productions:

For every production of the X old string

Add new production of the form X .., where right side will

account for every modification of the old string that can be

formed by deleting all possible subsets of null-able Non-

Terminals, except that we do not allow X , to be formed if

all the character in old string are null-able

50

7/21/2017 10:52 AM CS 120 SemesterII-2013

Example Consider the CFG

S a | Xb | aYa

XY|

Yb|X

Production Production

XY nothing S a | Xb | aa | aYa |b

X nothing XY

YX nothing Yb|X

S Xb Sb

S aYa S aa

51

7/21/2017 10:52 AM CS 120 SemesterII-2013

Consider the CFG

Example S Xa

X aX | bX |

Production Production

S Xa Sa S a | Xa

X aX | bX | a | b

X aX Xa

X bX Xb

52

7/21/2017 10:52 AM CS 120 SemesterII-2013

Example

S XY

X Zb

Null-able Non-terminals are?

Y bW

Z AB A, B, Z and W

WZ

A aA | bA |

B Ba | Bb |

53

7/21/2017 10:52 AM CS 120 SemesterII-2013

S XY

X Zb

Example Contd. Y bW

Z AB

WZ

A aA | bA |

B Ba | Bb |

Old nullable New So the new CFG is

Production Production S XY

X Zb Xb

Y bW Yb X Zb | b

Z AB Z A and Z B Y bW | b

WZ Nothing new Z AB | A | B

A aA Aa

A bA Ab

WZ

B Ba B a A aA | bA | a | b

B Bb Bb B Ba | Ba | a | b

54

7/21/2017 10:52 AM CS 120 SemesterII-2013

Killing unit-productions

Nonterminal one Nonterminal

is called a unit production.

The following theorem allows us to get rid of unit

productions:

Theorem :

If there is a CFG for the language L that has no -

productions, then there is also a CFG for L with no -

productions and no unit productions.

55

7/21/2017 10:52 AM CS 120 SemesterII-2013

Killing unit-productions

Algorithm: For every pair of non-terminals A and B, if the

CFG has a unit production A B, or if there is a chain

A X1 X2 B where X1, X2, ... are non-

terminals, create new productions as follows:

If the non-unit productions from B are

B s1 | s2| where s1, s2, ... are strings, we create the

productions A s1| s2|

56

7/21/2017 10:52 AM CS 120 SemesterII-2013

Killing unit-productions

Consider the CFG

S A| bb

AB|b

BS|a

The non-unit productions are

S bb, A b ,B a

And unit productions are

SA

AB

BS

57

7/21/2017 10:52 AM CS 120 SemesterII-2013

Killing unit-productions: Example

contd.

Lets list all unit productions and their sequences and create new

productions:

SA gives Sb

SAB gives Sa

AB gives Aa

ABS gives A bb

BS gives B bb

BSA gives Bb

Eliminating all unit productions, the new CFG is

S bb | b | a

A b | a | bb

B a | bb | b

This CFG generates a finite language since there are no non-terminals

in any strings produced from S.

58

7/21/2017 10:52 AM CS 120 SemesterII-2013

Useless Symbols

Let a CFG G. A symbol X (V U ) is useful if there is a derivation

* *

S UxV w

G G

Where U and V (V U ) and w *. A symbol that is not useful is

useless

A terminal is useful if it occurs in a string of the language of G.

A variable is useful if it occurs in a derivation that begins from S and

generates a terminal string

2. There must be a derivation of a terminal string from the variable.

A variable that occurs in a sentential form is said to be reachable from

S.

A two part procedure is presented to eliminate useless symbols.

7/21/2017 10:52 AM CS 120 SemesterII-2013 59

Useless Productions

S aSb

S

SA

Useless Production

A aA

S A aA aaA aaaA

63

7/21/2017 10:52 AM CS 120 SemesterII-2013

Another grammar:

SA

A aA

A

B bA Useless Production

64

7/21/2017 10:52 AM CS 120 SemesterII-2013

contains only

In general:

terminals

if S xAy w

w L(G )

65

7/21/2017 10:52 AM CS 120 SemesterII-2013

A production A x is useless

if any of its variables is useless

S aSb

S Productions

Variables SA useless

useless A aA useless

useless BC useless

useless CD useless

66 7/21/2017 10:52 AM CS 120 SemesterII-2013

Removing Useless Productions

Example Grammar:

S aS | A | C

Aa

B aa

C aCb

67

7/21/2017 10:52 AM CS 120 SemesterII-2013

First: Find all variables that can produce

strings with only terminals

Round 1:

S aS | A | C { A, B}

Aa SA

B aa

C aCb Round 2: { A, B, S}

68

7/21/2017 10:52 AM CS 120 SemesterII-2013

Keep only the variables

that produce terminal symbols:{ A, B, S}

(the rest variables are useless)

S aS | A | C

Aa S aS | A

B aa Aa

C aCb B aa

Remove useless productions

69

7/21/2017 10:52 AM CS 120 SemesterII-2013

Find all variables

Second:

reachable from S

S aS | A

Aa S A B

B aa not

reachable

70

7/21/2017 10:52 AM CS 120 SemesterII-2013

Keep only the variables

reachable from S

(the rest variables are useless)

Final Grammar

S aS | A

S aS | A

Aa

Aa

B aa

71

7/21/2017 10:52 AM CS 120 SemesterII-2013

Set of variables that Derive terminal

symbols

Input = CFG (V, , P , S)

TERM = { A | there is a rule Aw P with

w *}

repeat

PREV = TERM

For each variable in A V do

o If there is a rule A w and w (PREV U )* then

TERM = TERM U {A}

Until PREV = TERM

72 CS 120 SemesterII-2013 7/21/2017 10:52 AM

Example

G: S AC | BS | B

A aA | aF

B CF | b

C cC | D

D aD | BD | C

E aA | BSA

73

F bB | b

7/21/2017 10:52 AM CS 120 SemesterII-2013

S AC | BS | B New Grammar from

A aA | aF TERM will be

B CF | b GT:

C cC | D S BS | B

D aD | BD | C A aA | aF

E aA | BSA Bb

F bB | b E aA | BSA

F bB | b

ITERATION TERM PREV

0 {B, F} {}

1 {B, F, A, S} {B, F}

2 {B, F, A, S, E} {B, F, A, S}

3 {B, F, A, S, E} {B, F, A, S, E}

Construction of set of reachable

Variables

Input = CFG (V, , P , S)

REACH = {S}

1. PREV = null

2. repeat

i. NEW = REACH PREV

ii. PREV = REACH

iii. For each variable A in NEW do

i. For each rule A w do add all variables in w to

REACH

3.

75

Until

7/21/2017 10:52 AM

REACH = PREV

CS 120 SemesterII-2013

GT:

S BS | B

A aA | aF

Bb

E aA | BSA

F bB | b

0 {S} {}

1 {S, B} {S} {S}

2 {S, B} {S, B} {B}

3 {S, B} {S, B}

76

7/21/2017 10:52 AM CS 120 SemesterII-2013

Removing All

77

7/21/2017 10:52 AM CS 120 SemesterII-2013

Chomsky Normal Form (CNF)

A CFG is in Chomsky Normal Form if each rule is of the

form

A BC

Aa

where

a is any terminal

A,B,C are variables

B, C cannot be start variable

However, S is allowed

Examples:

S AS S AS

S a S AAS

A SA A SA

Ab A aa

Chomsky Not Chomsky

Normal Form Normal Form

79

7/21/2017 10:52 AM CS 120 SemesterII-2013

Chomsky Normal Form (CNF)

Why should we care for CNF? Well, its an effective

grammar, in the sense that every variable that being

expanded (being a node in a parse tree), is guaranteed

to generate a letter in the final string.

parse tree that has O(n) nodes. This is of course not

necessarily true with general grammars that might have

huge trees, with little strings generated by them.

Converting a CFG to CNF

Theorem: Any context-free language can be

generated by a context-free grammar in

Chomsky Normal Form.

Form?

Proof Idea

The only reasons for a CFG not in CNF:

Start variable appears on right side

It has rules, such as A

It has unit rules, such as A A, or B C

Some rules does not have exactly two variables or

one terminal on right side

the above cases

Convertion to Chomsky Normal Form

S ABa

Example:

A aab

B Ac

Not Chomsky

Normal Form

83

7/21/2017 10:52 AM CS 120 SemesterII-2013

Introduce variables for Ta , Tb , Tc

terminals:

S ABTa

S ABa A TaTaTb

A aab B ATc

B Ac Ta a

Tb b

Tc c

84

7/21/2017 10:52 AM CS 120 SemesterII-2013

Introduce intermediate variable: V1

S AV1

S ABTa

V1 BTa

A TaTaTb

A TaTaTb

B ATc

B ATc

Ta a

Ta a

Tb b

Tb b

Tc c

Tc c

85

7/21/2017 10:52 AM CS 120 SemesterII-2013

Introduce intermediate variable: V2

S AV1

S AV1

V1 BTa

V1 BTa

A TaV2

A TaTaTb

V2 TaTb

B ATc

B ATc

Ta a

Ta a

Tb b

Tb b

Tc c

86

7/21/2017 10:52 AM CS 120 SemesterII-2013 Tc c

Final grammar in S AV1

Chomsky Normal Form:

V1 BTa

A TaV2

Initial grammar

V2 TaTb

S ABa B ATc

A aab Ta a

B Ac Tb b

87

7/21/2017 10:52 AM CS 120 SemesterII-2013

Tc c

General Conversion Steps [Step 1]

the context-free language. We want to convert G into

CNF.

Step 1: Add a new start variable S0 and the rule S0

S, where S is the start variable of G

does not appear on right side

General Conversion Steps [Step 2]

Step 2: We take care of all rules. To remove the

rule A , for each occurrence of A on the right side

of a rule, we add a new rule with that occurrence

deleted.

E.g., R uAvAw causes us to add the rules: R

uAvw, R uvAw, R uvw

If we have the rule R A, we add R unless we

had previously removed R

After removing A , the new grammar still generates

the same language as G.

General Conversion Steps [Step 3]

Step 3: We remove the unit rule A B. To do so,

for each rule B u (where u is a string of variables

and terminals), we add the rule A u.

E.g., if we have A B, B aC, B CC, we add:

A aC, A CC

the same language as G.

General Conversion Steps [Step 4]

Step 4: Suppose we have a rule A u1 u2 uk,

where k > 2 and each ui is a variable or a terminal. We

replace this rule by

A u1A1, A1 u2A2, A2 u3A3, ,

Ak-2 uk-1uk

After the change, the string on the right side of any rule

is either of length 1 (a terminal) or length 2 (two

variables, or 1 variable + 1 terminal, or two terminals)

General Conversion Steps [Step 4 ..]

right side, we replace the terminal ui by a new variable

Ui and add the rule Ui ui

After the change, the string on the right side of any rule

is exactly a terminal or two variables

Example 2

grammar on the right side after the first step.

S ASA | aB S0 S

AB|S S ASA | aB

Bb| AB|S

Bb|

Example 2 ..

S0 S S0 S

S ASA | aB S ASA | aB | a

AB|S AB|S|

Bb| Bb

Example 2..

After that, we remove A

S0 S S0 S

S ASA | aB | a S ASA | aB | a |

AB|S| SA | AS | S

Bb AB|S

Bb

Before removing After removing

A A

7/21/2017 10:52 AM CS 120 SemesterII-2013 95

Example 2..

S0 S S0 ASA | aB | a |

S ASA | aB | a | SA | AS

SA | AS S ASA | aB | a |

AB|S SA | AS

Bb AB|S

Bb

After removing S S After removing S0 S

Example 2..

Then, we remove A B

S0 ASA | aB | a | S0 ASA | aB | a |

SA | AS SA | AS

S ASA | aB | a | S ASA | aB | a |

SA | AS SA | AS

AB|S Ab|S

Bb Bb

AB AB

7/21/2017 10:52 AM CS 120 SemesterII-2013 97

Example 2..

Then, we remove A S

S0 ASA | aB | a | S0 ASA | aB | a |

SA | AS SA | AS

S ASA | aB | a | S ASA | aB | a |

SA | AS SA | AS

Ab|S A b | ASA | aB |

a | SA | AS

Bb

Bb

AS AS

7/21/2017 10:52 AM CS 120 SemesterII-2013 98

Example 2...

Then, we apply Step 4

S0 AA1 | UB | a | SA |

S0 ASA | aB | a |

AS

SA | AS

S AA1 | UB | a | SA | AS

S ASA | aB | a |

A b | AA1 | UB | a | SA |

SA | AS

AS

A b | ASA | aB |

Bb

a | SA | AS

A1 SA

Bb

Ua After Step 4 Grammar

Before Step 4

is in CNF

- History 6Transféré parawadhesh786
- r05310501 Formal Languages and Automata TheoryTransféré parSRINIVASA RAO GANTA
- 2marks AITransféré parPuspha Vasanth R
- QUESTION BANK FOR COMPILER DESIGNTransféré parPRIYA RAJI
- Building Cvpr10Transféré parJelena Slović
- 113211687 Chapter 6 Ll k and Lr k Grammars Formal Language Automata TheoryTransféré parKaranpreet Singh
- CD Course FileTransféré parAshutosh Jharkhade
- PPL-Unit_2Transféré parEdwayne Cullen
- 9a.pdfTransféré parHiralDesai
- mca2ndyear_180815Transféré parMohit Rajput
- Creating a DSL with PythonTransféré parhenderson
- Compiler DesignTransféré parMilan Patel
- المحاضرة 8 معالجة اللغات الطبيعيةTransféré parLua
- Syntactic annotation of spontaneous speech--application to call-center.pdfTransféré pararpray
- Compiler Construction Chapter 6Transféré parHammadHussain
- StanfordParserTransféré parpthosa
- Bottom ParseTransféré parscribd_guard.bloodnok2391
- Syntax and SemanticsTransféré parJyothi Jyo
- grammarTransféré parTauseef Khan
- nullTransféré parapi-26318246
- 18 SemanticsTransféré parJanardhan Reddy
- CSE Total SyllabusTransféré parSuneeth Roy
- How to Conver PDF Doc in XiTransféré parwaseemqa
- Syllabus_of_B.Sc_Computer_Science_Semester_Pattern2013.pdfTransféré parSiva
- CSSA-Exam-2Transféré parAndreea Veringa
- lexpcfgssrrgTransféré parDomagoj Volarevic
- Batliner 1998 Speech-CommunicationTransféré parAmanda Martinez
- Introduction 1Transféré parAnurag Dhiman
- Fleischman Roy 2007Transféré parGeorge
- tmp52E5.tmpTransféré parFrontiers

- artifical intelligenceTransféré parplanket
- Lecture 01Transféré parSaimo Mghase
- 415 Quiz1 AnswersTransféré parSaimo Mghase
- 415 Quiz 1 AnswersTransféré parAshley Portega
- Microprocessor 8085 - notesTransféré parDr. N.Shanmugasundaram
- ddb07Transféré parAnurag Upadhyay
- Lecture 1Transféré parSaimo Mghase
- Lecture 2.pptxTransféré parSaimo Mghase
- 20Transféré parSaimo Mghase
- NDW ProjectManagementPlan V2.0Transféré parSaimo Mghase
- Microprocessor TutorialTransféré parVishnu Balachandran
- 10290792Transféré parSaimo Mghase
- inv.pdfTransféré parSaimo Mghase
- Lecture 2Transféré parSaimo Mghase
- 1111Transféré parMubeen Naeem
- Graphic Ass 01Transféré parPriyank_Agarwa_9041
- 8606_5767_3345Transféré parSaimo Mghase
- 08-tcp-dns.pdfTransféré parSaimo Mghase
- Midterm Sample 1Transféré parSaimo Mghase
- aie_logic_s (1)Transféré parSaimo Mghase
- Final 11spTransféré parSaimo Mghase
- ip secTransféré parSaimo Mghase
- Ans-Assi2.docTransféré parSaimo Mghase
- Chapter 17Transféré parSaimo Mghase
- lab11_1Transféré parSaimo Mghase
- Employee eTransféré parSaimo Mghase
- 6 WC Command Examples to Count Number of Lines, Words, Characters in LinuxTransféré parSaimo Mghase
- chieffTransféré parSaimo Mghase
- LudoTransféré parSaimo Mghase

- Stakeholder Influence MapTransféré parprasad
- 4dTransféré parSudip Mondal
- Topic on Microwave XPIC-BTransféré parHugo Medrano
- CCNA BlueprintTransféré parJeff Munoz
- Ebusiness AssignmentTransféré parAnonymous sXvOUKf
- hyss.pdfTransféré parEugenio Alejandro Pérez Reséndiz
- SYH_IE-Net_76Transféré parKukinjos
- Anritsu Understanding OtdrsTransféré parMathieu Bolle
- Sameesk.newTransféré parPraveen Kumar Madupu
- Rhino Advanced Surf ModelingTransféré pardvd_flc
- Information Technology Service Delivery Manager Infrastructure Resume Rodney RobbinsTransféré parRodneyRobbins2
- Power Quality in CablesTransféré parlisused
- Internet Programming Lab ManualTransféré parSriram Janakiraman
- QB Module8 (1)Transféré parVibhor Mittal
- Elevated Program Shortcut Without UAC Prompt - Create - Windows 7 Help ForumsTransféré parclu5t3r
- Advanced PhysicalTransféré pargurukumar
- Brain Mapping What is ItTransféré parRoohollah Basatnia
- hw3Transféré parAnritsi An
- 04 Shell ScriptsTransféré parRavishankar Sravanam
- Physics SUMMER MmfTransféré parNandanKumar
- 54268146-Citroen-AL4-Fault-Finding.pdfTransféré parpurysan
- LECTURE 5 IntegrationTransféré parAzeem Khan
- CS321-CS225_Chapter4Transféré parJoshuaChrisEspiritu
- Tutorial Solid WorksTransféré parLauraAnindyta
- Catalogo YorkTransféré parMarco Guachun
- InteliDrive IPC DatasheetTransféré parMuhammad Shoaib Hussain
- UAV Paparazzi SystemTransféré parElizabete Pacheco
- Desktop gear engineeringTransféré parAdam Foltz
- 7-2_Solving_Exponential_Equations_and_Inequalities.pdfTransféré parEnas Basheer
- DELL R610 TroubleshootingTransféré parlankoor