Title: More Applications of The Pumping Lemma
1More Applicationsof The Pumping Lemma
2The Pumping Lemma
For infinite context-free language
there exists an integer such that
for any string
we can write
with lengths
and it must be
3Non-context free languages
Context-free languages
4Theorem
The language
is not context free
Proof
Use the Pumping Lemma for context-free languages
5Assume for contradiction that
is context-free
Since is context-free and infinite we can
apply the pumping lemma
6Pumping Lemma gives a magic number such that
Pick any string of with length at least
we pick
7We can write
with lengths and
Pumping Lemma says
for all
8We examine all the possible locations of string
in
9Case 1
is within the first
10Case 1
is within the first
11Case 1
is within the first
12Case 1
is within the first
However, from Pumping Lemma
Contradiction!!!
13Case 2
is in the first
is in the first
14Case 2
is in the first
is in the first
15Case 2
is in the first
is in the first
16Case 2
is in the first
is in the first
However, from Pumping Lemma
Contradiction!!!
17Case 3
overlaps the first
is in the first
18Case 3
overlaps the first
is in the first
19Case 3
overlaps the first
is in the first
20Case 3
overlaps the first
is in the first
However, from Pumping Lemma
Contradiction!!!
21Case 4
in the first
Overlaps the first
Analysis is similar to case 3
22Other cases
is within
or
or
Analysis is similar to case 1
23More cases
overlaps
or
Analysis is similar to cases 2,3,4
24There are no other cases to consider
Since , it is impossible
to overlap
nor
nor
25In all cases we obtained a contradiction
Therefore
The original assumption that
is context-free must be wrong
Conclusion
is not context-free
26Non-context free languages
Context-free languages
27Theorem
The language
is not context free
Proof
Use the Pumping Lemma for context-free languages
28Assume for contradiction that
is context-free
Since is context-free and infinite we can
apply the pumping lemma
29Pumping Lemma gives a magic number such that
Pick any string of with length at least
we pick
30We can write
with lengths and
Pumping Lemma says
for all
31We examine all the possible locations of string
in
There is only one case to consider
32(No Transcript)
33(No Transcript)
34(No Transcript)
35(No Transcript)
36Since , for we have
37(No Transcript)
38However, from Pumping Lemma
Contradiction!!!
39We obtained a contradiction
Therefore
The original assumption that
is context-free must be wrong
Conclusion
is not context-free
40Non-context free languages
Context-free languages
41Theorem
The language
is not context free
Proof
Use the Pumping Lemma for context-free languages
42Assume for contradiction that
is context-free
Since is context-free and infinite we can
apply the pumping lemma
43Pumping Lemma gives a magic number such that
Pick any string of with length at least
we pick
44We can write
with lengths and
Pumping Lemma says
for all
45We examine all the possible locations of string
in
46Most complicated case
is in
is in
47(No Transcript)
48Most complicated sub-case
and
49Most complicated sub-case
and
50Most complicated sub-case
and
51and
52(No Transcript)
53However, from Pumping Lemma
Contradiction!!!
54When we examine the rest of the cases we also
obtain a contradiction
55In all cases we obtained a contradiction
Therefore
The original assumption that
is context-free must be wrong
Conclusion
is not context-free
56YACC
- Yet Another Compiler Compiler
57Yacc is a parser generator
Input A Grammar Output A parser for the
grammar
Reminder a parser finds derivations
58Example grammar
expr -gt ( expr ) expr '' expr
expr '-' expr expr '' expr
expr '/' expr - expr INT
The yacc code
expr '(' expr ')' expr '' expr
expr '-' expr expr '' expr
expr '/' expr - expr INT
59Exampe Input
10 3 4
Yacc Derivation
expr gt expr expr gt expr expr expr
gt 103 4
60Resolving Ambiguities
left '', '-' left '', '/' left UMINUS
expr '(' expr ')' expr '' expr
expr '-' expr expr '' expr expr
'/' expr '-' expr prec UMINUS
INT
61Actions
left '', '-' left '', '/' left UMINUS
expr '(' expr ')' 2 expr
'' expr 1 3 expr '-'
expr 1 - 3 expr '' expr
1 3 expr '/' expr
1 / 3 '-' expr prec
UMINUS -2 INT
1
62A Complete Yacc program
union int int_val left '', '-' left
'', '/' left UMINUS token ltint_valgt INT type
ltint_valgt expr start program
63program expr printf("Expr value d
\n", 1) error printf("YACC
syntax error near line d \n", linenum)
abort() expr '('
expr ')' 2 expr '' expr
1 3 expr '-' expr 1
- 3 expr '' expr 1 3
expr '/' expr 1 / 3 '-'
expr prec UMINUS -2
INT 1 include
"lex.yy.c"
64Execution Example
Input
10 20(3 - 4 25)
Output
Expr value 490
65The Lex Code
int linenum1 int temp_int \n
linenum \t / skip spaces
/ \/\/\n / ignore comments / ""
return '' "-" return '-' ""
return '' "/" return '/' ")"
return ')' "(" return '('
660-9 sscanf(yytext, "d", temp_int)
yylval.int_val temp_int return
INT . printf("LEX unknown input string
found in line d \n", linenum) abort()
67Compiling
yacc YaccFile lex LexFile cc y.tab.c -ly -ll -o
myparser
Executable myparser
68Another Yacc Program
union int int_val left '', '-' left
'', '/' left UMINUS token ltint_valgt INT type
ltint_valgt expr start program
69program stmt_list error
printf("YACC syntax error near line d \n",
linenum) abort()
stmt_list stmt_list stmt
stmt stmt expr ''
printf("Expr value d \n", 1)
70expr '(' expr ')' 2 expr
'' expr 1 3 expr '-' expr
1 - 3 expr '' expr
1 3 expr '/' expr 1 /
3 '-' expr prec UMINUS
-2 INT 1
include "lex.yy.c"
71Execution Example
Input
10 20(30 -67) / 4 34 35 - 123
-001 178/6
Output
Expr value -175 Expr value 1066 Expr value
22
72Lex Code
int linenum1 int temp_int \n
linenum \t / skip spaces
/ \/\/\n / ignore comments /
73"" return '' "-" return '-' ""
return '' "/" return '/' ")"
return ')' "(" return '(' "" return
'' 0-9 sscanf(yytext, "d",
temp_int) yylval.int_val temp_int
return INT . printf("LEX unknown
input string found in line d \n", linenum)
abort()
74Another Yacc Program
union int int_val char str_val left
'', '-' left '', '/' left UMINUS token
PRINT token NEWLINE token ltstr_valgt
STRING token ltint_valgt INT type ltint_valgt
expr start program
75program stmt_list error
printf("YACC syntax error near line d \n",
linenum) abort()
stmt_list stmt_list stmt stmt
stmt expr ''
printf("expression found\n") PRINT
expr '' printf("d", 2)
PRINT STRING '' printf("s", 2)
PRINT NEWLINE '' printf("\n")
76 expr '(' expr ')' 2 expr
'' expr 1 3 expr '-' expr
1 - 3 expr '' expr
1 3 expr '/' expr 1 /
3 '-' expr prec UMINUS
-2 INT 1
include "lex.yy.c"
77Execution Example
Input
print "The value of expression 123 25 is
" print 123 25 print newline 10 5
8 print "end of program" print newline
Output
The value of expression 123 25 is
3075 expression found end of program
78Lex Code
int linenum1 int temp_int char
temp_str200 \n linenum \t
/ skip spaces / \/\/\n /
ignore comments /
79"" return '' "-" return '-'
"" return '' "/" return
'/' ")" return ')' "("
return '(' "" return '' "print"
return PRINT "newline" return NEWLINE
80 0-9 sscanf(yytext, "d", temp_int)
yylval.int_val temp_int
return INT \""\n\" strncpy(temp_str,
(yytext1), strlen(yytext)-2)
temp_strstrlen(yytext)-2 (char) 0
yylval.str_val temp_str
return STRING . printf("LEX unknown
input string found in line d \n", linenum)
abort()