You are on page 1of 10

CONTEXT FREE

GRAMMARS

Bachelor of Technology
Computer Science and Engineering

Submitted By

RAISA FARHIN (13000118012)


SEC- A, 2nd YEAR

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

 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 | ε

A CFG for Arithmetic Expressions

An example grammar that generates strings representing arithmetic expressions with the four
operators +, -, *, /, and numbers as operands is:

1. <expression> --> number


2. <expression> --> ( <expression> )
3. <expression> --> <expression> + <expression>
4. <expression> --> <expression> - <expression>
5. <expression> --> <expression> * <expression>
6. <expression> --> <expression> / <expression>

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:

I. What are Context Free Grammars?

 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

II. What does CFG do?

 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

IV. Production rule notation


 A production rule in R is formalized mathematically as a pair (a,b) , where a is a non-terminal
and b is a string of variables and non-terminals; rather than using ordered pair notation,
production rules are usually written using an arrow operator with a as its left hand side and b
as its right hand side: a à b.
 It is allowed for b to be the empty string, and in this case, it is customary to denote it by ε. The
form a à ε is called an ε- production.
V. Context-Free Languages

VI. Example: Well-formed parentheses


 The canonical example of a context free grammar is parenthesis matching, which is
representative of the general case. There are two terminal symbols "(" and ")" and one non
terminal symbol S. The production rules are

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

 A parse tree of a derivation is a tree in which:

 Each internal node is labeled with a nonterminal

 If a rule A <> A1A2…An occurs in the derivation then A is a parent node of nodes labeled A1,
A2, …, An

VIII. Leftmost, Rightmost Derivations


 A left-most derivation of a sentential form is one in which rules transforming the left-most
nonterminal are always applied
 A right-most derivation of a sentential form is one in which rules transforming the right-most
nonterminal are always applied
IX. Ambiguous Grammar

 A grammar G is ambiguous if there is a word w


 L(G) having are least two different parse trees

 Notice that a has at least two left-most derivations

X. Ambiguity & Disambiguation


 Given an ambiguous grammar, would like an equivalent unambiguous grammar.
 Allows you to know more about structure of a given derivation.
 Simplifies inductive proofs on derivations.
 Can lead to more efficient parsing algorithms.
 In programming languages, want to impose a canonical structure on derivations. E.g., for 1+2X3.
 Strategy: Force an ordering on all derivations.
XI. CFG Simplification

Can’t always eliminate ambiguity.


But, CFG simplification & restriction still useful theoretically & pragmatically.
• Simpler grammars are easier to understand.
• Simpler grammars can lead to faster parsing.
• Restricted forms useful for some parsing algorithms.
Restricted forms can give you more knowledge about derivations.
3. References:
 https://www.cs.rochester.edu/~nelson/courses/csc_173/grammars/cfg.html
 https://www.tutorialspoint.com/automata_theory/context_free_grammar_introduction.htm
 https://en.wikipedia.org/wiki/Context-free_grammar
 https://www.geeksforgeeks.org/classification-of-context-free-grammars/
 https://www.wisdomjobs.com/e-university/automata-theory-tutorial-352/context-free-grammar-
introduction-25743.html
 https://www.javatpoint.com/context-free-grammar

You might also like