0% found this document useful (0 votes)
37 views29 pages

Automata Ch3

The document discusses context-free languages and context-free grammars. It provides examples of context-free grammars that generate specific languages. It also covers derivation, which is the process of applying production rules to generate strings. Derivation can be leftmost or rightmost. Derivation trees provide a graphical representation of derivations. The document notes that ambiguity occurs when a grammar allows multiple derivations or parse trees for a single string.
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)
37 views29 pages

Automata Ch3

The document discusses context-free languages and context-free grammars. It provides examples of context-free grammars that generate specific languages. It also covers derivation, which is the process of applying production rules to generate strings. Derivation can be leftmost or rightmost. Derivation trees provide a graphical representation of derivations. The document notes that ambiguity occurs when a grammar allows multiple derivations or parse trees for a single string.
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/ 29

Automata and Complexity

Theory
Chapter three (1)
Context Free Languages and Context-free Grammar(CFG)

02/02/2024 For third year CS student by Gezahiegn.T In 2022 1


Context Free Languages
In formal language theory, a context-free language is a language
generated by a context-free grammar.

Context-free languages have many applications in programming


languages, in particular, most arithmetic expressions are generated by
context-free grammars.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 2


Context-Free Grammar (CFG)
• CFG stands for context-free grammar.
• It is a formal grammar which is used to generate all possible patterns of
strings in a given formal language.
• Context-free grammar G can be defined by four tuples as:
G = (V, T, P, S)
• Where,
• G is the grammar, which consists of a set of the production rule. It is used to
generate the string of a language.
• T is the finite set of a terminal symbol. It is denoted by lower case letters.
• V is the finite set of a non-terminal symbol. It is denoted by capital letters.
02/02/2024 For third year CS student by Gezahiegn.T In 2022 3
Continue..
• P is a set of production rules, which is used for replacing non-
terminals symbols(on the left side of the production) in a string with
other terminal or non-terminal symbols(on the right side of the
production).

• S is the start symbol which is used to derive the string. We can derive
the string by repeatedly replacing a non-terminal by the right-hand
side of the production until all non-terminal have been replaced by
terminal symbols.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 4


Example 1:

• Construct the CFG for the language having any number of a's over the
set ∑= {a}.
• Solution:
As we know the regular expression for the above language is
r.e. = a*
• Production rule for the Regular expression is as follows:
• S → aS rule 1
• S → ε rule 2

02/02/2024 For third year CS student by Gezahiegn.T In 2022 5


Continue..
Now if we want to derive a string "aaaaaa", we can start with start symbols.
• S
• aS
• aaS rule 1
• aaaS rule 1
• aaaaS rule 1
• aaaaaS rule 1
• aaaaaaS rule 1
• aaaaaaε rule 2
• aaaaaa
02/02/2024 For third year CS student by Gezahiegn.T In 2022 6
Example 2:
• Construct a CFG for a language L = {wcwR | where w € (a, b)*}.
• Solution:
The string that can be generated for a given language is {aacaa, bcb,
abcba, bacab, abbcbba, ....}
• The grammar could be:
• S → aSa rule 1
• S → bSb rule 2
•S→c rule 3

02/02/2024 For third year CS student by Gezahiegn.T In 2022 7


Continue..
Now if we want to derive a string "abbcbba", we can start with start
symbols.

• S → aSa
• S → abSba from rule 2
• S → abbSbba from rule 2
• S → abbcbba from rule 3

02/02/2024 For third year CS student by Gezahiegn.T In 2022 8


Example 3:
• Construct a CFG for the language L = anb2n where n>=1.
• Solution:
• The string that can be generated for a given language is {abb, aabbbb,
aaabbbbbb....}.
• The grammar could be:
S → aSbb | abb
• Now if we want to derive a string "aabbbb", we can start with start
symbols.
S → aSbb
S → aabbbb
02/02/2024 For third year CS student by Gezahiegn.T In 2022 9
Derivation
• Derivation is a sequence of production rules. It is used to get the input
string through these production rules. During parsing, we have to take
two decisions. These are as follows:

 We have to decide the non-terminal which is to be replaced.


 We have to decide the production rule by which the non-
terminal will be replaced.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 10


We have two options to decide which non-terminal to be placed
with production rule.

1. Leftmost Derivation:
• In the leftmost derivation, the input is scanned and replaced with the
production rule from left to right. So in leftmost derivation, we read
the input string from left to right.
• Example:
• Production rules: and Input a - b + a
E -> E + E
E -> E - E
E -> a | b
02/02/2024 For third year CS student by Gezahiegn.T In 2022 11
Continue..
• The leftmost derivation is:
•E=E+E
•E=E-E+E
•E=a-E+E
• E=a-b+E
• E=a-b+a

02/02/2024 For third year CS student by Gezahiegn.T In 2022 12


Continue..
2. Rightmost Derivation: In rightmost derivation, the input is scanned
and replaced with the production rule from right to left. So in rightmost
derivation, we read the input string from right to left.
• Example
• Production rules: and Input a - b + a
E=E+E
E=E-E
E=a|b

