Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Union
Context-free languages are closed under: Union
is context-free
L1 ! {a b } L2 ! {ww }
R
n n
S1 p aS1b | P S 2 p aS 2a | bS2b | P
Union
L ! {a b } {ww }
Courtesy Costas Busch - RPI
n n
S p S1 | S 2
3
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
Courtesy Costas Busch - RPI
L1 L2 S S p S1 | S 2
4
Concatenation
Context-free languages Concatenation are closed under:
is context-free
5
L1 ! {a b } L2 ! {ww }
R
n n
S1 p aS1b | P S 2 p aS 2a | bS2b | P
Concatenation
L ! {a b }{ww }
Courtesy Costas Busch - RPI
n n
S p S1S 2
6
In general: For context-free languages with context-free grammars and start variables
The grammar of the concatenation L1L2 S has new start variable and additional production S p S1S 2
Courtesy Costas Busch - RPI 7
Star Operation
L is context free
L is context-free
8
L ! {a b }
n n
S p aSb | P
Star Operation
L ! {a b } *
n n
S1 p SS1 | P
Courtesy Costas Busch - RPI 9
In general: For context-free language with context-free grammar and start variable
L G S
The grammar of the star operation L * S1 has new start variable and additional production S1 p SS1 | P
Courtesy Costas Busch - RPI 10
11
Intersection
Example
L1 ! {a b c }
Context-free:
n n m
L2 ! {a b c }
Context-free:
n m m
S p AC A p aAb | P C p cC | P
Intersection
S p AB A p aA | P B p bBc | P
L1 L2 ! {a b c } NOT context-free
Courtesy Costas Busch - RPI 13
n n n
Complement
L is context free
14
Example
L1 ! {a b c }
Context-free:
n n m
L2 ! {a b c }
Context-free:
n m m
S p AC A p aAb | P C p cC | P
Complement
S p AB A p aA | P B p bBc | P
n n n
L1 L2 ! L1 L2 ! {a b c }
NOT context-free
Courtesy Costas Busch - RPI 15
16
context-free
17
Machine
M1 L1
M2 L2
NPDA for
context-free
NPDA
M1
q2
p1
DFA
M2
a p2
q1
a, b p c
transition
transition
NPDA
q1, p1 a, b p c q2 , p2
transition
Courtesy Costas Busch - RPI 19
NPDA
M1
q2
p1
DFA
M2
q1
P, b p c transition
NPDA
P, b p c q1, p1 q2 , p1 transition
Courtesy Costas Busch - RPI 20
NPDA
M1
DFA
M2
p0
q0
initial state
initial state
NPDA
q0 , p0
Initial state
Courtesy Costas Busch - RPI 21
NPDA
M1
DFA
M2
p2
q1
final state
p1
final states
NPDA
M
q1, p2
22
q1, p1
final states
Courtesy Costas Busch - RPI
Example:
context-free
NPDA a, P p 1 b, P p 1
M1
c,1 p P d ,1 p P
q0 P , P p P q1 P , P p P q2 P , P p P q3
23
regular
L2 ! {a, c}
DFA
M2
a, c
p0
24
context-free
M
c,1 p P
q3 , p0
P , P p P q1, p0 P , P p P q2 , p0 P , P p P
25
In General:
M simulates in parallel M1 and M 2 M accepts string w if and only if M1 accepts string w M 2 accepts string w L ( M ) ! L ( M1 ) L( M 2 )
Courtesy Costas Busch - RPI 26
and
Therefore:
M is NPDA
L( M1 ) L( M 2 ) is context-free
L1 L2 is context-free
Courtesy Costas Busch - RPI 27
28
Regular Closure
L1 L2
context-free
Courtesy Costas Busch - RPI 29
Prove that:
L ! {a b : n { 100, n u 0}
is context-free
n n
30
We know:
{a b : n u 0}
n n
is context-free
31
We also know:
L1 ! {a
100 100
is regular
L1 ! {( a b) } {a
100 100
is regular
32
{a b }
context-free
n n
L1 ! {( a b) } {a
100 100
regular
(regular closure)
{a b } L1
n n
context-free
{a b } L1 ! {a b : n { 100, n u 0} ! L
is context-free
Courtesy Costas Busch - RPI 33
n n
n n
Prove that:
L ! {w : na ! nb ! nc }
is not context-free
34
If
L ! {w : na ! nb ! nc } is context-free
(regular closure)
Then
L {a * b * c*} ! {a b c }
regular context-free
n n n
context-free
Impossible!!!
Therefore,
36
w L(G ) ?
38
39
Output: find if
w L(G )
Courtesy Costas Busch - RPI 40
41
42
43
The Algorithm
Input example: Grammar
G : S p AB A p BB Apa B p AB B pb
String
w : aabbb
44
aabbb
a aa aab aabb aabbb a ab abb abbb b bb bbb b bb b
45
S p AB A p BB Apa B p AB B pb
aab aabb aabbb
Courtesy Costas Busch - RPI 46
a A aa
a A ab abb abbb
b B bb bbb
b B bb
b B
S p AB A p BB Apa B p AB B pb
aab aabb aabbb
Courtesy Costas Busch - RPI 47
a A aa
b B bb A bbb
b B bb A
b B
S p AB A p BB Apa B p AB B pb
a A aa aab S,B aabb A aabbb S,B
Courtesy Costas Busch - RPI 48
b B bb A bbb S,B
b B bb A
b B
Therefore:
aabbb L(G )
Time Complexity:
| w|
Observation:
S is useless
50
Infinite Language Question: for context-free grammar find if L (G ) 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 - RPI 51
Example:
S p AB A p aCb | a B p bB | bb C p cBS
Dependency graph
Infinite language
A
S
B
Courtesy Costas Busch - RPI 52
53
54
YACC
Yet Another Compiler Compiler
55
Example grammar:
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 - RPI 57
Exampe Input: 10 * 3 + 4
Yacc Derivation: expr => expr + expr => expr * expr + expr => 10*3 + 4
58
Resolving Ambiguities
%left '+', '-' %left '*', '/' %left UMINUS %% expr : '(' expr ')' | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | '-' expr %prec UMINUS | INT ;
Courtesy Costas Busch - RPI 59
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 - RPI 60
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 - RPI 62
Execution Example
Input:
10 + 20*(3 - 4 + 25)
Output:
63
[\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */; "+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';}
Courtesy Costas Busch - RPI 64
[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();}
65
Executable: myparser
66
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);}
68
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"
69
Output:
Lex Code
%{ int linenum=1; int temp_int; %} %% \n {linenum++;}
71
{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();}
72
%left '+', '-' %left '*', '/' %left UMINUS %token PRINT %token NEWLINE %token <str_val> STRING %token <int_val> INT %type <int_val> expr %start program %%
73
program : stmt_list | error {printf("YACC: syntax error near line %d \n", linenum); abort();} ; stmt_list : stmt_list stmt | stmt ; stmt : expr ';' | PRINT expr ';' | PRINT STRING ';' | PRINT NEWLINE ';' ; {printf("expression found\n");} {printf("%d", $2);} {printf("%s", $2);} {printf("\n");}
74
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"
75
Output:
76
Lex Code
%{ int linenum=1; int temp_int; char temp_str[200]; %} %% \n {linenum++;}
77
"+" {return '+';} "-" {return '-';} "*" {return '*';} "/" {return '/';} ")" {return ')';} "(" {return '(';} ";" {return ';';} "print" {return PRINT;} "newline" {return NEWLINE;}
78
[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();}
79