More Applications of The Pumping Lemma

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

Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
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 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.
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.
LEX and YACC work as a team
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
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.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
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:
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.
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.
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.
CS 310 – Fall 2008 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Yacc.
Syntax Analysis Part III
Non-regular languages
Standard Representations of Regular Languages
CSE322 PUMPING LEMMA FOR REGULAR SETS AND ITS APPLICATIONS
More Applications of the Pumping Lemma
Java CUP.
Simplifications of Context-Free Grammars
PDAs Accept Context-Free Languages
Syntax Analysis Part III
Bison: Parser Generator
Syntax Analysis Part III
Syntax Analysis Part III
Yet Another Compiler Compiler
Syntax Analysis Part III
Deterministic PDAs - DPDAs
Pumping Lemma for Context-free Languages
Elementary Questions about Regular Languages
Non-regular languages
More Applications of the Pumping Lemma
Yet Another Compiler Compiler
Compiler Lecture Note, Miscellaneous
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Applications of Regular Closure
CMPE 152: Compiler Design December 4 Class Meeting
Systems Programming & Operating Systems Unit – III
Faculty of Computer Science and Information System
Presentation transcript:

More Applications of The Pumping Lemma

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:

Non-context free languages

Theorem: The language is not context free Proof: Use the Pumping Lemma for context-free languages

Assume for contradiction that is context-free Since is context-free and infinite we can apply the pumping lemma

Pumping Lemma gives a magic number such that: Pick any string of with length at least we pick:

We can write: with lengths and Pumping Lemma says: for all

We examine all the possible locations of string in

Case 1: is within the first

Case 1: is within the first

Case 1: is within the first

Case 1: is within the first However, from Pumping Lemma: Contradiction!!!

Case 2: is in the first is in the first

Case 2: is in the first is in the first

Case 2: is in the first is in the first

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

Case 3: overlaps the first is in the first

Case 3: overlaps the first is in the first

Case 3: overlaps the first is in the first

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

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

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

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

There are no other cases to consider Since , it is impossible to overlap: nor nor

In all cases we obtained a contradiction Therefore: The original assumption that is context-free must be wrong Conclusion: is not context-free

Non-context free languages

Theorem: The language is not context free Proof: Use the Pumping Lemma for context-free languages

Assume for contradiction that is context-free Since is context-free and infinite we can apply the pumping lemma

Pumping Lemma gives a magic number such that: Pick any string of with length at least we pick:

We can write: with lengths and Pumping Lemma says: for all

We examine all the possible locations of string in There is only one case to consider

Since , for we have:

However, from Pumping Lemma: Contradiction!!!

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

Non-context free languages

Theorem: The language is not context free Proof: Use the Pumping Lemma for context-free languages

Assume for contradiction that is context-free Since is context-free and infinite we can apply the pumping lemma

Pumping Lemma gives a magic number such that: Pick any string of with length at least we pick:

We can write: with lengths and Pumping Lemma says: for all

We examine all the possible locations of string in

Most complicated case: is in is in

Most complicated sub-case: and

Most complicated sub-case: and

Most complicated sub-case: and

and

However, from Pumping Lemma: Contradiction!!!

When we examine the rest of the cases we also obtain a contradiction

In all cases we obtained a contradiction Therefore: The original assumption that is context-free must be wrong Conclusion: is not context-free

Yet Another Compiler Compiler YACC Yet Another Compiler Compiler

Yacc is a parser generator Input: A Grammar Output: A parser for the grammar Reminder: a parser finds derivations

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 ;

Exampe Input: 10 * 3 + 4 Yacc Derivation: expr => expr + expr => expr * expr + expr => 10*3 + 4

Resolving Ambiguities %left '+', '-' %left '*', '/' %left UMINUS %% expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ;

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

A Complete Yacc program %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %%

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"

Execution Example Input: 10 + 20*(3 - 4 + 25) Output: Expr value = 490

The Lex Code %{ int linenum=1; int temp_int; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; "+" {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();}

Compiling: yacc YaccFile lex LexFile cc y.tab.c -ly -ll -o myparser Executable: myparser

Another Yacc Program %union{ int int_val; } %left '+', '-' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %%

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

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"

Execution Example Input: Output: 10 + 20*(30 -67) / 4; 34 * 35 - 123 + -001; 17*8/6; Output: Expr value = -175 Expr value = 1066 Expr value = 22

Lex Code %{ int linenum=1; int temp_int; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */;

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

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 %%

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

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"

Execution Example Input: Output: print "The value of expression 123 * 25 is "; print 123 * 25; print newline; 10 + 5 * 8; print "end of program"; Output: The value of expression 123 * 25 is 3075 expression found end of program

Lex Code %{ int linenum=1; int temp_int; char temp_str[200]; %} %% \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */;

"+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';} ";" {return ';';} "print" {return PRINT;} "newline" {return NEWLINE;}

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