Costas Busch - RPI 2
The Pumping Lemma:
• Given a infinite regular language L
• there exists an integer m
• for any string with length Lw mw ||
• we can write zyxw
• with andmyx || 1|| y
• such that: Lzyx i ...,2,1,0i
Costas Busch - RPI 4
Theorem:The language
is not regular
Proof: Use the Pumping Lemma
*}:{ vvvL R },{ ba
Costas Busch - RPI 5
Assume for contradictionthat is a regular languageL
Since is infinitewe can apply the Pumping Lemma
L
*}:{ vvvL R
Costas Busch - RPI 6
mmmm abbaw We pick
Let be the integer in the Pumping Lemma
Pick a string such that: w Lw
mw ||length
m
and
*}:{ vvvL R
Costas Busch - RPI 7
Write zyxabba mmmm
it must be that length
From the Pumping Lemma
ababbabaaaaxyz ..................
x y z
m m m m
1||,|| ymyx
1, kay kThus:
Costas Busch - RPI 9
From the Pumping Lemma:
Lababbabaaaaaazxy ∈.....................=2
x y z
km + m m m
1, kay k
y
Lzyx 2
Thus:
mmmm abbazyx
Labba mmmkm
Costas Busch - RPI 11
Our assumption thatis a regular language is not true
L
Conclusion: L is not a regular language
Therefore:
Costas Busch - RPI 13
Theorem:The language
is not regular
Proof: Use the Pumping Lemma
}0,:{ lncbaL lnln
Costas Busch - RPI 14
Assume for contradictionthat is a regular languageL
Since is infinitewe can apply the Pumping Lemma
L
}0,:{ lncbaL lnln
Costas Busch - RPI 15
mmm cbaw 2We pick
Let be the integer in the Pumping Lemma
Pick a string such that: w Lw
mw ||length
m
}0,:{ lncbaL lnln
and
Costas Busch - RPI 16
Write zyxcba mmm 2
it must be that length
From the Pumping Lemma
cccbcabaaaaaxyz ..................
x y z
m m m2
1||,|| ymyx
1, kay kThus:
Costas Busch - RPI 17
From the Pumping Lemma: Lzyx i
...,2,1,0i
Thus:
mmm cbazyx 2
Lxzzyx ∈=0
1, kay k
Costas Busch - RPI 18
From the Pumping Lemma:
Lcccbcabaaaxz ...............
x z
km m m2
mmm cbazyx 2 1, kay k
Lxz
Thus: Lcba mmkm 2
Costas Busch - RPI 20
Our assumption thatis a regular language is not true
L
Conclusion: L is not a regular language
Therefore:
Costas Busch - RPI 22
Theorem:The language
is not regular
Proof: Use the Pumping Lemma
}0:{ ! naL n
nnn )1(21!
Costas Busch - RPI 23
Assume for contradictionthat is a regular languageL
Since is infinitewe can apply the Pumping Lemma
L
}0:{ ! naL n
Costas Busch - RPI 24
!mawWe pick
Let be the integer in the Pumping Lemma
Pick a string such that: w Lw
mw ||length
m
}0:{ ! naL n
Costas Busch - RPI 25
Write zyxam !
it must be that length
From the Pumping Lemma
aaaaaaaaaaaxyz m ...............!
x y z
m mm !
1||,|| ymyx
mkay k 1,Thus:
Costas Busch - RPI 27
From the Pumping Lemma:
Laaaaaaaaaaaazxy ..................2
x y z
km mm !
Thus:
!mazyx mkay k 1,
Lzyx 2
y
La km !
Costas Busch - RPI 29
However:
)!1(
)1(!
!!
!!
!
m
mm
mmm
mm
mmkm ! for 1m
)!1(! mkm
!! pkm for any p
Costas Busch - RPI 31
Our assumption thatis a regular language is not true
L
Conclusion: L is not a regular language
Therefore:
Costas Busch - RPI 33
Lex: a lexical analyzer
• A Lex program recognizes strings
• For each kind of string found the lex program takes an action
Costas Busch - RPI 34
Var = 12 + 9;
if (test > 20)
temp = 0;
else
while (a < 20)
temp++;
Lexprogram
Identifier: Var
Operand: =
Integer: 12
Operand: +
Integer: 9
Semicolumn: ;
Keyword: if
Parenthesis: (
Identifier: test
....
Input
Output
Costas Busch - RPI 35
In Lex strings are described with regular expressions
“if”“then”
“+”“-”“=“
/* operators */
/* keywords */
Lex programRegular expressions
Costas Busch - RPI 36
(0|1|2|3|4|5|6|7|8|9)+ /* integers */
/* identifiers */
Regular expressions
(a|b|..|z|A|B|...|Z)+
Lex program
Costas Busch - RPI 39
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”);
Costas Busch - RPI 41
A small lex program
%%
[a-zA-Z]+ printf(“Identifier\n”);
[0-9]+ printf(“Integer\n”);
[ \t\n] ; /*skip spaces*/
Costas Busch - RPI 42
1234 test
var 566 78
9800
Input Output
Integer
Identifier
Identifier
Integer
Integer
Integer
Costas Busch - RPI 43
%%
[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++;
Costas Busch - RPI 44
1234 test
var 566 78
9800 +
temp
Input Output
Integer
Identifier
Identifier
Integer
Integer
Integer
Error in line: 3
Identifier
Costas Busch - RPI 45
Lex matches the longest input string
“if”“ifend”
Regular Expressions
Input: ifend if
Matches: “ifend” “if”
Example: