costas busch - rpi1 positive properties of context-free languages

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

If you can't read please download the document

Post on 20-Dec-2015

216 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Costas Busch - RPI1 Positive Properties of Context-Free languages
  • Slide 2
  • Costas Busch - RPI2 Context-free languages are closed under: Union is context free is context-free Union
  • Slide 3
  • Costas Busch - RPI3 Example Union LanguageGrammar
  • Slide 4
  • 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
  • Slide 5
  • Costas Busch - RPI5 Context-free languages are closed under: Concatenation is context free is context-free Concatenation
  • Slide 6
  • Costas Busch - RPI6 Example Concatenation LanguageGrammar
  • Slide 7
  • 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
  • Slide 8
  • Costas Busch - RPI8 Context-free languages are closed under: Star-operation is context freeis context-free Star Operation
  • Slide 9
  • Costas Busch - RPI9 Example Language Grammar Star Operation
  • Slide 10
  • 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
  • Slide 11
  • Costas Busch - RPI11 Negative Properties of Context-Free Languages
  • Slide 12
  • Costas Busch - RPI12 Context-free languages are not closed under: intersection is context free not necessarily context-free Intersection
  • Slide 13
  • Costas Busch - RPI13 Example Context-free: NOT context-free Intersection
  • Slide 14
  • Costas Busch - RPI14 Context-free languages are not closed under: complement is context freenot necessarily context-free Complement
  • Slide 15
  • Costas Busch - RPI15 NOT context-free Example Context-free: Complement
  • Slide 16
  • Costas Busch - RPI16 Intersection of Context-free languages and Regular Languages
  • Slide 17
  • Costas Busch - RPI17 The intersection of a context-free language and a regular language is a context-free language context free regular context-free
  • Slide 18
  • Costas Busch - RPI18 for NPDA DFA Construct a new NPDA machine that accepts Machine context-free regular simulates in parallel and
  • Slide 19
  • Costas Busch - RPI19 transition NPDADFA transition NPDA
  • Slide 20
  • Costas Busch - RPI20 transition NPDADFA transition NPDA
  • Slide 21
  • Costas Busch - RPI21 initial state NPDADFA Initial state NPDA
  • Slide 22
  • Costas Busch - RPI22 final state final states NPDADFA final states NPDA
  • Slide 23
  • Costas Busch - RPI23 Example: NPDA context-free
  • Slide 24
  • Costas Busch - RPI24 DFA regular
  • Slide 25
  • Costas Busch - RPI25 Automaton for: NPDA context-free
  • Slide 26
  • Costas Busch - RPI26 simulates in parallel and accepts stringif and only if accepts string and accepts string In General:
  • Slide 27
  • Costas Busch - RPI27 Therefore: is NPDA is context-free
  • Slide 28
  • Costas Busch - RPI28 Applications of Regular Closure
  • Slide 29
  • 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
  • Slide 30
  • Costas Busch - RPI30 An Application of Regular Closure Prove that: is context-free
  • Slide 31
  • Costas Busch - RPI31 We know: is context-free
  • Slide 32
  • Costas Busch - RPI32 is regular We also know:
  • Slide 33
  • Costas Busch - RPI33 regularcontext-free is context-free (regular closure)
  • Slide 34
  • Costas Busch - RPI34 Another Application of Regular Closure Prove that: is not context-free
  • Slide 35
  • Costas Busch - RPI35 context-freeregularcontext-free If is context-free Then Impossible!!! Therefore, is not context free (regular closure)
  • Slide 36
  • Costas Busch - RPI36 Decidable Properties of Context-Free Languages
  • Slide 37
  • Costas Busch - RPI37 Membership Question: for context-free grammar find if string Membership Algorithms: Parsers Exhaustive search parser CYK parsing algorithm
  • Slide 38
  • Costas Busch - RPI38 Empty Language Question: for context-free grammar find if Algorithm: 1.Remove useless variables 2.Check if start variable is useless
  • Slide 39
  • Costas Busch - RPI39 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
  • Slide 40
  • Costas Busch - RPI40 Example: Dependency graph Infinite language
  • Slide 41
  • Costas Busch - RPI41
  • Slide 42
  • Costas Busch - RPI42 YACC Yet Another Compiler Compiler
  • Slide 43
  • Costas Busch - RPI43 Yacc is a parser generator Input: A Grammar Output: A parser for the grammar Reminder: a parser finds derivations
  • Slide 44
  • Costas Busch - RPI44 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 ;
  • Slide 45
  • Costas Busch - RPI45 Exampe Input: 10 * 3 + 4 Yacc Derivation: expr => expr + expr => expr * expr + expr => 10*3 + 4
  • Slide 46
  • Costas Busch - RPI46 Resolving Ambiguities %left '+', '-' %left '*', '/' %left UMINUS % expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ;
  • Slide 47
  • Costas Busch - RPI47 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;} ;
  • Slide 48
  • Costas Busch - RPI48 A Complete Yacc program %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token INT %type expr %start program %
  • Slide 49
  • Costas Busch - RPI49 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"
  • Slide 50
  • Costas Busch - RPI50 Execution Example 10 + 20*(3 - 4 + 25) Input: Output: Expr value = 490
  • Slide 51
  • Costas Busch - RPI51 The Lex Code %{ int linenum=1; int temp_int; %} % \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; "+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';}
  • Slide 52
  • Slide 53
  • Costas Busch - RPI53 Compiling: yacc YaccFile lex LexFile cc y.tab.c -ly -ll -o myparser Executable: myparser
  • Slide 54
  • Costas Busch - RPI54 Another Yacc Program %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token INT %type expr %start program %
  • Slide 55
  • Costas Busch - RPI55 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);} ;
  • Slide 56
  • Costas Busch - RPI56 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"
  • Slide 57
  • Costas Busch - RPI57 Execution Example 10 + 20*(30 -67) / 4; 34 * 35 - 123 + -001; 17*8/6; Input: Output: Expr value = -175 Expr value = 1066 Expr value = 22
  • Slide 58
  • Costas Busch - RPI58 Lex Code %{ int linenum=1; int temp_int; %} % \n {linenum++;} [\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */;
  • Slide 59
  • Costas Busch - RPI59 "+" {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();}
  • Slide 60
  • Costas Busch - RPI60 Another Yacc Program %union{ int int_val; char *str_val; } %left '+', '-' %left '*', '/' %left UMINUS %toke

Recommended

View more >