Convert the grammar into Chomsky Form

In summary: Could you explain me how you filled the matrix? I got stuck right...Sure!So the idea is to go through the expression from left to right and match it with the rules.For each rule that matches, we write it down in the matrix and move the cursor one step further in the expression.We continue until we have matched the whole expression or until we cannot match any more rules.Here is the matrix again, with some additional annotations: Rule Recognized $S_0 \to ST$$S_0 \to a$ $|ST$$a| \qquad$ FAILStart symbolThis is the symbol that we start with and that we are trying to match the expression to. In
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:
I am asked to convert the following grammar into Chomsky Form
$$\Sigma=\{a,b,c,+\}, \Sigma_Q=\{S,V\},I=S$$
$$S -> S+S|V$$
$$V -> a|b|c$$

My idea is the following:
$$S_0 \rightarrow S$$
$$S \rightarrow ST|V$$
$$T \rightarrow +S$$
$$V \rightarrow A|B|C$$
$$A \rightarrow a$$
$$B \rightarrow b$$
$$C \rightarrow c$$

Could you tell me if my idea is right?
 
Technology news on Phys.org
  • #2
mathmari said:
Hey! :eek:
I am asked to convert the following grammar into Chomsky Form
$$\Sigma=\{a,b,c,+\}, \Sigma_Q=\{S,V\},I=S$$
$$S -> S+S|V$$
$$V -> a|b|c$$

My idea is the following:
$$S_0 \rightarrow S$$
$$S \rightarrow ST|V$$
$$T \rightarrow +S$$
$$V \rightarrow A|B|C$$
$$A \rightarrow a$$
$$B \rightarrow b$$
$$C \rightarrow c$$

Could you tell me if my idea is right?

Looks like you are going in the right direction. :)
Introducing $S_0$ makes sure the start symbol does not appear on the right of any rule.
Replace $+S$ by $T$ brings the number of variables on the RHS down to 2, as they should.

However, you still have unit rules, like $S \to V$, which are not supposed to be there.
And you also have a mixed form, $T \to +S$, which I don't think you should have.
 
  • #3
I like Serena said:
However, you still have unit rules, like $S \to V$, which are not supposed to be there.
And you also have a mixed form, $T \to +S$, which I don't think you should have.

How can I change this?
 
  • #4
mathmari said:
How can I change this?

In a rule like $S \to V$, replace $V$ with its possibilities.
In the rule $T \to +S$, introduce a new variable that maps to $+$.
 
  • #5
I like Serena said:
In a rule like $S \to V$, replace $V$ with its possibilities.
In the rule $T \to +S$, introduce a new variable that maps to $+$.

Do you mean the following?

$$S_0 \rightarrow S$$
$$S \rightarrow ST$$
$$T \rightarrow PS$$
$$P \rightarrow +$$
$$S \rightarrow a$$
$$S \rightarrow b$$
$$S \rightarrow c$$
 
  • #6
mathmari said:
Do you mean the following?

$$S_0 \rightarrow S$$
$$S \rightarrow ST$$
$$T \rightarrow PS$$
$$P \rightarrow +$$
$$S \rightarrow a$$
$$S \rightarrow b$$
$$S \rightarrow c$$

Yep!
There you go - Chomsky normal form.
 
  • #7
I like Serena said:
Yep!
There you go - Chomsky normal form.

Great! Thanks a lot for your help!
 
  • #8
And if I want to analyze the expression $$a+b+c$$ based on the Chomsky normal form, how can I do this?
 
  • #9
mathmari said:
And if I want to analyze the expression $$a+b+c$$ based on the Chomsky normal form, how can I do this?

Let's try to walk through your expression from left to right.

We begin with $a$.
There is only 1 rule that has $a$ in its RHS ($S \to a$), so obviously that one needs to be applied.
That leaves us with $S$. There is only 1 rule that begins with $S$ in its RHS: $S \to ST$.

Summing it up, we get:

RuleRecognized
$S \to a$$a$
$S \to S|T$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
......

The vertical line $|$ marks the point to where we have matched the expression.

Perhaps you can continue the analysis...
 
  • #10
I like Serena said:
Let's try to walk through your expression from left to right.

We begin with $a$.
There is only 1 rule that has $a$ in its RHS ($S \to a$), so obviously that one needs to be applied.
That leaves us with $S$. There is only 1 rule that begins with $S$ in its RHS: $S \to ST$.

Summing it up, we get:

RuleRecognized
$S \to a$$a$
$S \to S|T$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
......

The vertical line $|$ marks the point to where we have matched the expression.

Perhaps you can continue the analysis...

I tried to continue and this is what I found:

RuleRecognized
$S \to a$$a$
$S \to S|T$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
$S \to b$$a+b$
$S \to S|T$$a+b|T$
$T \to PS$$a+b|PS$
$P \to +$$a+b+|S$
$S \to c$$a+b+c$

Could you tell me if it is correct?
 
  • #11
mathmari said:
I tried to continue and this is what I found:

