Homework Homework #2 returned Context Free Languages Homework #3 - - PDF document

homework
SMART_READER_LITE
LIVE PREVIEW

Homework Homework #2 returned Context Free Languages Homework #3 - - PDF document

Homework Homework #2 returned Context Free Languages Homework #3 due today Homework #4 Pg 133 -- Exercise 1 (use structural induction) Pg 133 -- Exercise 3 Pg 134 -- Exercise 8b,c,d Pg 135 -- Exercise 22 Pg 145 --


slide-1
SLIDE 1

1

Context Free Languages Homework

 Homework #2 returned  Homework #3 due today  Homework #4

 Pg 133 -- Exercise 1 (use structural induction)  Pg 133 -- Exercise 3  Pg 134 -- Exercise 8b,c,d  Pg 135 -- Exercise 22  Pg 145 -- Exercise 13

 Due 10 / 7

Announcements

 Exam 1

 Next class (Oct 2nd )  1 hour  Cover Regular Languages (up to and including

today’s lecture)

 Closed book (1 sheet study guide okay)

 Remainder of next class will be a problem

session

Before We Start

 Any questions?

Plan for today

 1st half

 Context Free Grammars and Languages

 2nd half

 Regular Languages and Context Free

Languages / L-Systems

Languages

 Recall.

 What is a language?  What is a class of languages?

slide-2
SLIDE 2

2

Regular Languages

 For the past several weeks, we have

been looking at Regular Languages:

 language: Regular Expression  Machine for accepting: Finite Automata

Last class we discovered

 Venn-diagram of languages

Regular Languages Finite Languages Is there something

  • ut here?

YES!

Context Free Languages

 Context Free Languages(CFL) is the

next class of languages outside of Regular Languages:

 Language / grammar: Context Free

Grammar

 Machine for accepting: Pushdown

Automata

Grammars

 Wikipedia says:

 languages can be described as a system of

symbols and the grammars (rules) by which the symbols are manipulated

 Grammar is the study of rules governing

the use of language.

Grammars

Let’s redefine grammars for CS Theory use:

1.

Terminals = Set of symbols that form the strings of the language being defined

2.

Variables = Set of symbols representing categories

3.

Start Symbol = variable that represents the “base category” that defines our language

4.

Production rules = set of rules that recursively define the language

Grammars

 Let’s formalize this a bit:

 A grammar is a 4-tuple: (V, T, P, S) where

 V is a set of variables  T is a set of terminals  P is a set of production rules  V and T are disjoint (I.e. V ∩ T = ∅)  S ∈V, is your start symbol

slide-3
SLIDE 3

3

General Grammars

 Production Rules

 Of the form A → B

 A is a string of terminals and variables  B is a string of terminals and variables  To apply a rule, replace any occurrence of A

with the string B.

Grammars

 Let’s formalize this a bit:

 Production rules

 We say that γ can be derived from α in one step:  A → β is a rule  α = α1A α2  γ = α1 β α2  α ⇒ γ  We write α ⇒* γ if γ can be derived from α in zero or more

steps.

Context Free Grammars

 Production Rules

 Of the form A → B

 A is a variable  B is a string, combining terminals and variables  To apply a rule, replace an occurrence of A with the

string B.

 We say that the grammar is context-free since this

substitution can take place regardless of where A is.

Context Free Grammars

 The language generated by a grammar

 Let G = (V, T, P, S)  The language generated by G, L(G)

 L(G) = { x ∈ T* | S ⇒* x}

 A language L is a Context Free

Language (CFL) iff there is a CFG G, such that

 L = L(G)

Example

 Recall our friend: Palindromes

 A palindrome is a string that is the same

read left to right or right to left

 First half of a palindrome is a “mirror

image” of the second half

 Examples:

 a, b, aba, abba, babbab.

Example

 Recursive definition for palindromes

(pal) over Σ

  • 1. λ ∈ pal
  • 2. For any a ∈ Σ, a ∈ pal
  • 3. For any x ∈pal and a ∈ Σ, axa ∈ pal
  • 4. No string is in pal unless it can be obtained

by rules 1-3

slide-4
SLIDE 4

4

Example

A CFG for palindromes over {a,b}

Base cases

1.

P → λ

2.

P → a

3.

P → b

Recursion

  • 4. P → aPa
  • 5. P → bPb

Back to CS Theory

 Building the palindrome abba using

grammar

 P ⇒ aPa (Rule 4)  P ⇒ abPba (Rule 5)  P ⇒ ab λ ba (Rule 1)  P ⇒ abba

Another Example

 Find a CFG to describe:

 L = {x ∈ {0,1}* | n0(x) = n1(x)}  Basic idea (define recursively)

 λ is certainly in the language  For all strings in the language, if we add a 0

and 1 to the string, the result is in the language.

 The concatenation of any two strings in the

language will also be in the language

Another Example

 Find a CFG to describe:

 L = {x ∈ {0,1}* | n0(x) = n1(x)}

 S → λ (1)  S → 0S1 (2)  S → 1S0 (3)  S → SS (4)

Another Example

 Let’s derive a string from L

 00110011  S ⇒ SS rule 4

⇒ 0S1 0S1 rule 2 ⇒ 00S11 00S11 rule 2 ⇒ 00 λ 11 00 λ 11 rule 1 = 00110011

Yet Another example

 Find a CFG to describe:

 L = {aibjck | i = k}

 Number of a’s equals the number of c’s with

any number of b’s between them

 Use variable B to represent bj  Every time you add a to the left of B you need

to add c to the right.

slide-5
SLIDE 5

5

Yet Another example

 Find a CFG to describe:

 L = {aibjck | i = k}

 S → B (1)  S → aSc (2)  B → bB (3)  B → λ (4)

 Can also write as

 S → B | aSc  B → bB | λ

Another example

 Let’s derive a string from L: aabbbcc

 S ⇒ aSc rule 2

S ⇒ aaScc rule 2 S ⇒ aaBcc rule 1 S ⇒ aabBcc rule 3 S ⇒ aabbBcc rule 3 S ⇒ aabbbBcc rule 3 S ⇒ aabbb λ cc rule 4 = aabbbcc

One more example

 Defining the grammar for algebraic

expressions:

 Let a = a numeric constant  Set of binary operators = {+, -, *, /}  Expressions can be parenthesized

One more example

 Defining the grammar for algebraic

expressions:

 G = (V, T, P, S)  V = {S}  T = { a, -, +, *, /, (, ) }  S = S  P = see next slide

One more example

 Defining the grammar for algebraic

expressions – Production rules

 S → S + S (1)

S → S – S (2) S → S * S (3) S → S / S (4) S → (S) (5) S → a (6)

One more example

 Show derivation for a + (a * a) / a

 S ⇒ S + S rule 1

S ⇒ a + S rule 6 S ⇒ a + S / S rule 4 S ⇒ a + (S) / S rule 5 S ⇒ a + (S * S) / S rule 3 S ⇒ a + (a * S) / S rule 6 S ⇒ a + (a * a) / S rule 6 S ⇒ a + (a * a) / a rule 6

slide-6
SLIDE 6

6

Parse Trees

 Graphical means to illustrate a

derivation of a string from a grammar

 Root of the tree = start variable  Interior nodes = other variables

 Children of nodes = application of a production

rule

 Leaf nodes = Terminal symbols

Another example

 Find a CFG to describe:

 L = {aibjck | i = k}

 S → B (1)  S → aSc (2)  B → bB (3)  B → λ (4)

 Can also write as

 S → B | aSc  B → bB | λ

Another example

 Let’s derive a string from L: aabbcc

 S ⇒ aSc rule 2

S ⇒ aaScc rule 2 S ⇒ aaBcc rule 1 S ⇒ aabBcc rule 3 S ⇒ aabbBcc rule 3 S ⇒ aabb λ cc rule 4 = aabbcc

Parse Tree

 An inorder traversal

  • f the tree will give

the the string derived.

S a S c a S c B B b B b

λ

Rule 2 Rule 2 Rule 1 Rule 3 Rule 3 Rule 4

Recall our example from last time

 Defining the grammar for algebraic

expressions – Production rules

 S → S + S (1)

