Professional Documents
Culture Documents
Context Free Grammars: Bachelor of Technology Computer Science and Engineering
Context Free Grammars: Bachelor of Technology Computer Science and Engineering
GRAMMARS
Bachelor of Technology
Computer Science and Engineering
Submitted By
MAY 2019
Techno India
EM-4/1, Sector-V, Salt Lake
Kolkata- 700091
West Bengal
India
TABLE OF CONTENTS
1. Introduction
2. Body
i. What are Context Free Grammars?
ii. What does CFG do?
iii. Formal Definition of CFG
iv. Production rule notation
v. Context-Free Languages
vi. Example: Well-formed parentheses
vii. Parse Tree
viii. Leftmost, Rightmost Derivations
ix. Ambiguous Grammar
x. Ambiguity & Disambiguation
xi. CFG Simplification
3. References
1. INTRODUCTION:
Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules is a
quadruple (N, T, P, S) where
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.
Example
The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε
An example grammar that generates strings representing arithmetic expressions with the four
operators +, -, *, /, and numbers as operands is:
The only nonterminal symbol in this grammar is <expression>, which is also the start symbol.
The terminal symbols are {+,-,*,/,(,),number}. (We will interpret "number" to represent any valid
number.)
2. Body:
In Formal Language Theory, a Context free Grammar (CFG) is a formal grammar in which
every production rule is of the form
V w
Where V is a single nonterminal symbol and w is a string of terminals and/or non terminals
(w can be empty)
The languages generated by context free grammars are knows as the context free languages
A CFG provides a simple and mathematically precise mechanism for describing the methods
by which phrases in some natural language are built from smaller blocks, capturing the “block
structure” of sentences in a natural way.
Important features of natural language syntax such as agreement and reference are not the part
of context free grammar, but the basic recursive structure of sentences, the way in which
clauses nest inside other clauses, and the way in which list of adjectives and adverbs are
swallowed by nouns and verbs is described exactly.
III. Formal Definition of CFG
S→S
SS → (S)
S → ()
The first rule allows Ss to multiply; the second rule allows Ss to become enclosed by matching
parentheses; and the third rule terminates the recursion.
VII. Parse Tree
If a rule A <> A1A2…An occurs in the derivation then A is a parent node of nodes labeled A1,
A2, …, An