Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.

Slides:



Advertisements
Similar presentations
PDAs Accept Context-Free Languages
Advertisements

Fall 2003Costas Busch - RPI1 Properties of Context-Free languages.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Prof. Busch - LSU1 Properties of Context-Free languages.
Costas Busch - RPI1 Single Final State for NFAs. Costas Busch - RPI2 Any NFA can be converted to an equivalent NFA with a single final state.
Fall 2006Costas Busch - RPI1 Regular Expressions.
1 Positive Properties of Context-Free languages. 2 Context-free languages are closed under: Union is context free is context-free.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Recursively Enumerable and Recursive Languages
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
1 Converting NPDAs to Context-Free Grammars. 2 For any NPDA we will construct a context-free grammar with.
Fall 2006Costas Busch - RPI1 The Post Correspondence Problem.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Costas Busch - RPI1 Positive Properties of Context-Free languages.
Fall 2003Costas Busch - RPI1 Decidable Problems of Regular Languages.
1 Single Final State for NFAs and DFAs. 2 Observation Any Finite Automaton (NFA or DFA) can be converted to an equivalent NFA with a single final state.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Fall 2006Costas Busch - RPI1 Properties of Regular Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
Courtesy Costas Busch - RPI1 Non-regular languages.
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 - RPI1 Decidable Problems of Regular Languages.
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 A Single Final State for Finite Accepters. 2 Observation Any Finite Accepter (NFA or DFA) can be converted to an equivalent NFA with a single final.
1 A Non Context-Free Language (We will prove it at the next class)
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
TM Design Universal TM MA/CSSE 474 Theory of Computation.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
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.
Costas Busch - LSU1 Properties of Context-Free languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
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.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
G. Pullaiah College of Engineering and Technology
PDAs Accept Context-Free Languages
Properties of Regular Languages
NPDAs Accept Context-Free Languages
Simplifications of Context-Free Grammars
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
Yet Another Compiler Compiler
CSE322 PROPERTIES OF REGULAR LANGUAGES
Properties of Regular Languages
Deterministic PDAs - DPDAs
The Post Correspondence Problem
Parsing Costas Busch - LSU.
Closure Properties of Context-Free languages
Decidable Problems of Regular Languages
Properties of Context-Free languages
Yet Another Compiler Compiler
More Applications of The Pumping Lemma
Applications of Regular Closure
Normal Forms for Context-free Grammars
Presentation transcript:

Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages

Courtesy Costas Busch - RPI2 Context-free languages are closed under: Union is context free is context-free Union

Courtesy Costas Busch - RPI3 Example Union LanguageGrammar

Courtesy Costas Busch - RPI4 In general: The grammar of the union has new start variable and additional production For context-free languages with context-free grammars and start variables

Courtesy Costas Busch - RPI5 Context-free languages are closed under: Concatenation is context free is context-free Concatenation

Courtesy Costas Busch - RPI6 Example Concatenation LanguageGrammar

Courtesy Costas Busch - RPI7 In general: The grammar of the concatenation has new start variable and additional production For context-free languages with context-free grammars and start variables

Courtesy Costas Busch - RPI8 Context-free languages are closed under: Star-operation is context freeis context-free Star Operation

Courtesy Costas Busch - RPI9 Example Language Grammar Star Operation

Courtesy Costas Busch - RPI10 In general: The grammar of the star operation has new start variable and additional production For context-free language with context-free grammar and start variable

Courtesy Costas Busch - RPI11 Negative Properties of Context-Free Languages

Courtesy Costas Busch - RPI12 Context-free languages are not closed under: intersection is context free not necessarily context-free Intersection

Courtesy Costas Busch - RPI13 Example Context-free: NOT context-free Intersection

Courtesy Costas Busch - RPI14 Context-free languages are not closed under: complement is context freenot necessarily context-free Complement

Courtesy Costas Busch - RPI15 NOT context-free Example Context-free: Complement

Courtesy Costas Busch - RPI16 Intersection of Context-free languages and Regular Languages

Courtesy Costas Busch - RPI17 The intersection of a context-free language and a regular language is a context-free language context free regular context-free

Courtesy Costas Busch - RPI18 for NPDA DFA Construct a new NPDA machine that accepts Machine context-free regular simulates in parallel and

Courtesy Costas Busch - RPI19 transition NPDADFA transition NPDA

Courtesy Costas Busch - RPI20 transition NPDADFA transition NPDA

