More Applications of The Pumping Lemma 1 The

































































![[0 -9]+ {sscanf(yytext, "%d", &temp_int); yylval. int_val = temp_int; return INT; }. {printf("LEX: unknown [0 -9]+ {sscanf(yytext, "%d", &temp_int); yylval. int_val = temp_int; return INT; }. {printf("LEX: unknown](https://slidetodoc.com/presentation_image/22a53c3f1061125474f209987113569b/image-66.jpg)





![Lex Code %{ int linenum=1; int temp_int; %} %% n {linenum++; } [t ] Lex Code %{ int linenum=1; int temp_int; %} %% n {linenum++; } [t ]](https://slidetodoc.com/presentation_image/22a53c3f1061125474f209987113569b/image-72.jpg)





![Lex Code %{ int linenum=1; int temp_int; char temp_str[200]; %} %% n {linenum++; } Lex Code %{ int linenum=1; int temp_int; char temp_str[200]; %} %% n {linenum++; }](https://slidetodoc.com/presentation_image/22a53c3f1061125474f209987113569b/image-78.jpg)

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