Context-Free Grammars 2IT70 Finite Automata and Process Theory - - PowerPoint PPT Presentation

context free grammars 2it70 finite automata and process
SMART_READER_LITE
LIVE PREVIEW

Context-Free Grammars 2IT70 Finite Automata and Process Theory - - PowerPoint PPT Presentation

Context-Free Grammars 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 21, 2014 Generating strings language L 1 = { a n b n n > 0 } language L 2 = ( 01 ) ab L 1 L 2 if w L 1 then awb L


slide-1
SLIDE 1

Context-Free Grammars 2IT70 Finite Automata and Process Theory

Technische Universiteit Eindhoven May 21, 2014

slide-2
SLIDE 2

Generating strings

language L1 = {anbn ∣ n > 0} ab ∈ L1 if w ∈ L1 then awb ∈ L1 production rules S → ab and S → aSb language L2 = (01)∗ ε ∈ L2 if w ∈ L2 then 01w ∈ L2 production rules S → ε and S → 01S

2 IT70 (2014) Context-Free Grammars 2 / 13

slide-3
SLIDE 3

Variables, terminals, production rules, start symbol

palindromes over {a,b} S → ε S → a S → b S → aSa S → bSb alternative notation S → ε ∣ a ∣ b ∣ aSa ∣ bSb binary integer expressions E → I E → N E → E + E E → E ∗ E E → (E) I → a I → I0 I → I1 N → 1 N → N0 N → N1 alternative notation E → I ∣ N ∣ E + E ∣ E ∗ E ∣ (E) I → a ∣ I0 ∣ I1 N → 1 ∣ N0 ∣ N1

2 IT70 (2014) Context-Free Grammars 3 / 13

slide-4
SLIDE 4

Clicker questions L81

Consider again the grammar given by

E → I ∣ N ∣ E + E ∣ E ∗ E ∣ (E) I → a ∣ I0 ∣ I1 N → 1 ∣ N0 ∣ N1

How many of the strings aa1, a01, 011, 11a, a01 + a01, a11 ∗ a10, +101, (110) cannot be generated by the grammar, you expect?

  • A. Two strings
  • B. Three strings
  • C. Four strings
  • D. Six strings
  • E. Can’t tell

2 IT70 (2014) Context-Free Grammars 4 / 13

slide-5
SLIDE 5

Language of a CFG

context-free grammar G = (V , T, R, S ) V variables and T terminals R ⊆ V ×(V ∪ T)∗ production rules A → α S ∈ V start symbol productions ⇒G ⊆ (V ∪T) × (V ∪T) γ ⇒G γ′ if γ = β1Aβ2, A → α rule of G, γ′ = β1αβ2 production sequences γ0 ⇒G γ1 ⇒G ⋯ ⇒G γn language of a variable LG(A) = {w ∈ T ∗ ∣ A ⇒

∗ G w }

language of the grammar L(G) = LG(S)

2 IT70 (2014) Context-Free Grammars 5 / 13

slide-6
SLIDE 6

More examples

⟨expression⟩ ∶∶= ⟨term⟩ ∣ ⟨expression⟩+ ⟨term⟩ ⟨term⟩ ∶∶= ⟨factor⟩ ∣ ⟨term⟩∗ ⟨factor⟩ ⟨factor⟩ ∶∶= ⟨identifier⟩ ∣ (⟨expression⟩) ⟨identifier⟩ ∶∶= a ∣ b ∣ c ∣ ... ⟨char⟩ ∶∶= a ∣ ... ∣ z ∣ A ∣ ... ∣ Z ∣ ... ⟨text⟩ ∶∶= ε ∣ ⟨char⟩⟨text⟩ ⟨doc⟩ ∶∶= ε ∣ ⟨element⟩⟨doc⟩ ⟨element⟩ ∶∶= ⟨text⟩ ∣ <EM>⟨doc⟩</EM> ∣ <P>⟨doc⟩ ∣ <OL>⟨list⟩</OL> ⟨listitem⟩ ∶∶= <LI>⟨doc⟩ ⟨list⟩ ∶∶= ε ∣ ⟨listitem⟩⟨doc⟩

2 IT70 (2014) Context-Free Grammars 6 / 13

