Skip to main content

9. Context-Free Grammars

02/03/23

Chomsky type 3 = regular language - DFA, NFA, RE Chomsky type 2 = Content-free language(CFL) - Context-free grammars (CFGs), Push-down automata (PDA)

2(x+3)2*(x+3) Expressions E Terms (part of +) T Factor (part of *) F

Σ={a,,+,(,)}\Sigma = \{a,*,+,(,)\} ETE\to T EE+TE\to E+T TFT\to F TTFT \to T*F FaF\to a F(E)F\to (E)

Can be shorthanded using or (|) statements

E=>T=>IF=>FF=>aF=>a(E)=>a(E+T)=>a(I+T)=>a(F+T)=>a(a+T)=>a(a+F)=>a(a+a)E => T => I*F => F*F => a * F => a*(E) => a*(E+T) => a*(I+T) => a*(F+T) => a*(a+T) => a*(a+F) => a*(a+a)

What is a CFG?

CFGs are a formalism to define languages that is more general than regular expressions; there are more languages definable by CFGs than by regular expressions and finite automata.

G=(N,Σ,P,S)G = (N,\Sigma,P,S) NN a finite state f non-terminal symbols Σ\Sigma (or TT) a finite set of symbols (or terminal symbols) NΣ=N\cap \Sigma = \emptyset

PfinN×(N˙Σ)P \subseteq_{fin} N \times (N \dot\cup \Sigma)* - finite set of productions S:NS:N the start symbol

++\cup = not fresh

=>(N+Σ)×(N+Σ)=> \subseteq(N+\cup\Sigma)*\times(N+\cup\Sigma)*

???

Language of a grammar

L(G)={w:ΣS=>w}L(G) = \{w:\Sigma*|S=>*w\}

A CFL is a language given by CFG, L=L(G)L=L(G)

L={anbnn:N}L=\{a^nb^n|n:\mathbb{N}\}

Theorem - Every REG is also a CFL REG \subseteq CFL