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.

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Courtesy Costas Busch - RPI1 More Applications of the Pumping Lemma.
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Courtesy Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
1 More Applications of the Pumping Lemma. 2 The Pumping Lemma: Given a infinite regular language there exists an integer for any string with length we.
Costas Busch - RPI1 Positive Properties of Context-Free languages.
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
CS 310 – Fall 2006 Pacific University CS310 Lex & Yacc Today’s reference: UNIX Programming Tools: lex & yacc by: Levine, Mason, Brown Chapter 1, 2, 3 November.
Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
1 More Applications of the Pumping Lemma. 2 The Pumping Lemma: Given a infinite regular language there exists an integer for any string with length we.
Courtesy Costas Busch - RPI1 Non-regular languages.
Fall 2003Costas Busch1 More Applications of The Pumping Lemma.
Fall 2004COMP 3351 Standard Representations of Regular Languages Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Fall 2006Costas Busch - RPI1 More Applications of the Pumping Lemma.
1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)
Prof. Busch - LSU1 Pumping Lemma for Context-free Languages.
Prof. Busch - LSU1 More Applications of the Pumping Lemma.
1 Non-regular languages. 2 Regular languages Non-regular languages.
Costas Busch - RPI1 More Applications of the Pumping Lemma.
1 Applications of Regular Closure. 2 The intersection of a context-free language and a regular language is a context-free language context free regular.
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
Costas Busch1 More Applications of The Pumping Lemma.
Parser construction tools: YACC
LEX and YACC work as a team
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
1 Programming Languages (CS 550) Lecture 1 Summary Grammars and Parsing Jeremy R. Johnson.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Prof Busch - LSU1 YACC Yet Another Compiler Compiler.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Introduction to YACC CS 540 George Mason University.
1 Programming Languages (CS 550) Lecture 1 Summary Grammars and Parsing Jeremy R. Johnson.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
Costas Busch - LSU1 Pumping Lemma for Context-free Languages.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Compiler Construction Sohail Aslam Lecture Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The.
Syntax Analysis Part III
Standard Representations of Regular Languages
CSE322 PUMPING LEMMA FOR REGULAR SETS AND ITS APPLICATIONS
More Applications of the Pumping Lemma
Java CUP.
Syntax Analysis Part III
Bison: Parser Generator
Syntax Analysis Part III
Syntax Analysis Part III
Yet Another Compiler Compiler
Syntax Analysis Part III
Pumping Lemma for Context-free Languages
Elementary Questions about Regular Languages
More Applications of the Pumping Lemma
Yet Another Compiler Compiler
Compiler Lecture Note, Miscellaneous
More Applications of The Pumping Lemma
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Applications of Regular Closure
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

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();}