Professional Documents
Culture Documents
Formal Grammar
Formal Grammar
o Formal grammar is a set of rules. It is used to identify correct or incorrect strings of tokens in a
language. The formal grammar is represented as G.
o Formal grammar is used to generate all possible strings over the alphabet that is syntactically
correct in the language.
o Formal grammar is used mostly in the syntactic analysis phase (parsing) particularly during the
compilation.
Constitute Of Grammar :
Grammar is basically composed of two basic elements –
o Terminal Symbols –
Terminal symbols are those which are the components of the sentences generated using a
grammar and are represented using small case letter like a, b, c etc.
o Non-Terminal Symbols –
Non-Terminal Symbols are those symbols which take part in the generation of the sentence
but are not the component of the sentence. Non-Terminal Symbols are also called Auxiliary
Symbols and Variables. These symbols are represented using a capital letter like A, B, C, etc.
Example: L = {a, b}, N = {S, R, B}
Example
Grammar G1 −
({S, A, B}, {a, b}, S, {S → AB, A → a, B → b})
Here,
S, A, and B are Non-terminal symbols;
a and b are Terminal symbols
S is the Start symbol, S ∈ N
Productions, P : S → AB, A → a, B → b
Example
Grammar G2 − (({S, A}, {a, b}, S,{S → aAb, aA → aaAb, A → ε } )
Here,
S and A are Non-terminal symbols.
a and b are Terminal symbols.
ε is an empty string.
S is the Start symbol, S ∈ N
Production P : S → aAb, aA → aaAb, A → ε
Production rules:
S = bR
R = aR
R = aB
B = b
1. Through this production we can produce some strings like: bab, baab, baaab etc.