RuleRecognized
$S \to a$$a$
$S \to S|T$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
$S \to b$$a+b$ FAILED due to ending prematuraly
$S \to S|T$ No S to expand$a+b|T$
$T \to PS$$a+b|PS$
$P \to +$$a+b+|S$
$S \to c$$a+b+c$

Could you tell me if it is correct?

There is a problem that I've marked in RED.
At that point there are 2 rules that can be applied.
So you need to apply them simultaneously until one of the 2 paths fails, which happens to be right after.
The same "problem" occurs when recognizing $c$ at the end.

EDIT: Also fixed the first couple of steps.

Properly, it is:

RuleRecognized
$S_0 \to ST$
$S_0 \to a$
$|ST$
$a| \qquad$ FAIL
$S \to a$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
$S \to b$
$S \to ST$
$a+b| \qquad$ FAIL
$a+|ST \quad$ only other possibility
$S \to b$$a+b|T$
$T \to PS$$a+b|PS$
$P \to +$$a+b+|S$
$S \to c$
$S \to ST$
$a+b+c| \qquad$ SUCCESS :D
$a+b+|ST \quad$ will fail after 2 more steps
 
  • #12
I like Serena said:
RuleRecognized
$S_0 \to ST$
$S_0 \to a$
$|ST$
$a| \qquad$ FAIL
$S \to a$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
$S \to b$
$S \to ST$
$a+b| \qquad$ FAIL
$a+|ST \quad$ only other possibility
$S \to b$$a+b|T$
$T \to PS$$a+b|PS$
$P \to +$$a+b+|S$
$S \to c$
$S \to ST$
$a+b+c| \qquad$ SUCCESS :D
$a+b+|ST \quad$ will fail after 2 more steps
Could you explain me how you filled the matrix? I got stuck right now..
 
  • #13
mathmari said:
Could you explain me how you filled the matrix? I got stuck right now..

How much did you understand?
Or more to the point, where are you stuck?
 
  • #14
I like Serena said:
How much did you understand?
Or more to the point, where are you stuck?

From all the possible rules for $S$, we take $S \to a$, since the expression we want to analyze is $a+b+c$, right?
So, by the rule $S \to ST$ we have $S \to aT$
Then we replace the rule of $T$, so $S \to aPS$ $\Rightarrow$ $S \to a+S$.

We continue by replacing S with the next symbol of the expression, that is $b$ $(S \to bT)$.
So, $S \to a+bT \Rightarrow S \to a+bPS \Rightarrow S \to a+b+S$.

And then we do the same for $c$.
$S \to a+b+c $.

Could you tell me if I understood the procedure of the analysis right?
 
  • #15
mathmari said:
From all the possible rules for $S$, we take $S \to a$, since the expression we want to analyze is $a+b+c$, right?
So, by the rule $S \to ST$ we have $S \to aT$
Then we replace the rule of $T$, so $S \to aPS$ $\Rightarrow$ $S \to a+S$.

We continue by replacing S with the next symbol of the expression, that is $b$ $(S \to bT)$.
So, $S \to a+bT \Rightarrow S \to a+bPS \Rightarrow S \to a+b+S$.

And then we do the same for $c$.
$S \to a+b+c $.

Could you tell me if I understood the procedure of the analysis right?

Looks like a sound analysis. :cool:

Here's an slightly alternative view.

We have to start with $S_0$, the start symbol.
(I didn't do that at first, which was a mistake on my part. :eek:)

There is only 1 rule that applies: $S_0 \to S$.
After that we are left with $S$.
From there we have 4 options:
  1. $S \to ST$
  2. $S \to a$
  3. $S \to b$
  4. $S \to c$
Since we're supposed to read $a+b+c$, only the first 2 rules might apply.
So let's keep track of both of them.

If we try $S \to ST$, we are again confronted with the same choice for $S$. Again only the first 2 choices apply. We'll continue with that later.

If we try $S \to a$, no other rules can be applied afterward.
Since there is still more to read, this does not work and we have to mark this as a failure.

So we stick with $S \to ST$ and see where we can go from there...
 
  • #16
Wait a second!
$S_0 \to S$ is not a valid Chomsky Normal Form rule, since they should always be either of the form $A \to BC$ or the form $A \to a$.
It should be replaced by $S_0 \to ST\ |\ a\ |\ b\ |\ c$.

Anyway, I'm glad that the rest of the arguments are still the same.

Sorry for being so obtuse. :eek:
 
  • #17
I like Serena said:
$S_0 \to S$ is not a valid Chomsky Normal Form rule, since they should always be either of the form $A \to BC$ or the form $A \to a$.
It should be replaced by $S_0 \to ST\ |\ a\ |\ b\ |\ c$.

Do you mean that the Chomsky normal form of the grammar is the following?
$$S_0 \to ST | a | b | c$$
$$T \to PS$$
$$P \to +$$
 
  • #18
mathmari said:
Do you mean that the Chomsky normal form of the grammar is the following?
$$S_0 \to ST | a | b | c$$
$$T \to PS$$
$$P \to +$$

Almost. You also need a rule for $S$.
 
  • #19
I like Serena said:
Almost. You also need a rule for $S$.

Oh yes...But what rule is left for $S$ since we have written the rule $S \to a|b|c$ at the first rule? Can I maybe replace $S$ with $S_0$ ?
 
  • #20
mathmari said:
Oh yes...But what rule is left for $S$? Can I maybe replace $S$ with $S_0$ ?

The rule for $S$ is the same as the one for $S_0$. :)
Still, they have to be distinct, since CNF requires that the start symbol does not appear on the right hand side of a rule.
 
  • #21
I like Serena said:
The rule for $S$ is the same as the one for $S_0$. :)
Still, they have to be distinct, since CNF requires that the start symbol does not appear on the right hand side of a rule.

Ok.. But what rule is then for $S$ ?

The only idea I have right now is:
$$S_0 \to ST$$
$$T \to PS$$
$$P \to +$$
$$S \to a|b|c$$
 
  • #22
mathmari said:
Ok.. But what rule is then for $S$ ?

The only idea I have right now is:
$$S_0 \to ST$$
$$T \to PS$$
$$P \to +$$
$$S \to a|b|c$$

Let's pick:
$$S_0 \to ST|a|b|c$$
$$S \to ST|a|b|c$$
$$T \to PS$$
$$P \to +$$

Yes, it may seem silly, but this fulfills the requirements of CNF.
 
  • #23
I like Serena said:
Let's pick:
$$S_0 \to ST|a|b|c$$
$$S \to ST|a|b|c$$
$$T \to PS$$
$$P \to +$$

Yes, it may seem silly, but this fulfills the requirements of CNF.

So are the following forms not in CNF?
$$S_{0} \to ST $$
$$S \to ST $$
$$T \to PS $$
$$P \to +$$
$$S \to a|b|c$$

or

$$S_{0} \to ST $$
$$T \to PS $$
$$P \to +$$
$$S \to a|b|c$$
 
  • #24
mathmari said:
So are the following forms not in CNF?
$$S_{0} \to ST $$
$$S \to ST $$
$$T \to PS $$
$$P \to +$$
$$S \to a|b|c$$

or

$$S_{0} \to ST $$
$$T \to PS $$
$$P \to +$$
$$S \to a|b|c$$

They are in CNF.
Any reason to think otherwise?
 
  • #25
I like Serena said:
They are in CNF.
Ok!

I like Serena said:
Any reason to think otherwise?

No,I wanted to know if I can write your version like that. :eek:
 
Last edited by a moderator:
  • #26
mathmari said:
Ok!

No,I wanted to know if I can write your version like that. :eek:

Well... they are in CNF, but they represent different languages. :eek:
 
  • #27
I like Serena said:
Well... they are in CNF, but they represent different languages. :eek:

Ok.. Thanks! :p
 
  • #28
And something else... :eek: Is the following analysis a syntax analysis?
I like Serena said:
RuleRecognized
$S_0 \to ST$
$S_0 \to a$
$|ST$
$a| \qquad$ FAIL
$S \to a$$a|T$
$T \to PS$$a|PS$
$P \to +$$a+|S$
$S \to b$
$S \to ST$
$a+b| \qquad$ FAIL
$a+|ST \quad$ only other possibility
$S \to b$$a+b|T$
$T \to PS$$a+b|PS$
$P \to +$$a+b+|S$
$S \to c$
$S \to ST$
$a+b+c| \qquad$ SUCCESS :D
$a+b+|ST \quad$ will fail after 2 more steps
 
  • #29
mathmari said:
And something else... :eek: Is the following analysis a syntax analysis?

Yes.
 
  • #30
I like Serena said:
Yes.

Ok..Thanks a lot! :eek:
 

Related to Convert the grammar into Chomsky Form

1. What is Chomsky Form?

Chomsky Form, also known as Chomsky Normal Form, is a specific form of grammar used in formal language theory. It was developed by linguist Noam Chomsky in the 1950s and is used to simplify the rules of a grammar.

2. Why is it important to convert a grammar into Chomsky Form?

Converting a grammar into Chomsky Form allows for easier analysis and manipulation of the grammar. It also helps in determining the complexity and properties of the language generated by the grammar.

3. How do you convert a grammar into Chomsky Form?

To convert a grammar into Chomsky Form, you must first remove all epsilon productions (productions that generate the empty string) and unit productions (productions with only one non-terminal symbol). Then, you must replace all remaining productions with two non-terminal symbols on the right-hand side with new non-terminal symbols. Finally, you must replace all terminals with new non-terminal symbols and add new productions to generate the original terminals.

4. Can any grammar be converted into Chomsky Form?

Yes, any context-free grammar can be converted into Chomsky Form. However, some grammars may require additional steps or may result in a larger number of productions in Chomsky Form.

5. What are the benefits of converting a grammar into Chomsky Form?

Converting a grammar into Chomsky Form can help in determining the complexity and properties of the language generated by the grammar. It also allows for easier analysis and manipulation of the grammar, making it useful in natural language processing and other areas of computer science.

Similar threads

  • Special and General Relativity
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
13
Views
549
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
911
  • Programming and Computer Science
Replies
4
Views
2K
  • Beyond the Standard Models
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
20
Views
349
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top