slide-7
SLIDE 7

Combining and splitting productions

lemma CFG G = (V , T, R, S ) if X1 ⇒n1

G γ1,... ,Xk ⇒nk G γk then

X1⋯Xk ⇒n

G γ1⋯γk

where n = n1 + ⋯ + nk if X1⋯Xk ⇒n

G γ then

X1 ⇒n1

G γ1,... ,Xk ⇒nk G γk

where n = n1 + ⋯ + nk and γ = γ1 ... γk

X1,... ,Xk ∈ (V ∪ T)

2 IT70 (2014) Context-Free Grammars 7 / 13

slide-8
SLIDE 8

The parentheses language L()

CFG S → ε ∣ SS ∣ (S) several production sequences for string ()(())

S ⇒G SS ⇒G S(S) ⇒G S((S)) ⇒G S(()) ⇒G (S)(()) ⇒G ()(()) S ⇒G SS ⇒G (S)S ⇒G ()S ⇒G ()(S) ⇒G ()((S)) ⇒G ()(()) S ⇒G SS ⇒G (S)S ⇒G (S)(S) ⇒G ()(S) ⇒G ()((S)) ⇒G ()(())

leftmost, rightmost, mixed production sequence

2 IT70 (2014) Context-Free Grammars 8 / 13

slide-9
SLIDE 9

Clicker question L82

Given the CFG S → () ∣ (S) ∣ SS. How many production sequences are there for the string (())((()))?

  • A. ()(()) has 5 possible production sequences
  • B. ()(()) has 6 possible production sequences
  • C. ()(()) has 10 possible production sequences
  • D. ()(()) has 12 possible production sequences
  • E. Can’t tell

2 IT70 (2014) Context-Free Grammars 9 / 13

slide-10
SLIDE 10

Proving a grammar correct

CFG G with production rules S → ab and S → aSb for L = {anbn ∣ n ⩾ 1} it holds that L(G) = L proof induction on n: if S ⇒n

G w then w ∈ L, thus L(G) ⊆ L

induction on n: if w = anbn then w ∈ L(G), thus L ⊆ L(G)

2 IT70 (2014) Context-Free Grammars 10 / 13

slide-11
SLIDE 11

Avoiding the inductive proofs

lemma CFGs G1 = (V1, T1, R1, S1 ) and G2 = (V2, T2, R2, S2 ) moreover V1 and V2 disjoint define CFG G = ({S} ∪ V1 ∪ V2, T1 ∪ T2, R, S ) if R = {S → S1 ∣ S2} ∪ R1 ∪ R2 then L(G) = L(G1) ∪ L(G2) if R = {S → S1S2} ∪ R1 ∪ R2 then L(G) = L(G1) ⋅ L(G2) if R = {S → ε ∣ S1S} ∪ R1 then L(G) = L(G1)∗

2 IT70 (2014) Context-Free Grammars 11 / 13

slide-12
SLIDE 12

Avoiding the inductive proofs (cont.)

CFG G with production rules S → S1 ∣ S2 S1 → aB B → ε ∣ bB S2 → bA A → ε ∣ aA then L(G) = {abn, bam ∣ n,m ⩾ 0} proof use the lemma LG(A) = {am ∣ m ⩾ 0} and LG(B) = {bn ∣ n ⩾ 0} LG(S1) = {a} ⋅ {bn ∣ n ⩾ 0} and LG(S2) = {b} ⋅ {am ∣ m ⩾ 0} L(G) = {abn ∣ n ⩾ 0} ∪ {bam ∣ m ⩾ 0}

2 IT70 (2014) Context-Free Grammars 12 / 13

slide-13
SLIDE 13

Context-free languages

language L is context-free if L = L(G) for CFG G {anbn ∣ n ⩾ 0} and {w wR ∣ w ∈ {0,1}∗ } are context-free theorem if L is regular then L is context-free proof for DFA D = (Q, Σ, δ, q0, F ) put G = (Q, Σ, R, q0 ) where R = {q → aq′ ∣ δ(q,a) = q′ } ∪ {q → ε ∣ q ∈ F } then L = L(G)

2 IT70 (2014) Context-Free Grammars 13 / 13