• No results found

Regular Languages

N/A
N/A
Protected

Academic year: 2022

Share "Regular Languages"

Copied!
148
0
0

Loading.... (view fulltext now)

Full text

(1)

Properties of Regular Languages

(2)

Standard Representations of Regular Languages

A language L over an alphabet S is regular

iff

• It is accepted by FA (DFA, NFA, or NFA-e).

• It is described by a regular expression.

• It is generated by regular grammar (Left-linear or Right-linear).

(3)

Standard Representations of Regular Languages

Regular Languages

Regular

Expressions

Regular Grammars (Left/Right Linear) FA

(DFA/NFA/NFA- e)

(4)

When we say: We are given a Regular Language

We mean:

L

Language is in a standard Representation (FA/RE/RG)

L

Standard Representations of Regular Languages

(5)

Closure Properties

• Recall a closure property is a statement that a certain operation on languages, when applied to languages in a class, produces a result that is also in that class.

• For regular languages, we can use any of its representations to prove a closure property.

Given two regular languages L

1

and L

2

, is their union is also regular ? If it is true for all regular

languages, then family of regular

languages is closed under union.

(6)

L 1 L 2

Are regular Languages For regular languages and

we will prove that:

Properties of RLs

(7)

We say: The family of Regular languages are

closed under

(8)

L 1 Regular language

( ) M 1 L 1

L = M 1

Single final state

NFA M 2

L 2

Single final state

( ) M 2 L 2

L =

Regular language

NFA

(9)

Example

a

b

M 1

{ } ba

L 2 = b a

M 2

L

1

={a

n

b :n≥0}

(10)

Union

NFA for

M 1

M 2

!

"

∪ !

$

l l l

l

(11)

Example

a

b

b a

l l l

l

}

1 { a b L = n

}

2 { ba L =

} {

}

2 {

1 L a b ba

L È = n È

NFA for

(12)

Concatenation

NFA for L 1 L 2

M 1 M 2

l l

(13)

Example

NFA for

a

b b a

}

1 { a b L = n

}