02/02/2024 For third year CS student by Gezahiegn.T In 2022 13


Continue..
• The rightmost derivation is:
•E=E-E
•E=E-E+E
•E=E-E+a
• E=E-b+a
• E=a-b+a
• When we use the leftmost derivation or rightmost derivation, we may
get the same string

02/02/2024 For third year CS student by Gezahiegn.T In 2022 14


Example 1:
• Derive the string "abb" for leftmost derivation and rightmost
derivation using a CFG given by
S → AB | ε rightmost derivation

A → aB
B → Sb
• Solution:
• Leftmost derivation:

02/02/2024 For third year CS student by Gezahiegn.T In 2022 15


Example 2:
• Derive the string "aabbabba" for leftmost derivation and rightmost derivation using a CFG given by,
S → aB | bA
S → a | aS | bAA
S → b | aS | aBB
• Leftmost derivation:
• S
• aB S → aB
• aaBB B → aBB
• aabB B→b
• aabbS B → bS
• aabbaB S → aB
• aabbabS B → bS
• aabbabbA S → bA
• aabbabba A → a
02/02/2024 For third year CS student by Gezahiegn.T In 2022 16
Continue..
• Rightmost derivation:
• S
• aB S → aB
• aaBB B → aBB
• aaBbS B → bS
• aaBbbA S → bA
• aaBbba A→a
• aabSbba B → bS
• aabbAbba S → bA
• aabbabba A → a
02/02/2024 For third year CS student by Gezahiegn.T In 2022 17
Example 3:
• Derive the string "00101" for leftmost derivation and rightmost derivation using a CFG given by,
S → A1B
A → 0A | ε
B → 0B | 1B | ε
• Solution:
• Leftmost derivation:
• S
• A1B
• 0A1B
• 00A1B
• 001B
• 0010B
• 00101B
• 00101
02/02/2024 For third year CS student by Gezahiegn.T In 2022 18
Continue..
• Rightmost derivation:
•S
• A1B
• A10B
• A101B
• A101
• 0A101
• 00A101
• 00101
02/02/2024 For third year CS student by Gezahiegn.T In 2022 19
Derivation Tree
• Derivation tree is a graphical representation for the derivation of the
given production rules for a given CFG.
• It is the simple way to show how the derivation can be done to obtain
some string from a given set of production rules.
• The derivation tree is also called a parse tree.
• Parse tree follows the precedence of operators.
• The deepest sub-tree traversed first.
• So, the operator in the parent node has less precedence over the
operator in the sub-tree.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 20


A parse tree contains the following properties:

The root node is always a node indicating start symbols.

The derivation is read from left to right.

The leaf node is always terminal nodes.

The interior nodes are always the non-terminal nodes.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 21


Example 1:
• Production rules:
E -> E + E
E -> E * E
E -> a | b | c
With Input a * b + c

02/02/2024 For third year CS student by Gezahiegn.T In 2022 22


Example 2:
• Construct a derivation tree for the string aabbabba for the CFG given
by,
S → aB | bA solution
A → a | aS | bAA
B → b | bS | aBB

02/02/2024 For third year CS student by Gezahiegn.T In 2022 23


Example 4:
• Show the derivation tree for string "aabbbb" with the following
grammar.
S → AB | ε
A → aB
B → Sb

02/02/2024 For third year CS student by Gezahiegn.T In 2022 24


Ambiguity in Grammar
• A grammar is said to be ambiguous if there exists more than one
leftmost derivation or more than one rightmost derivation or more than
one parse tree for the given input string. If the grammar is not
ambiguous, then it is called unambiguous.
• If the grammar has ambiguity, then it is not good for compiler
construction. No method can automatically detect and remove the
ambiguity, but we can remove ambiguity by re-writing the whole
grammar without ambiguity.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 25


Example 1:
• Let us consider a grammar G with the production rule
E→I sol:
E→E+E
E→E*E
E → (E)
I → ε | 0 | 1 | 2 | ... | 9
For the string "3 * 2 + 5", the above grammar can generate two parse
trees by leftmost derivation:

02/02/2024 For third year CS student by Gezahiegn.T In 2022 26


Example 2:
• Check whether the given grammar G is ambiguous or not.
E→E+E
E→E-E
E → id
From the above grammar String "id + id - id" can be derived in 2 ways:
• First Leftmost derivation
• E→E+E
• → id + E
• → id + E - E
• → id + id - E
• → id + id- id
02/02/2024 For third year CS student by Gezahiegn.T In 2022 27
Continue..
• Second Leftmost derivation
• E→E-E
• →E+E-E
• → id + E - E
• → id + id - E
• → id + id - id
• Since there are two leftmost derivation for a single string "id + id - id",
the grammar G is ambiguous.

02/02/2024 For third year CS student by Gezahiegn.T In 2022 28


Simplification of CFG
We will see next

02/02/2024 For third year CS student by Gezahiegn.T In 2022 29

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