0% found this document useful (0 votes)
35 views12 pages

CFGs and Its Derivation Tree

Cfg PDF

Uploaded by

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

CFGs and Its Derivation Tree

Cfg PDF

Uploaded by

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

CFG (Context-Free Grammar)

What is a Context-Free Grammar?


A Context-Free Grammar is a set of rules that describe how to generate
strings of a language. It's called "context-free" because the rules for
generating strings depend only on the current symbol being processed,
not on the surrounding context.

Key Components of a CFG


A CFG consists of four main components:

1. Terminals: These are the basic symbols of the language, such as


keywords, identifiers, and punctuation.
2. Non-Terminals: These are symbols that represent a set of terminals
or other non-terminals. Non-terminals are used to define the structure
of the language.
3. Production Rules: These are the rules that describe how to generate
strings of the language. Production rules are written in the form A → β,
where A is a non-terminal and β is a string of terminals and/or non-
terminals.
4. Start Symbol: This is a designated non-terminal symbol that serves as
the starting point for generating strings.
How CFGs Work
Here's a step-by-step overview of how CFGs work:

1. Start with the Start Symbol: Begin with the start symbol, which
represents the starting point for generating strings.
2. Apply Production Rules: Apply the production rules to the current
symbol, replacing it with the corresponding string of terminals and/or
non-terminals.
3. Repeat Step 2: Continue applying production rules until a string of
terminals is generated.
4. Resulting String: The resulting string is a valid sentence in the
language defined by the CFG.

# Example CFG
Consider a simple CFG that generates strings of binary numbers:

S → AB
A → 0A | 1A | ε
B → 0B | 1B | ε
In this example, S is the start symbol, A and B are non-terminals, and 0
and 1 are terminals. The production rules define how to generate
strings of binary numbers.

Applications of CFGs
CFGs have numerous applications in:

1. Compiler Design: CFGs are used to define the syntax of programming


languages.
2. Natural Language Processing: CFGs are used to model the grammar
of natural languages.
3. Data Compression: CFGs can be used to compress data by
representing it as a set of production rules.

In conclusion, Context-Free Grammars provide a powerful framework


for describing the structure of languages. They play a crucial role in the
design and implementation of programming languages, natural
language processing, and compiler design.

Derivation of Context-Free Grammars (CFGs)

Derivation is the process of generating a string of terminals from a CFG


by applying the production rules. Here's a step-by-step guide to
deriving a string from a CFG:
Derivation Steps
1. Start with the Start Symbol: Begin with the start symbol (S) of the
CFG.
2. Apply a Production Rule: Choose a production rule that has the
current symbol (S) on its left-hand side. Replace the current symbol
with the right-hand side of the production rule.
3. Repeat Step 2: Continue applying production rules until a string of
terminals is generated.
4. Resulting String: The resulting string is a valid sentence in the
language defined by the CFG.

Types of Derivations
There are two types of derivations:

1. Leftmost Derivation: In a leftmost derivation, the leftmost non-


terminal symbol is replaced at each step.
2. Rightmost Derivation: In a rightmost derivation, the rightmost non-
terminal symbol is replaced at each step.

Both leftmost and rightmost derivations can be used to derive strings


from a CFG. However, leftmost derivations are more commonly used.
Examples of Derivation :
The production rules for the derivation tree are as follows −

E=E+E
E=E*E
E=a|b|c

Here, let the input be a*b+c

Solution :

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:

1. E = E + E
2. E = E - E
3. E = a | b
Input

1. a - b + a
The leftmost derivation is:

1. E = E + E
2. E = E-E+E
3. E = a-E+E
4. E = a-b+E
5. E = a-b+a
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:

1. E = E + E
2. E = E - E
3. E = a | b
Input

1. a - b + a
The rightmost derivation is:

1. E = E - E
2. E=E-E+E
3. E=E-E+a
4. E=E-b+a
5. E=a-b+a
When we use the leftmost derivation or rightmost derivation, we may get the
same string. This type of derivation does not affect on getting of a string.

Example 2:
Derive the string "abb" for leftmost derivation and rightmost derivation using
a CFG given by,

1. S → AB | ε
2. A → aB
3. B → Sb
Solution:

Leftmost derivation:

Rightmost derivation:

Example 3:
Derive the string "00101" for leftmost derivation and rightmost derivation
using a CFG given by,
1. S → A1B
2. A → 0A | ε
3. B → 0B | 1B | ε

Leftmost derivation:

1. S
2. A1B
3. 0A1B
4. 00A1B
5. 001B
6. 0010B
7. 00101B
8. 00101
Rightmost derivation:

1. S
2. A1B
3. A10B
4. A101B
5. A101
6. 0A101
7. 00A101
8. 00101

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.

A parse tree contains the following properties:

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


2. The derivation is read from left to right.

Example 1:
Production rules:

1. E = E + E
2. E = E * E
3. E = a | b | c
Input

1. a * b + c
Step 1:

Step 2:

Step 2:
Step 4:

Step 5:
Note: We can draw a derivation tree step by step or
directly in one step.
Example 2:
Draw a derivation tree for the string "bab" from the CFG given by

1. S → bSb | a | b
Solution:

Now, the derivation tree for the string "bbabb" is as follows:

The above tree is a derivation tree drawn for deriving a string bbabb. By
simply reading the leaf nodes, we can obtain the desired string. The same
tree can also be denoted by,
Example 4:
Show the derivation tree for string "aabbbb" with the following grammar.

1. S → AB | ε
2. A → aB
3. B → Sb
Solution:

To draw a tree we will first try to obtain derivation for the string aabbbb

Now, the derivation tree for the string "aabbbb" is as follows:


Ambiguity in Context-Free Grammar (CFG)
Ambiguity in CFG happens when a single string can be derived in more than one way, leading to
multiple parse trees or different interpretations. This creates confusion about the structure or
meaning of the string.

What is Ambiguity?
A CFG is ambiguous if:
A string can have two or more derivation trees.
The same string can have different meanings based on the grammar.
This makes the grammar unclear and problematic, especially for programming languages,
where each expression must have only one valid interpretation.

Why is Ambiguity a Problem?


In practical scenarios like programming, ambiguity can cause:

Misinterpretation: The compiler might interpret the same expression differently.


Undefined Behavior: It becomes unclear which operation should happen first.

example :
G:->Sa|aS|a
string to drive aaa

You might also like