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