2 { ba L =

} {

} }{

2 {

1 L a b ba a bba

L = n = n

l l

³ 0

n

(14)

Star Operation

NFA for L 1 *

M 1

l

l

1 * Î L

l

l l

(15)

Example

The string in L

1

* is w NFA for

* } {

1 * a b L = n

a

b

}

1 { a b L = n l

l

l

l

1 2 1

L w

w w

w w

i

k

Î

= !

(16)

Reversal

L 1 R

M 1

NFA for 1 ¢ M

1. Reverse all transitions

2. Mark initial state as final state and vice versa

L 1

(17)

Example

}

1 { a b L = n

a

b

M 1

}

1 R { ba n

L =

a

b

1 ¢

M

(18)

Complement

1. Take the DFA that accepts L 1

M 1

L 1 ¢

M 1

L 1

2. Make final states non-final,

and vice-versa

(19)

Example

}

1 { a b L = n

a

b

M 1

b a,

b a,

} {

* } ,

1 { a b a b

L = - n a

b

1 ¢ M

b a,

b

a,

(20)

Intersection

DeMorgan’s Law: L 1 Ç L 2 = L 1 È L 2

2 1

, L

L regular

2 1

, L

L regular

2

1

L

L È regular

2

1

L

L È regular

2

1

L

L Ç regular

(21)

Example

}

1 { a b L = n

} ,

2 { ab ba L =

regular regular

}

2 {

1 L ab

L Ç =

regular

(22)

Example

a b

b a

b a,

b

} a,

2 {

1 L ab

L Ç =

(23)

Intersection (Product)

L1 = L(M1), M1 = (Q, S, d1, q0, F1), Q = {q0, q1…... qm,} L2 = L(M2), M2 = (P, S, d2, p0, F2), Q = {p0, p1…... pn,} M1 and M2 are DFAs.

Define new automation, M’ = (Q’, S, d’, (q0, p0), F’) Q’ = QXP = {(qi, pj): qi Q, pj P}

Define transition function d’ s.t.

d’( (qi, pj), a ) = (qk, pl) If d1(qi, a ) = qk

AND d2(pj, a ) = pl

Define transition for all states.

The initial state is (q0, p0),

F’ is the set of all states s.t. (qi, pj) s.t.

qi, F1, pj F2

(24)

Example-1

(25)

Example-2

(26)

Find product

(27)

DIFFERENCE

L1 = L(M1), M1 = (Q, S, d1, q0, F1), Q = {q0, q1…... qm,} L2 = L(M2), M2 = (P, S, d2, p0, F2), Q = {p0, p1…... pn,} M1 and M2 are DFAs

!

"

− !

$

= !

"

∩ !

$

2 1

, L

L regular

regular regular

!

"

, !

$

!

"

∩ !

$

(28)

DIFFERENCE

If L1 and L2 are regular languages, then So is L1 - L2 L1 - L2 consists of strings in L1 but not L2 i.e.

!

"

− !

$

= !

"

∩ !

$

Let M1 and M2 be DFA’s whose languages are L1 and L2, respectively.

Construct C, the product automaton of M1 and M2. Make the final states of C be the pairs where M1-state is final but M2-state is not.

(29)

Example

(30)

Exercise

L

1

= L ( (a+b)a*) L

2

= L( baa*)

For given languages L

1

and L

2

, construct the FAs.

• Union

• Star closure

• Concatenation

• Complement

• Reversal

• Intersection

• Difference

(31)

Exercise

L1 = L ( (a+b)a*) L2 = L( baa*)

!

"

⊝ !

$

= {': ' ∈ !

"

*+ ' ∈

!

$

,-. ' /0 1*. /1 ,*.ℎ !

"

314 !

$

} 1*+ (!

"

, !

$

) = {': ' ∉ !

"

314 ' ∉ !

$

}

:*+ (!

"

, !

$

) = {' ∶ ' ∈ !

"

*+ ' ∈ !

$

}

! = {-<: - ∈ !, < ∈ !

=

}

(32)

Exercise

L1 = {w ε {0, 1 }* : w consists of 0’s in multiple of 3}

L2 = {w ε {0, 1 }* : w consists of odd number of 0’s}

!

"

⊝ !

$

= {': ' ∈ !

"

*+ ' ∈

!

$

,-. ' /0 1*. /1 ,*.ℎ !

"

314 !

$

} 1*+ (!

"

, !

$

) = {': ' ∉ !

"

314 ' ∉ !

$

}

:*+ (!

"

, !

$

) = {' ∶ ' ∈ !

"

*+ ' ∈ !

$

}

! = {-<: - ∈ !, < ∈ !

=

}

(33)

Exercise

L

1

= {w ε {0, 1 }* : w consists of 0’s in multiple of 3}

L

2

= {w ε {0, 1 }* : w consists of odd number of 0’s}

For given languages L

1

and L

2

, find FA for

• Union

• Star closure

• Concatenation

• Complement

• Reversal

• Intersection

• Difference

(34)

Reversal of Regular Expression

Basis: If r is a primitive RE (∅, λ or a ε ∑ ), then rR= r.

Induction: If r1 and r2 are two REs (r1 + r2)R = (r1)R + (r2)R

(r1r2)R = (r2)R.(r1)R (r1*)R = ((r1)R)*

(35)

Reversal of Regular Expression: Example

Let r1 = 01* , r2 =10*.

(r1 + r2)R = (01* + 10*)R = (01*)R+ (10*)R

= (1*)R0R + (0*)R1R

= (1R)*0 + (0R)*1

= 1*0+ 0*1

(36)

Homomorphism

A homomorphism on an alphabet is a function that gives a string for each symbol in that alphabet.

h : ∑ Γ*

Where ∑ and Γ are alphabets.

Rules: h(a1a2 … an-1an) = h(a1)h(a2) …… h(an-1)h(an) h(a1+ a2) = h(a1) + h(a2)

h( (a1)* ) = (h(a1))*

Example: h(0) = ab; h(1) = λ

∑ = {0, 1} Γ ={a, b}

h(01010) = h(0)h(1)h(0)h(1)h(0)

= (ab)(λ)(ab)(λ)(ab)(λ)(ab) = ababab

(37)

Closure Under Homomorphism

If L is a regular language, and h is a homomorphism on the alphabet of language L.

Then h(L) = {h(w) | w εL} is also a regular language.

Proof: Let r be a regular expression for L.

Apply h to each symbol in r to form a new RE r’

The new RE r’ generates language h(L).

(38)

Closure Under Homomorphism : Example

Let h(0) =ab; h(1) = λ.

∑ = {0, 1} Γ ={a, b}

If L is the RL with regular expression 01* + 10*.

Then, h(L) is the homomorphism on the alphabet of language L.

The regular expression of language h(L) is r’ = h(01* +10*)

= h(0)(h(1))* + h(1) (h(0)*

= (ab)(λ)* + (λ)(ab)*

= (ab)λ + λ (ab)*

= (ab) + (ab)*

= (ab)*

(39)

Closure Under Homomorphism : Example

Let h(a) =dbcc; h(b) = bdc.

∑ = {a, b} Γ ={b, c, d}

If L is the regular language denoted by RE r = (a + b*) (aa*).

Show that h(L) is also regular language.

(40)

Right Quotient of Languages

Let L

1

and L

2

be languages on the same alphabet.

Then the right quotient of L

1

with L

2

is

L

1

/ L

2

= {x: xy L

1

for some y L

2

}

In other words, if the string in L

1

has a

suffix from L

2

, remove the suffix and the

resulting string is in L

1

/L

2

(41)

Right Quotient of Languages: Example

L1 = {anbm : n ≥ 1, m ≥ 0} ∪ {ba}

L1 = {ba, a, aa, aaa, …. ab, abb, …… aab, aabb……}

L2 = {bm : m ≥ 1}

L2 = {b, bb, bbb, ….. }

Then the right quotient of L1 with L2 is L1 / L2 = {anbm : n ≥ 1, m ≥ 0}

(42)

Closure under right quotient

If L

1

, L

2

are regular then L

1

/L

2

is regular.

L

1

is regular so it has a FA.

For each node in the FA of L

1

, check if there is a walk from that node to a final node using a string in L

2

.

If so, mark that node final.

So L

1

/L

2

is regular.

(43)

Example

L

1

= L(a*baa*); L

2

= L(ab*) DFA for L

1

:

q0 q1 q2

q3

a

b a

a

a, b

b b

(44)

Example

L

1

= L(a*baa*); L

2

= L(ab*) DFA for L

2

:

L

2

= {a, ab, abb … }

p0 p1

p2

b a

a

a, b

b

(45)

Example

L

1

= L(a*baa*); L

2

= L(ab*)

Remove final marking, remember final state is q2.

q0 q1 q2

q3

a

b a a

a, b b b

L

2

= {a, ab, abb … }

(46)

Example

L

1

= L(a*baa*); L

2

= L(ab*)

For each node, look for a walk on element of L2 to q2.

q0 q1 q2

q3

a

b a a

a, b b b

L

2

= {a, ab, abb … }

(47)

Example

L

1

= L(a*baa*); L

2

= L(ab*) DFA for L

1

/L

2

:

q0 q1 q2

q3

a

b a a

a,b

b b

(48)

Find Right Quotient

L

1

= {a

n

b

m

: n ≥1, m ≥0} ∪ {ba}

L

2

= {b

m

: m ≥ 1 }

L

1

/L

2

= ?

(49)

Exercise

L

1

=L(a*baa*) L

2

=L(aba*)

Find L

1

/L

2

If head of a language is defined as head(L) = {x : xy L for some y

∑*}

Find head(L

1

) and head(L

2

)

(50)

Elementary Questions about

Regular Languages

(51)

Membership Question

Question: Given regular language and string

how can we check if ?

L

L w Î

w

Answer: Take the DFA that accepts and check if is accepted

L

w

(52)

DFA

L w Î

DFA

L w Ï w

w

If there is a path from initial state to final state with labeled w; Accepted

If there is no path from initial state to final state with labeled w; Rejected

(53)

Given regular language how can we check

if is empty: ?

L L

Take the DFA that accepts

Check if there is any path from the initial state to a final state

L )

( L = Æ

Question:

Answer:

(54)

DFA

Æ

¹ L

DFA

Æ

=

L

(55)

Given regular language how can we check

if is finite?

L L

Take the DFA that accepts

Check if there is a walk with cycle from the initial state to a final state

L

Question:

Answer:

(56)

DFA

L is infinite

DFA

L is finite

(57)

Given regular languages and

how can we check if ? L 1 L 2

2

1 L

L =

Question:

Æ

= Ç

È

Ç ) ( ) ( L 1 L 2 L 1 L 2

Find if

Answer:

(58)

Æ

= Ç

È

Ç ) ( ) ( L 1 L 2 L 1 L 2

Æ

= Ç 2

1 L

L and L 1 Ç L 2 = Æ

2

1 L

L = L 1

L 2 L 2 L 1

2

1 L

L Í L 2 Í L 1

L 2 L 1

(59)

Æ

¹ Ç

È

Ç ) ( ) ( L 1 L 2 L 1 L 2

Æ

¹ Ç 2

1 L

L or L 1 Ç L 2 ¹ Æ L 1

L 2 L 2 L 1

2

1 L

L Ë L 2 Ë L 1

2

1 L

L ¹

(60)

Non-regular languages

(61)

Regular Languages

Every finite language is regular.

Some infinite languages are regular L

1

={a

n

b : n >=0}

Regularity:

The language is regular if, in processing any string, the information that has to be

remembered at any stage is strictly limited.

Some infinite languages are not regular

L

2

= {a

n

b

n

: n >=0}

(62)

How can we prove that a language L is regular?

Prove that there is DFA M that accepts L

Prove that there is RE r that generates L

Prove that there is RG G that generates L

(63)

Regular languages

b

a * b * c + a

...

etc

* ) ( a b c

b + +

Non-regular languages { a n b n : n ³ 0 }

}*}

, { :

{ vv R v Î a b

(64)

How can we prove that a language is not regular?

L

Prove that there is no DFA that accepts L

Problem: this is not easy to prove

Solution: the Pumping Lemma !!!

The pumping Lemma uses pigeonhole principle.

(65)

The Pigeonhole Principle

(66)

pigeons

pigeonholes

4

3

(67)

A pigeonhole must

contain at least two pigeons

(68)

...

...

pigeons

pigeonholes

n

m n > m

(69)

The Pigeonhole Principle

...

pigeons

pigeonholes

n m

m

n > There is a pigeonhole

with at least 2 pigeons

(70)

The Pigeonhole Principle

If we put n objects into m boxes and if n > m

• then at least one box must have more than

one object in it.

(71)

The Pigeonhole Principle and

DFAs

(72)

DFA with 4 states

q 1 a q 2 q 3 b

q 4

b

b b

b

a a

(73)

aab

q 1 a q 2 q 3 b

q 4

b

b

b

a a

a

In walks of strings: no state

is repeated

(74)

abab aabb aabbb abbabb

In walks of strings:

q 1 a q 2 q 3 b

q 4

b

b

b

a a

a

a state

is repeated

(75)

If string has length :

q 1 a q 2 q 3 b

q 4

b

b

b

a a

a

w | w | ³ 4

Thus, a state must be repeated Then the transitions of string

are more than the states of the DFA

w

(76)

In general, for any DFA:

String has length number of states w ³

A state must be repeated in the walk of q w

... q ...

walk of w

Repeated state

(77)

In other words for a string : transitions are pigeons states are pigeonholes

q a

w

... q ...

walk of w

Repeated state

(78)

The Pumping Lemma

(79)

Take an infinite regular language L

There exists a DFA that accepts L

states m

(80)

Take string with w w Î L

There is a walk with label : w

...

walk w

(81)

If string has length w | w | ³ m

(number

of states of DFA)

then, from the pigeonhole principle:

a state is repeated in the walk w

... q ...

walk w

(82)

q

... q ...

walk w

Let be the first state repeated in the

walk of w

(83)

Write w = x y z

... q ...

x

y

z

(84)

... q ...

x

y

z

Observations: length | x y | £ m number of states of DFA

1

|

| y ³

length

(85)

The string is accepted.

z

Observation: x

... q ...

x

y

z

(86)

The string x y z

is accepted Observation:

... q ...

x

y

z

(87)

The string is accepted

z y y

Observation: x

... q ...

x

y

z

(88)

The string is accepted

z y y y

Observation: x

... q ...

x

y

z

(89)

The string is accepted

z y

x i

In General:

...

, 2 , 1 ,

= 0 i

... q ...

x

y

z

(90)

L z

y x i

In General: i = 0 , 1 , 2 , ...

... q ...

x

y

z

Language accepted by the DFA

(91)

In other words, we described:

The Pumping Lemma !!!

(92)

The Pumping Lemma:

• Given a infinite regular language L

• there exists an integer for any string m

with length

L

w Î | w | ³ m

Then, we we can decompose w = x y z

with and | x y | £ m | y | ³ 1

such that: x y i z Î L i = 0 , 1 , 2 , ...

(93)

Applications of

the Pumping Lemma

(94)

Theorem: The language L = { a n b n : n ³ 0 }

is not regular

Proof: Use the Pumping Lemma

(95)

Assume for contradiction

that is a regular language L

Since is infinite.

we can apply the Pumping Lemma

L

} 0 :

{ ³

= a b n

L n n

(96)

Let be the integer in the Pumping Lemma

Pick a string such that: w w Î L

m w | ³

length |

m m b a

w =

We pick

m

} 0 :

{ ³

= a b n

L n n

(97)

it must be that length

From the Pumping Lemma

1

|

| ,

|

| x y £ m y ³

b ab

aa aa

a b

a

xyz = m m = ... ... ... ...

1

, ³

= a k

y k

x y z

m m

Write: a m b m = x y z

Thus:

(98)

From the Pumping Lemma: x y i z Î L ...

, 2 , 1 ,

= 0 i

Thus:

m m b a

z y

x =

L z

y

x 2 Î

1

, ³

= a k

y k

(99)

From the Pumping Lemma:

L b

ab aa

aa aa

a z

xy 2 = ... ... ... ... ... Î

x y z

k

m + m

Thus:

L z

y

x 2 Î

m m b a

z y

x = y = a k , k ³ 1

y

L b

a m + k m Î

(100)

L b

a m + k m Î

} 0 :

{ ³

= a b n L n n

BUT:

L b

a m + k m Ï

CONTRADICTION!!!

1

k

(101)

Our assumption that

is a regular language is not true

L

Conclusion: L is not a regular language

Therefore:

(102)

More Applications of

the Pumping Lemma

(103)

Theorem: The language is not regular

Proof: Use the Pumping Lemma

*}

:

{ Î S

= vv v

L R S = { a , b }

(104)

Assume for contradiction

that is a regular language L

Since is infinite

we can apply the Pumping Lemma

L

*}

:

{ Î S

= vv v

L R

(105)

m m

m

m b b a a

w =

We pick

Let be the integer in the Pumping Lemma

Pick a string such that: w w Î L

m w | ³

length | m

and

*}

:

{ Î S

= vv v

L R

(106)

Write a m b m b m a m = x y z

it must be that length

From the Pumping Lemma

a ba

bb ab

a aa

a

xyz = ... ... ... ... ... ...

x y z

m m m m

1

|

| ,

|

| x y £ m y ³

1

, ³

= a k

y k

Thus:

(107)

From the Pumping Lemma: x y i z Î L ...

, 2 , 1 ,

= 0 i

Thus: x y 2 z Î L

1

, ³

= a k

y k

m m

m

m b b a a

z y

x =

(108)

From the Pumping Lemma:

L a

ba bb

ab a

aa aa

a z

xy 2 = ... ... ... ... ... ... ...

x y z

k

m + m m m

1

, ³

= a k

y k

y

L z

y

x 2 Î

Thus:

m m

m

m b b a a

z y

x =

L a

b b

a m + k m m m Î

(109)

L a

b b

a m + k m m m Î

L a

b b

a m + k m m m Ï

BUT:

CONTRADICTION!!!

³ 1 k

*}

:

{ Î S

= vv v

L R

(110)

Our assumption that

is a regular language is not true

L

Conclusion: L is not a regular language

Therefore:

(111)

Regular languages Non-regular languages

} 0 ,

:

{ ³

= a b c + n l

L n l n l

(112)

Theorem: The language is not regular

Proof: Use the Pumping Lemma

} 0 ,

:

{ ³

= a b c + n l

L n l n l

(113)

Assume for contradiction

that is a regular language L

Since is infinite

we can apply the Pumping Lemma

L

} 0 ,

:

{ ³

= a b c + n l

L n l n l

(114)

m m

m b c a

w = 2

We pick

Let be the integer in the Pumping Lemma

Pick a string such that: w w Î L

m w | ³

length | m

} 0 ,

:

{ ³

= a b c + n l L n l n l

and

(115)

Write a m b m c 2 m = x y z

it must be that length

From the Pumping Lemma

c cc

bc ab

aa aa

a

xyz = ... ... ... ... ... ...

x y z

m m 2 m

1

|

| ,

|

| x y £ m y ³

1

, ³

= a k

y k

Thus:

(116)

From the Pumping Lemma: x y i z Î L ...

, 2 , 1 ,

= 0 i

Thus:

m m

m b c a

z y

x = 2

L xz

z y

x 0 =

1

, ³

= a k

y k

(117)

From the Pumping Lemma:

L c

cc bc

ab aa

a

xz = ... ... ... ... ... Î

x z

k

m - m 2 m

m m

m b c a

z y

x = 2 y = a k , k ³ 1 L

xz Î

Thus: a m - k b m c 2 m Î L

(118)

L c

b

a m - k m 2 m Î

L c

b

a m - k m 2 m Ï

BUT:

CONTRADICTION!!!

} 0 ,

:

{ ³

= a b c + n l L n l n l

³ 1

k

(119)

Our assumption that

is a regular language is not true

L

Conclusion: L is not a regular language

Therefore:

(120)

Regular languages

Non-regular languages L = { a n ! : n ³ 0 }

(121)

Theorem: The language

is not regular

Proof: Use the Pumping Lemma

} 0 :

{ ! ³

= a n

L n

n n

n ! = 1 × 2 ! ( - 1 ) ×

(122)

Assume for contradiction

that is a regular language L

Since is infinite

we can apply the Pumping Lemma

L

} 0 :

{ ! ³

= a n

L n

(123)

!

a m

w =

We pick

Let be the integer in the Pumping Lemma

Pick a string such that: w w Î L

m w | ³

length | m

} 0 :

{ ! ³

= a n

L n

(124)

Write a m ! = x y z

it must be that length

From the Pumping Lemma

a aa

aa aa

aa a

a

xyz = m ! = ... ... ... ... ...

x y z

m m ! - m

1

|

| ,

|

| x y £ m y ³

m k

a

y = k , 1 £ £

Thus:

(125)

From the Pumping Lemma: x y i z Î L ...

, 2 , 1 ,

= 0 i

Thus:

!

a m

z y

x =

L z

y

x 2 Î

m k

a

y = k , 1 £ £

(126)

From the Pumping Lemma:

L a

aa aa

aa aa

aa a

z

xy 2 = ... ... ... ... ... ... Î

x y z

k

m + m ! - m

Thus:

!

a m

z y

x = y = a k , 1 £ k £ m L

z y

x 2 Î

y

L

a m ! + k Î

(127)

L a m ! + k Î

!

! k p

m + =

} 0 :

{ ! ³

= a n

L n

Since:

m k £

£ 1

There must exist such that: p

(128)

However:

)!

1 (

) 1 (

!

!

!

!

!

!

+

=

+

=

+

<

+

£

+

£

m

m m

m m

m

m m

m k m

m ! + for m > 1

)!

1 (

! + k < m + m

!

! k p

m + ¹ for any p

(129)

L a m ! + k Î

L a m ! + k Ï

BUT:

CONTRADICTION!!!

} 0 :

{ ! ³

= a n

L n

m k £

£

1

(130)

Our assumption that

is a regular language is not true

L

Conclusion: L is not a regular language

Therefore:

(131)

Exercise

Prove that the following languages are not regular.

• L ={a

n

b

l

a

k

: k ≥ n+l }

• L ={a

n

b

n

: n ≥ 0 } ∪ {a

n

b

n+1

: n ≥ 0 } ∪ {a

n

b

n+1

: n ≥ 0 }

• L ={a

n!

: n ≥ 1 }

Are the following languages are regular ?

• L ={w ε {a, b, c}* : |w| = 3n

a

(w)}

• L ={w

1

cw

2

: w

1

, w

2

ε {a, b}*, w

1

≠w

2

}

(132)

Exercise

Let L = {a

n

b

m

: n ≥ 100, m ≤ 50}

• Can you use the pumping lemma to show that L is regular ?

• Can you use the pumping lemma to show

that L is regular ?

(133)

Lex

(134)

Lex: a lexical analyzer

• A Lex program recognizes strings

• For each kind of string found

the lex program takes an action

(135)

Var = 12 + 9;

if (test > 20) temp = 0;

else

while (a < 20) temp++;

Lex

program

Identifier: Var Operand: = Integer: 12 Operand: + Integer: 9

Semicolumn: ; Keyword: if

Parenthesis: ( Identifier: test ....

Input

Output

(136)

In Lex strings are described with regular expressions

“if”

“then”

“+”

“-”

“=“

/* operators */

/* keywords */

Lex program

Regular expressions

(137)

(0|1|2|3|4|5|6|7|8|9)+ /* integers */

/* identifiers */

Regular expressions

(a|b|..|z|A|B|...|Z)+

Lex program

(138)

integers

[0-9]+

(0|1|2|3|4|5|6|7|8|9)+

(139)

(a|b|..|z|A|B|...|Z)+ [a-zA-Z]+

identifiers

(140)

Each regular expression

has an associated action (in C code)

Examples:

\n

Regular expression Action

linenum++;

[a-zA-Z]+ printf(“identifier”);

[0-9]+ prinf(“integer”);

(141)

Default action: ECHO;

Prints the string identified

to the output

(142)

A small lex program

%%

[a-zA-Z]+ printf(“Identifier\n”);

[0-9]+ printf(“Integer\n”);

[ \t\n] ; /*skip spaces*/

(143)

1234 test

var 566 78 9800

Input Output

Integer

Identifier

Identifier

Integer

Integer

Integer

(144)

%%

[a-zA-Z]+ printf(“Identifier\n”);

[0-9]+ prinf(“Integer\n”);

[ \t] ; /*skip spaces*/

. printf(“Error in line: %d\n”, linenum);

Another program

%{

int linenum = 1;

%}

\n linenum++;

(145)

1234 test

var 566 78 9800 +

temp

Input Output

Integer Identifier Identifier Integer Integer Integer

Error in line: 3

Identifier

(146)

Lex matches the longest input string

“if”

“ifend”

Regular Expressions

Input: ifend if Matches: “ifend” “if”

Example:

(147)

Internal Structure of Lex

Lex

Regular

expressions NFA DFA Minimal DFA

The final states of the DFA are

associated with actions

(148)

Thank You

References

Related documents

We study a class of timed context-sensitive languages called dense-time multistack visibly pushdown languages (dtMVPLs), characterized by dense-time visibly pushdown multistack

– Nondeterministic finite state automata (also with ε-transitions) – Kleene’s regular expressions, also appeared as Type-3 languages in.. Chomsky’s

– Nondeterministic finite state automata (also with ε-transitions) – Kleene’s regular expressions, also appeared as Type-3 languages in!. Chomsky’s

• In this session, we will study a program to create a binary file, in which fixed length records are written. • Later, we will see how these records can be directly accessed,

All languages arising from MSGs are finitely generated, so the language ac- cepted by the message-passing automaton on Figure 2 shows that not all regular MSC languages can be

– Nondeterministic finite state automata (also with ε-transitions) – Kleene’s regular expressions, also appeared as Type-3 languages in.. Chomsky’s

Chen et al., “On Visual Similarity Based 3D Model Retrieval”, 2003... Comparing Shapes

Jitendra Kumar, student of Dayalbagh Educational Institute, Agra completed a 6-week Internship Programme under Hankernest Technologies Pvt.. As part-fulfillment of the