Docsity

Free Languages - Computability - Lecture Slide, Slides of Computer Science

Partial preview of the text

Download Free Languages - Computability - Lecture Slide and more Slides Computer Science in PDF only on Docsity!

1

Context Free Languages

2

Agenda

Context Free Grammars

 Derivations  Parse-trees  CFGParse  Ambiguity

4

Motivating Example

pal = { x{0,1}* | x =x R^ }

A: Can generate pal recursively as

follows.

BASE CASE: e, 0 and 1 are palindromes

RECURSE: if x is a palindrome, then so

are 0x 0 and 1x 1.

5

Motivating Example

pal = { x{0,1}* | x =x R^ }

SUMMARY: In pal any x can be replaced by

any one of {e, 0, 1, 0x 0, 1x 1}.

NOTATION: x  e|0|1|0x 0|1x 1

Each pipe “|” is an or, just ad in UNIX regexp’s.

In fact, all elements of pal can be generated from e by using these rules.

Q: How would you generate 11011011 starting

from the variable x?

7

Context Free Grammars.

Definition

DEF: A context free grammar consists of

(V, S, R, S ) with:

 V – a finite set of variables (or symbols , or non- terminals )  S – a finite set set of terminals (or the alphabet )  R – a finite set of rules (or productions ) of the form v  w with v V, and w(S V )* (read “ v yields w ” or “ v produces w ” )  S  V – the start symbol.

Q: What are (V, S, R, S ) for pal?

8

Context Free Grammars.

Definition

A: V = {x}

S = {0,1},

R = {xe, x0, x1, x 0 x 0, x 1 x 1}

S = x

Standardize pal, reset:

V = {S }, S = S

R = {Se, S0, S1, S 0 S 0, S 1 S 1}

10

Derivations

A: S 0000S  y :

Any one of:

0000 S, 00000S, 10000S, 0S00000S,

1 S10000S, S0000, S00000, S00001,

S00000S0, S00001S

11

Derivations

DEF: The derivation symbol “*”, read

“derives” or “produces” or “yields” is a

relation between strings in (S V )*. We

write x *y if there is a sequence of 1-step

productions from x to y. I.e., there are

strings xi with i ranging from 0 to n such that

x = x 0 , y = xn and

x 0  x 1 , x 1  x 2 , x 2  x 3 , … , xn-1  xn

Q: Which of LHS’s yield which of RHS’s in pal?

01 S, SS ? 01, 0, 01S, 01110111, 0100111

13

Language Generated by a CFG

DEF: Let G be a context free grammar.

The language generated by G is the

set of all terminal strings which are

derivable from the start symbol.

Symbolically:

L(G ) = {w  S* | S * w}

14

Example. Infix Expressions

Infix expressions involving {+, , a, b, c, (, )}

E stands for an expression (most general)

F stands for factor (a multiplicative part)

T stands for term (a product of factors)

V stands for a variable, I.e. a, b, or c

Grammar is given by:

E  T | E+T

T  F | T  F

F  V | (E )

V  a | b | c

CONVENTION: Start variable is the first one (E)Docsity.com

16

Example. Infix Expressions

Continuing on in this fashion and summarizing:

E  E +T  T +T  T  F +T  F  F +T

 V  F+T  a F+T  a V+T  a  b +T

 a  b +F  a  b + (E )  a  b + (E +T )

 a b+(T+T)  a b+(F+T)  a b+(V+T )

 a  b + (c +T )  a  b + (c +F )

 a  b + (c + (E ) )  a  b + (c +(E +T ))

 a b+(c+(T+T ) )  a  b +(c + (F +T ) )

 a b+(c+(a+T ) )  a  b +(c +(a + F ) )

 a b+(c+(a+V ) )  a  b + (c + (a+c ) )

This is a so-called left-most derivation.

17

Right-most derivation

In a right-most derivation, the variable most to the right is replaced.

E  E +T  E + F  E + (E )  E + (E +T )

 E + (E +F )  etc.

There is a lot of ambiguity involved in how a string is derived. However, if decide that derivations are left-most, or right-most, each derivation is not unique.

Another way to describe a derivation in a unique way is using derivation trees.

19

Derivation Trees

EG, a derivation tree for

a  b + (c + (a + c ) )

Advantage. Derivation trees

also help understanding

semantics! You can tell how

expression should be

evaluated from the tree.

20

CFGParse

CFGParse is a tool that I wrote that helps you play with context free grammars and create derivation trees. Yoav Hirsch (TA) also added some functionality that allows you to convert CFG’s into various forms that we’ll learn about next week.

USAGE: java CFGParse <grammar-file> <input-string> pdflatex <LaTeX-tree-filename>

Second command only works on CUNIX, unless install LaTeX plus some libraries (ecltree).