Sei sulla pagina 1di 79

Positive Properties of Context-Free languages

Courtesy Costas Busch - RPI

Union
Context-free languages are closed under: Union

L1 is context free L1 L2 L2 is context free


Courtesy Costas Busch - RPI

is context-free

Example Language Grammar

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

L1, L2 G1, G2 S1, S 2

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:

L1 is context free L1L2 L2 is context free


Courtesy Costas Busch - RPI

is context-free
5

Example Language Grammar

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

L1, L2 G1, G2 S1, S 2

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

Context-free languages are closed under: Star-operation

L is context free

L is context-free
8

Courtesy Costas Busch - RPI

Example Language Grammar

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

Negative Properties of Context-Free Languages

Courtesy Costas Busch - RPI

11

Intersection

Context-free languages are not closed under: intersection

L1 is context free L1 L2 L2 is context free


Courtesy Costas Busch - RPI

not necessarily context-free


12

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

Context-free languages are not closed under: complement

L is context free

not necessarily context-free

Courtesy Costas Busch - RPI

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

Intersection of Context-free languages and Regular Languages

Courtesy Costas Busch - RPI

16

The intersection of a context-free language and a regular language is a context-free language

L1 context free L1 L2 L2 regular


Courtesy Costas Busch - RPI

context-free
17

Machine

M1 L1

Machine DFA for regular

M2 L2

NPDA for

context-free

Construct a new NPDA machine that accepts L1 L2

M simulates in parallel M1 and M 2


Courtesy Costas Busch - RPI 18

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

L1 ! {w1w2 : | w1 |!| w2 |, w1 {a, b}* , w2 {c, d }*}

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

Courtesy Costas Busch - RPI

23

regular

L2 ! {a, c}

DFA

M2

a, c

p0

Courtesy Costas Busch - RPI

24

context-free

Automaton for: L1 L2 ! {a nc n : n u 0} NPDA a, P p 1


q0 , p0

M
c,1 p P
q3 , p0

P , P p P q1, p0 P , P p P q2 , p0 P , P p P

Courtesy Costas Busch - RPI

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

Applications of Regular Closure

Courtesy Costas Busch - RPI

28

The intersection of a context-free language and a regular language is a context-free language

L1 context free L2 regular

Regular Closure

L1 L2
context-free
Courtesy Costas Busch - RPI 29

An Application of Regular Closure

Prove that:

L ! {a b : n { 100, n u 0}
is context-free

n n

Courtesy Costas Busch - RPI

30

We know:

{a b : n u 0}

n n

is context-free

Courtesy Costas Busch - RPI

31

We also know:

L1 ! {a

100 100

is regular

L1 ! {( a  b) }  {a

100 100

is regular

Courtesy Costas Busch - RPI

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

Another Application of Regular Closure

Prove that:

L ! {w : na ! nb ! nc }
is not context-free

Courtesy Costas Busch - RPI

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,

L is not context free


Courtesy Costas Busch - RPI 35

Decidable Properties of Context-Free Languages

Courtesy Costas Busch - RPI

36

Membership Question: for context-free grammar find if string w L(G )

Membership Algorithms: Parsers Exhaustive search parser CYK parsing algorithm


Courtesy Costas Busch - RPI 37

w L(G ) ?

38

The CYK Parser

Courtesy Costas Busch - RPI

39

The CYK Membership Algorithm Input: Grammar String

G in Chomsky Normal Form

Output: find if

w L(G )
Courtesy Costas Busch - RPI 40

The CYK Membership Algorithm

41

The CYK Membership Algorithm

42

43

The Algorithm
Input example: Grammar

G : S p AB A p BB Apa B p AB B pb

String

w : aabbb
44

Courtesy Costas Busch - RPI

aabbb
a aa aab aabb aabbb a ab abb abbb b bb bbb b bb b

Courtesy Costas Busch - RPI

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

a A ab S,B abb abbb

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

a A ab S,B abb A abbb S,B

b B bb A bbb S,B

b B bb A

b B

Therefore:

aabbb L(G )

Time Complexity:

| w|

Observation:

The CYK algorithm can be easily converted to a parser (bottom up parser)


Courtesy Costas Busch - RPI 49

Empty Language Question: for context-free grammar find if L (G ) !

Algorithm: 1. Remove useless variables 2. Check if start variable


Courtesy Costas Busch - RPI

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

S p AB A p aCb | a B p bB | bb C p cBS S AB aCbB acBSbB acbbSbbb


  

S acbbSbbb (acbb) S (bbb) (acbb) S (bbb)


i i

Courtesy Costas Busch - RPI

53

Undecidable CFL Problems

54

YACC
Yet Another Compiler Compiler

Courtesy Costas Busch - RPI

55

Yacc is a parser generator Input: A Grammar

Output: A parser for the grammar

Reminder: a parser finds derivations


Courtesy Costas Busch - RPI 56

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

Exampe Input: 10 * 3 + 4

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

Courtesy Costas Busch - RPI

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

A Complete Yacc program


%union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %%
Courtesy Costas Busch - RPI 61

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:

Expr value = 490

Courtesy Costas Busch - RPI

63

%{ int linenum=1; int temp_int; %} %% \n {linenum++;}

The Lex Code

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

Courtesy Costas Busch - RPI

65

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

Executable: myparser

Courtesy Costas Busch - RPI

66

Another Yacc Program


%union{ int int_val; } %left '+', '-' %left '*', '/' %left UMINUS %token <int_val> INT %type <int_val> expr %start program %%
Courtesy Costas Busch - RPI 67

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

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"

Courtesy Costas Busch - RPI

69

Execution Example Input:

10 + 20*(30 -67) / 4; 34 * 35 - 123 + -001; 17*8/6;

Output:

Expr value = -175 Expr value = 1066 Expr value = 22


Courtesy Costas Busch - RPI 70

Lex Code
%{ int linenum=1; int temp_int; %} %% \n {linenum++;}

[\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */;

Courtesy Costas Busch - RPI

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

Courtesy Costas Busch - RPI

72

%union{ int int_val; char *str_val; }

Another Yacc Program

%left '+', '-' %left '*', '/' %left UMINUS %token PRINT %token NEWLINE %token <str_val> STRING %token <int_val> INT %type <int_val> expr %start program %%

Courtesy Costas Busch - RPI

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

Courtesy Costas Busch - RPI

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"

Courtesy Costas Busch - RPI

75

Execution Example Input:


print "The value of expression 123 * 25 is "; print 123 * 25; print newline; 10 + 5 * 8; print "end of program"; print newline;

Output:

The value of expression 123 * 25 is 3075 expression found end of program

Courtesy Costas Busch - RPI

76

Lex Code
%{ int linenum=1; int temp_int; char temp_str[200]; %} %% \n {linenum++;}

[\t ] /* skip spaces */; \/\/[^\n]* /* ignore comments */;

Courtesy Costas Busch - RPI

77

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

Courtesy Costas Busch - RPI

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

Courtesy Costas Busch - RPI

79

Potrebbero piacerti anche