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