A formal system used to generate strings of a language using production rules. Denoted as CFG. Defined as a 4-tuple where:
- - finite set of variables (non-terminals)
- - finite set of terminal symbols
- - start symbol
- - finite set of productions
and are disjoint.
Production Rule
A production has the form where:
- (a non-terminal)
This means a non-terminal (regardless of the surrounding) can be replaced by a string of terminals and/or non-terminals.
Example:
Derivations
The process of generating strings from the start symbol using production rules.
Single-step derivation
Denoted as . Means is obtained from using a production.
Multi-step derivation
Denoted as . Means is derived from in zero or more steps.
Nullable Variable
A non-terminal symbol is a nullable variable iff there is a production .
FindNull Algorithm
- Start with , a set of variables directly producing
- Define
- Repeatedly calculate until
- is the set of all nullable non-terminals
Regular Grammar
A restricted CFG where productions have one of the forms:
where:
- are non-terminals
- is a terminal
CFG for a Regular Expression
Example regular language:
Equivalent CFG:
A → 011 | 1
B → AB | Λ
D → 01
C → DC | Λ
S → BC
CFG for a Finite Automata
For each transition P --a--> Q in the FA, add production . If state is an accepting state, add .