660 likes | 842 Views
Positive Properties of Context-Free languages. Union. Context-free languages are closed under:. Union. is context free. is context free. is context-free. Example. Language. Grammar. Union. In general:. For context-free languages with context-free grammars and start variables .
E N D
Positive Propertiesof Context-Free languages Costas Busch - RPI
Union Context-free languages are closed under: Union is context free is context free is context-free Costas Busch - RPI
Example Language Grammar Union Costas Busch - RPI
In general: For context-free languages with context-free grammars and start variables The grammar of the union has new start variable and additional production Costas Busch - RPI
Concatenation Context-free languages are closed under: Concatenation is context free is context free is context-free Costas Busch - RPI
Example Language Grammar Concatenation Costas Busch - RPI
In general: For context-free languages with context-free grammars and start variables The grammar of the concatenation has new start variable and additional production Costas Busch - RPI
Star Operation Context-free languages are closed under: Star-operation is context free is context-free Costas Busch - RPI
Example Language Grammar Star Operation Costas Busch - RPI
In general: For context-free language with context-free grammar and start variable The grammar of the star operation has new start variable and additional production Costas Busch - RPI
Negative Propertiesof Context-Free Languages Costas Busch - RPI
Intersection Context-free languages are not closed under: intersection is context free is context free not necessarily context-free Costas Busch - RPI
Example Context-free: Context-free: Intersection NOT context-free Costas Busch - RPI
Complement Context-free languages are not closed under: complement is context free not necessarily context-free Costas Busch - RPI
Example Context-free: Context-free: Complement NOT context-free Costas Busch - RPI
Intersectionof Context-free languagesand Regular Languages Costas Busch - RPI
The intersection of a context-free language and a regular language is a context-free language context free regular context-free Costas Busch - RPI
Machine Machine DFA for NPDA for regular context-free Construct a new NPDA machine that accepts simulates in parallel and Costas Busch - RPI
NPDA DFA transition transition NPDA transition Costas Busch - RPI
NPDA DFA transition NPDA transition Costas Busch - RPI
NPDA DFA initial state initial state NPDA Initial state Costas Busch - RPI
NPDA DFA final state final states NPDA final states Costas Busch - RPI
Example: context-free NPDA Costas Busch - RPI
regular DFA Costas Busch - RPI
context-free Automaton for: NPDA Costas Busch - RPI
In General: simulates in parallel and accepts string if and only if accepts string and accepts string Costas Busch - RPI
Therefore: is NPDA is context-free is context-free Costas Busch - RPI
Applications of Regular Closure Costas Busch - RPI
The intersection of a context-free language and a regular language is a context-free language Regular Closure context free regular context-free Costas Busch - RPI
An Application of Regular Closure Prove that: is context-free Costas Busch - RPI
We know: is context-free Costas Busch - RPI
We also know: is regular is regular Costas Busch - RPI
context-free regular (regular closure) context-free is context-free Costas Busch - RPI
Another Application of Regular Closure Prove that: is not context-free Costas Busch - RPI
Therefore, is not context free is context-free If (regular closure) Then context-free regular context-free Impossible!!! Costas Busch - RPI
Decidable Propertiesof Context-Free Languages Costas Busch - RPI
Parsers Membership Algorithms: • Exhaustive search parser • CYK parsing algorithm Membership Question: for context-free grammar find if string Costas Busch - RPI
Algorithm: • Remove useless variables • Check if start variable is useless Empty Language Question: for context-free grammar find if Costas Busch - RPI
Infinite Language Question: 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 for context-free grammar find if is infinite Costas Busch - RPI
Example: Infinite language Dependency graph Costas Busch - RPI
YACC Yet Another Compiler Compiler Costas Busch - RPI
Yacc is a parser generator Input: A Grammar Output: A parser for the grammar Reminder: a parser finds derivations Costas Busch - RPI
Example grammar: expr -> ( expr ) | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | - expr | INT ; The yacc code: expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | - expr | INT ; Costas Busch - RPI
Exampe Input: 10 * 3 + 4 Yacc Derivation: expr => expr + expr => expr * expr + expr => 10*3 + 4 Costas Busch - RPI
Resolving Ambiguities %left '+', '-' %left '*', '/' %left UMINUS %% expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ; Costas Busch - RPI
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;} ; Costas Busch - RPI
A Complete Yacc program %union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %% Costas Busch - RPI
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" Costas Busch - RPI
Execution Example Input: 10 + 20*(3 - 4 + 25) Output: Expr value = 490 Costas Busch - RPI