S → S – S (2) S → S * S (3) S → S / S (4) S → (S) (5) S → a (6)

One more example

 Show derivation for a + a * a

 S ⇒ S + S rule 1

S ⇒ a + S rule 6 S ⇒ a + S * S rule 3 S ⇒ a + a * S rule 6 S ⇒ a + a * a rule 6

slide-7
SLIDE 7

7

Parse Tree

 S ⇒ S + S

rule 1 S ⇒ a + S rule 6 S ⇒ a + S * S rule 3 S ⇒ a + a * S rule 6 S ⇒ a + a * a rule 6 S S + S a S * S a a

One more example

 Another derivation for a + a * a

 S ⇒ S * S rule 3

S ⇒ S * a rule 6 S ⇒ S + S * a rule 1 S ⇒ a + S * a rule 6 S ⇒ a + a * a rule 6

Parse Tree

 S ⇒ S * S

rule 3 S ⇒ S * a rule 6 S ⇒ S + S * a rule 1 S ⇒ a + S * a rule 6 S ⇒ a + a * a rule 6 S S * S S + S a a a

Parse trees

S S + S a S * S a a S S * S S + S a a a Same string, 2 derivations

Ambiguity

 A CFG is said to be ambiguous if there

is at least 1 string in L(G) having two or more distinct derivations.

 Some grammars are inherently

ambiguous.

Ambiguity

 Showing a grammar is ambiguous is

easy

 Find a string x in the L(G) that has two

derivations

 Showing a particular grammar is not

ambiguous is usually difficult.

 Showing that any grammar is not

ambiguous is not possible.

slide-8
SLIDE 8

8

Derivations

 Leftmost derivations

 A leftmost derivation is one where the leftmost

variable in the current string is always the first to get replaced via a production rule.

 A rightmost derivation is one where the rightmost

variable in the current string is always the first to get replaced via a production rule.

Derivations

S S + S a S + S a a S S + S S + S a a a rightmost derivation leftmost derivation a + a + a

Ambiguity

 As it turns out (we won’t prove this)

 In unambiguous grammars,leftmost

derivations will always be unique.

 In unambiguous grammars,rightmost

derivations will always be unique.

Removing ambiguities

 Some languages are inherently ambiguous

 This cannot always be done

 In fact,

 We can/will show there is no “algorithm” for determining if a

CFG is ambiguous

 However,

 On a case by case basis, ambiguities can be eliminated

Example

 Abbreviated grammar for algebraic

expressions – Production rules

 S → S + S (1)

S → S * S (2) S → (S) (3) S → a (4)

Example

This grammar has two problems

1.

Precedence of operators is not respected

a * a + a should be interpreted as (a*a) + a

2.

Sequence of identical operators can be grouped either from the left or the right

a + a + a can be interpreted as either (a+a)+a

  • r a + (a + a)
slide-9
SLIDE 9

9

Example

 Solution

 Introduce some new variables

 Factor – expression that cannot be broken up by either *

  • r +

 a  (S)  Term – expression that cannot be broken up by +  All Factors  T * F  Expression – all possible expression  All Terms  S + T

Example

 Our new grammar

 S → S + T | T  T → T * F | F  F → (S) | a

 Note that

 all recursion is leftmost  * has higher precedent than +  a + a + a + a * a is interpreted as

 ((a+a) + a) + (a*a)

Example

S S + T S + T S + T a T * F F a a F F a a

a + a + a + a * a

Example

 It can be shown

 That every string x, that is generated by

this new grammar, has only one leftmost derivation

 As such this new grammar is unambiguous  Done using induction on the |x|.

Reality Check

 Context Free Grammars (CFG)

 Context Free Languages are generated

from CFGs

 Parse Trees and Derviation  Ambiguity

About Ambiguity

 Ambiguity

 A grammar is ambiguous if there is a string

generated by the grammar that has two distinct derivations.

 Some languages are inherently ambiguous

 All grammars that generate the language are ambiguous

 There is no algorithm to determine if any given

grammar is ambiguous

 Proving a grammar to be ambiguous is easy  Proving that a grammar is not is hard.

slide-10
SLIDE 10

10

Questions?

 Break.