0% found this document useful (0 votes)
10 views35 pages

CFG_Lec1

context free grammar

Uploaded by

Zari Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views35 pages

CFG_Lec1

context free grammar

Uploaded by

Zari Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Theory of Automata

Context Free Grammar


A context-free grammar (CFG) is a set of
recursive rewriting rules (or productions)
used to generate patterns of strings.

A context-free grammar (CFG) is a formal


grammar in which every production rule is of
the form
V→w
CFG Components
A CFG consists of the following components:
 a set of terminal symbols, which are the
characters of the alphabet that appear in
the strings generated by the grammar.

 a set of non-terminal symbols, which are


placeholders for patterns of terminal
symbols that can be generated by the non-
terminal symbols.
CFG Components…
A CFG consists of the following components:
 a set of productions, which are rules for
replacing (or rewriting) non-terminal
symbols (on the left side of the production) in
a string with other non-terminal or terminal
symbols (on the right side of the production).

 a start symbol, which is a special non-


terminal symbol that appears in the initial
string generated by the grammar.
CFG Components
String Generation from CFG
To generate a string of terminal symbols
from a CFG, we

 Begin with a string consisting of the start


symbol

 Apply one of the productions with the


start symbol on the left hand size,
replacing the start symbol with the right
hand side of the production
String Generation from CFG…
Repeat the process of selecting non-
terminal symbols in the string, and
replacing them with the right hand side
of some corresponding production, until
all non-terminals have been replaced by
terminal symbols.
Arithmetic Expression
Production Rules
Production Rules
Backus Normal Form
Definitions
Definition of Equal
Production Rules
Simple String Generation
Production Rules

Generate aaaa String from given


CFG
Parse Tree
Generate baaabbab from language of
EQUAL equal
Parse Tree of EQUAL
Definition
The language generated by CFG
Consists of those strings which can
be produced from the start symbol
S using the production Rules

The language generated by CFG is


called Context Free Language(CFL)
Ambiguity
A CFG is said to be ambiguous if
there is at least 1 string in L(G)
having two or more distinct
derivations
Ambiguity
Arithmetic Expression Example
Arithmetic Expression Example

4+2*3
So Ambiguity is
A CFG is ambiguous if there is
some word it generates which
has two different parse trees

A CFG that is not ambiguous is


called unambiguous
Arithmetic Expressions E - Expression
T - Term
F - Factor

E is expression, T is term of
expression and F is factor of a term
Derivation of 4+2*3 using AE Productions

Derivation

E - Expression
T - Term
F - Factor
E is expression, T is term of
expression and F is factor of a term
Simple Example
Suppose CFG is

(1) S→S+S
(2) S→1
(3) S→a

Generate the string from the given CFG


1 + 1 + a
Derivation
S → S+S (rule 1 on first S)
→ S+S+S (rule 1 on second)
→ S+1+S (rule 2 on second
S)
→ S+1+a (rule 3 on third S)
→ 1+1+a (rule 2 on first S)
Types of Derivations
Leftmost derivation, it is always the leftmost
nonterminal

Rightmost derivation, it is always the rightmost


nonterminal
Left Most Derivation
S → S+S (rule 1 on first S)
→ 1+S (rule 2 on first S)
→ 1+S+S (rule 1 on first S)
→ 1+1+S (rule 2 on first
S)
→ 1+1+a (rule 3 on first
S)
Left Most Derivation
Right Most Derivation
S→S+S (Rule-1)
→S+a (Rule-3)
→S+S+a (Rule-1)
→S+1+a (Rule-2)
→1+1+a (Rule-2)
Right Most Derivation
Parse Tree from given input string
Parse Tree

Grammar

E - Expression
T - Term
F - Factor
E is expression, T is term of
expression and F is factor of a term
Parse Tree from given input string
Language Input String:
:

Grammar Parse Tree

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy