2. Context-Free Languages
•Context-Free Languages (CFL) are described
using Context-Free Grammars (CFG).
•A CFG is a simple recursive method of
specifying grammar rules which can generate
strings in a language – these languages are the
CFL’s.
3. Context-Free Grammars
• The following is an example of a CFG, call it G1:
• A → 1A0 (A and B = variables)
• A → B
• B → # (0, 1 and # = terminals)
• A grammar consists of a collection of substitution rules
(projections).
• A is start variable in this case – usually occurs on left
hand side of topmost rule.
4. Use grammar to describe a language by
generating each string of language:
• Write down start variable.
• Find a variable and a rule which starts with that variable. Replace
written variable with the right hand side of this rule.
• Repeat the second step until no variables remain.
5. •All strings generated in this manner constitute
the language of the grammar.
•L(G1) = language of grammar G1.
•Can show that L(G1) is {1n
#0n
| n ≥ 0}.
•Any language that can be generated by some
context-free grammar is called a context-free
language.
6. Defining CFG
• Informally a CFG consists of:
• A set of replacement rules, each having a Left-
Hand Side (LHS) and a Right-Hand Side (RHS).
• Two types of symbols; variables and terminals.
• LHS of each rule is a single variable (no
terminals).
• RHS of each rule is a string of zero or more
variables and terminals.
• A string consists of only terminals.
7. Definition of a CFG
•Formally, a context-free grammar is a 4-tuple
(V, T , R, S ), where
• V are the variables (finite set)
• T are the terminal states (finite set)
• R is the set of rules
• S is the start variable, S V
8. Context-Free Grammars
• In grammar G1, V = {A, B}, Σ = {0, 1, #}, S = A, and R
is the collections of the rules:
• A → 1A0
• A → B
• B → #
• Consider G3 = ({S}, {a,b}, R, S). The set of rules R, is
• S → aSb | SS | ε
• This grammar generates strings such as ab, abab,
aababb and aaabbb.
9. Pushdown Automata (PDA)
•Pushdown Automata are similar to
nondeterministic finite automata but have an
extra element – stack.
•This stack provided extra memory space.
•Also allows pushdown automata to recognise
some nonregular languages.
10. Context Free Language
•A language is context free if and only if some
pushdown automata recognises it.
•Every regular language is recognised by a
finite automaton and every finite automaton
is automatically a pushdown automaton that
ignores the stack, we can note that every
regular language is also a context-free
language.
12. Closure Properties of
CFL
The context free languages are closed under
the following operations:
1.Union
2.Concatenation
3.Closure
4.Homomorphism
13. Derivation Tree
Definition: Let G = (V, T, P, S) be a CFG. A tree is a
derivation (or parse) tree if:
• Every vertex has a label from V union T union {ε}
• The label of the root is S
• If a vertex with label A has children with labels X1, X2,
…, Xn, from left to right, then
A –> X1, X2,…, Xn must be a production in P
• If a vertex has label ε, then that vertex is a leaf and
the only child of its’ parent
More Generally, a derivation tree can be defined with
any non-terminal as the root.
14.
15. Backus-Naur Form
• The backus-naur form is a convenient notation use
to represent CFG in an intuitive and more compact
manner.
• In BNF we use the following symbols:
< > := |
• Given a CFG (V, T , R, S ) a variable is enclosed in
< and >.A terminal is represented as itself and a
production is represented as
<V>:=symbols
16. •When describing languages,BNF is a formal
notation for encoding grammars.
•Many programming languages,protocols or
formats have a BNF description in their
specification.