Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Applications of The Pumping Lemma

Similar presentations


Presentation on theme: "More Applications of The Pumping Lemma"— Presentation transcript:

1 More Applications of The Pumping Lemma

2 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:

3 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 However, from Pumping Lemma:
Contradiction!!!

13 Case 2: is in the first is in the first

14 Case 2: is in the first is in the first

15 Case 2: is in the first is in the first

16 Case 2: is in the first is in the first However, from Pumping Lemma:
Contradiction!!!

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

20 Case 3: overlaps the first is in the first
However, from Pumping Lemma: Contradiction!!!

21 Case 4: in the first Overlaps the first Analysis is similar to case 3

22 Other cases: is within or or Analysis is similar to case 1:

23 More cases: overlaps or Analysis is similar to cases 2,3,4:

24 There are no other cases to consider
Since , it is impossible to overlap: nor 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 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 However, from Pumping Lemma:
Contradiction!!!

39 We obtained a contradiction
Therefore: The original assumption that is context-free must be wrong Conclusion: is not context-free

40 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 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 Yet Another Compiler Compiler
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
| INT ; The yacc code: expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | - expr | INT ;

59 Exampe Input: 10 * 3 + 4 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_val> INT %type <int_val> 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 UMINUS %token <int_val> INT %type <int_val> 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 Input: Output: 10 + 20*(30 -67) / 4;
34 * ; 17*8/6; 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 <str_val> STRING %token <int_val> INT %type <int_val> 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: Output:
print "The value of expression 123 * 25 is "; print 123 * 25; print newline; * 8; print "end of program"; 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();}


Download ppt "More Applications of The Pumping Lemma"

Similar presentations


Ads by Google