Courtesy Costas Busch - RPI21 initial state NPDADFA Initial state NPDA

Courtesy Costas Busch - RPI22 final state final states NPDADFA final states NPDA

Courtesy Costas Busch - RPI23 Example: NPDA context-free

Courtesy Costas Busch - RPI24 DFA regular

Courtesy Costas Busch - RPI25 Automaton for: NPDA context-free

Courtesy Costas Busch - RPI26 simulates in parallel and accepts stringif and only if accepts string and accepts string In General:

Courtesy Costas Busch - RPI27 Therefore: is NPDA is context-free

Courtesy Costas Busch - RPI28 Applications of Regular Closure

Courtesy Costas Busch - RPI29 The intersection of a context-free language and a regular language is a context-free language context free regular context-free Regular Closure

Courtesy Costas Busch - RPI30 An Application of Regular Closure Prove that: is context-free

Courtesy Costas Busch - RPI31 We know: is context-free

Courtesy Costas Busch - RPI32 is regular We also know:

Courtesy Costas Busch - RPI33 regularcontext-free is context-free (regular closure)

Courtesy Costas Busch - RPI34 Another Application of Regular Closure Prove that: is not context-free

Courtesy Costas Busch - RPI35 context-freeregularcontext-free If is context-free Then Impossible!!! Therefore, is not context free (regular closure)

Courtesy Costas Busch - RPI36 Decidable Properties of Context-Free Languages

Courtesy Costas Busch - RPI37 Membership Question: for context-free grammar find if string Membership Algorithms: Parsers Exhaustive search parser CYK parsing algorithm

38

Courtesy Costas Busch - RPI39 The CYK Parser

Courtesy Costas Busch - RPI40 The CYK Membership Algorithm Input: Grammar in Chomsky Normal Form String Output: find if

41 The CYK Membership Algorithm

42 The CYK Membership Algorithm

43

Courtesy Costas Busch - RPI44 The Algorithm Grammar : String : Input example:

Courtesy Costas Busch - RPI45

Courtesy Costas Busch - RPI46

Courtesy Costas Busch - RPI47

Courtesy Costas Busch - RPI48

Courtesy Costas Busch - RPI49 Therefore: Time Complexity: The CYK algorithm can be easily converted to a parser (bottom up parser) Observation:

Courtesy Costas Busch - RPI50 Empty Language Question: for context-free grammar find if Algorithm: 1.Remove useless variables 2.Check if start variable is useless

Courtesy Costas Busch - RPI51 Infinite Language Question: for context-free grammar find if is infinite Algorithm: 1. Remove useless variables 2. Remove unit and productions 3. Create dependency graph for variables 4. If there is a loop in the dependency graph then the language is infinite

Courtesy Costas Busch - RPI52 Example: Dependency graph Infinite language

Courtesy Costas Busch - RPI53

54 Undecidable CFL Problems

Courtesy Costas Busch - RPI55 YACC Yet Another Compiler Compiler

Courtesy Costas Busch - RPI56 Yacc is a parser generator Input: A Grammar Output: A parser for the grammar Reminder: a parser finds derivations

Courtesy Costas Busch - RPI57 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 ;

Courtesy Costas Busch - RPI58 Exampe Input: 10 * Yacc Derivation: expr => expr + expr => expr * expr + expr => 10*3 + 4

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

Courtesy Costas Busch - RPI60 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;} ;

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

Courtesy Costas Busch - RPI62 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"

Courtesy Costas Busch - RPI63 Execution Example *( ) Input: Output: Expr value = 490

Courtesy Costas Busch - RPI64 The Lex Code %{ int linenum=1; int temp_int; %} % \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; "+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';}

Courtesy Costas Busch - RPI65 [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();}

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

Courtesy Costas Busch - RPI67 Another Yacc Program %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token INT %type expr %start program %

Courtesy Costas Busch - RPI68 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);} ;

Courtesy Costas Busch - RPI69 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"

Courtesy Costas Busch - RPI70 Execution Example *(30 -67) / 4; 34 * ; 17*8/6; Input: Output: Expr value = -175 Expr value = 1066 Expr value = 22

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

Courtesy Costas Busch - RPI72 "+" {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();}

Courtesy Costas Busch - RPI73 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 %

Courtesy Costas Busch - RPI74 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");} ;

Courtesy Costas Busch - RPI75 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"

Courtesy Costas Busch - RPI76 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

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

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

Courtesy Costas Busch - RPI79 [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();}