CC Manual - Grammar PDF
CC Manual - Grammar PDF
Name of Student:………………………………………………………
PRN:………. …
Course Teacher: Sharayu Bonde
Date of Performance: Date of completion:
Experiment No. B: 04
Theory:
A context-free grammar (CFG) is a set of recursive rewriting rules (or productions) used to generate
patterns of strings.
a set of terminal symbols, which are the characters of the alphabet that appear in the strings
generated by the grammar.
a set of nonterminal symbols, which are placeholders for patterns of terminal symbols that can be
generated by the nonterminal symbols.
a set of productions, which are rules for replacing (or rewriting) nonterminal symbols (on the left
side of the production) in a string with other nonterminal or terminal symbols (on the right side of
the production).
a start symbol, which is a special nonterminal symbol that appears in the initial string generated
by the grammar.
Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple (N,
T, P, S) where
N is a set of non-terminal symbols.
T is a set of terminals where N ∩ T = NULL.
P is a set of rules, P: N → (N ∪ T)*, i.e., the left-hand side of the production rule P does have any
right context or left context.
S is the start symbol.
Example
The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε
Generation of Derivation Tree
A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information
a string derived from a context-free grammar.
Representation Technique
Root vertex − Must be labeled by the start symbol.
Vertex − Labeled by a non-terminal symbol.
Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows −