Can You Convert a Context-Free Grammar to a Deterministic One?

  • MHB
  • Thread starter evinda
  • Start date
In summary, the conversation is about converting a context-free grammar into a deterministic one. The original grammar includes rules for I, X, and Y, and the speaker has attempted to convert it by creating new rules for K and M. However, there is a concern about whether the new language allows for cdadd, which was not allowed in the original grammar.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Blush)

I have a context-free grammar,that I want to convert in a deterministic one.

This is the context-free grammar:

I -> cdaX|cdbY, X-> XXX |d , Y -> I | X

That's what I have tried:



I -> cdK , K ->aX | bY
X -> dM, M -> d M |N, N-> dN | ∅
Y ->cdK| dM

Is it right? Or have I done something wrong? (Thinking)
 
Last edited:
Technology news on Phys.org
  • #2
evinda said:
Hello! (Blush)

I have a context-free grammar,that I want to convert in a deterministic one.

This is the context-free grammar:

I -> cdaX|cdbY, X-> XXX |d , Y -> I | X

That's what I have tried:



I -> cdK , K ->aX | bY
X -> dM, M -> d M |N, N-> dN | ∅
Y ->cdK| dM

Is it right? Or have I done something wrong? (Thinking)

Hi!

It is almost right. (Worried)

Is for instance [m]cdadd[/m] allowed? (Wondering)
 
  • #3
I like Serena said:
Hi!

It is almost right. (Worried)

Is for instance [m]cdadd[/m] allowed? (Wondering)

Do you mean that it should be like that?

I -> cdK , K ->aX | bY
X -> dM, M -> d M | ∅
Y ->cdK| dM

?
(Thinking)
 
  • #4
evinda said:
Do you mean that it should be like that?

I -> cdK , K ->aX | bY
X -> dM, M -> d M | ∅
Y ->cdK| dM

?
(Thinking)

No... my point was that the original language does not allow [m]cdadd[/m], while your languages both do. (Doh)
 
  • #5


Hello there!

I can provide some feedback on your attempt to convert the context-free grammar to a deterministic one. Overall, your approach is on the right track, but there are a few things that could be improved.

First, it's important to note that a deterministic grammar should have only one production rule for each non-terminal symbol. In your conversion, you have multiple production rules for the non-terminal symbol X (dM and N). This could lead to ambiguity and make the grammar non-deterministic.

Additionally, I see that you have introduced a new non-terminal symbol K, which is not present in the original grammar. This is not necessary in a deterministic conversion and could potentially create more confusion.

In order to make your grammar more deterministic, I would suggest the following modifications:

I -> cdA | cdB | cdC
A -> dX | ∅
B -> dM | ∅
C -> dN | ∅
X -> X | ∅
M -> M | ∅
N -> N | ∅

This conversion ensures that each non-terminal symbol has only one production rule, and it eliminates the need for the extra non-terminal symbol K. I hope this helps. Keep up the good work!
 

Related to Can You Convert a Context-Free Grammar to a Deterministic One?

1. What is a CFG?

A CFG, or Context-Free Grammar, is a formal grammar used to generate strings in a language. It consists of a set of production rules that define how symbols can be combined to form valid strings.

2. Why is converting a CFG to a Deterministic Grammar important?

Converting a CFG to a Deterministic Grammar is important because it allows for efficient parsing and analysis of a language. Deterministic grammars have a unique leftmost derivation for every string, making it easier to determine the structure of a sentence and its meaning.

3. What is the difference between a CFG and a Deterministic Grammar?

The main difference between a CFG and a Deterministic Grammar is that CFGs allow for multiple leftmost derivations for a string, while Deterministic Grammars have a unique leftmost derivation for every string. This means that Deterministic Grammars are more structured and easier to parse.

4. How do you convert a CFG to a Deterministic Grammar?

To convert a CFG to a Deterministic Grammar, you can use a parsing algorithm, such as the LL(1) or LR(1) algorithm, to eliminate any ambiguity and create a unique leftmost derivation for every string. This may involve adding new production rules or modifying existing ones.

5. Are there any limitations to converting a CFG to a Deterministic Grammar?

Yes, there are some limitations to converting a CFG to a Deterministic Grammar. Not all CFGs can be converted, especially if they are inherently ambiguous or contain left recursion. In these cases, alternative methods, such as using a parser generator, may be necessary.

Similar threads

  • Introductory Physics Homework Help
Replies
13
Views
696
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
2
Views
4K
Replies
4
Views
820
  • Programming and Computer Science
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
15
Views
419
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
849
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
Back
Top