MHB How to find a language generated by a given grammar

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Language
AI Thread Summary
To determine the language generated by a given grammar, one effective approach is to systematically explore all possible derivations of increasing lengths. For the grammar provided, the start symbol I can be replaced according to the rules, leading to various combinations of parentheses and brackets. The discussion emphasizes that when no rules are applied, the result is the start symbol I itself, and applying the rule I → ∅ yields an empty string.As derivations progress, participants clarify that applying rules generates strings containing balanced parentheses and brackets. They note that valid strings must maintain equal numbers of opening and closing delimiters, ensuring they are balanced. The conversation also touches on the specifics of how many rule applications are needed to achieve certain strings, such as "()", which requires three applications. Ultimately, the key takeaway is that the generated language consists of strings with balanced delimiters, and participants encourage exploring further derivations to identify patterns and properties of the language.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
If I am given a grammar,how can I find which language it generates?For example,if I am given the following grammar:
{w \epsilon {(,) [,]}^{*}:I\rightarrow(I)I | <i> I | \varnothing \}</i> ,where I is the start symbol, how can I find the language?
 
Last edited by a moderator:
Technology news on Phys.org
Re: Find the context-free grammar,that produces a language

evinda said:
If I am given a grammar,how can I find which language it generates?
There is no general way. Try systematically drawing all possible derivations of length 0, 1, 2, ... until it is feasible. Looking at these examples may help you formulate a hypothesis about the language and later prove it. Try doing this for the language you gave.
 
Re: Find the context-free grammar,that produces a language

Evgeny.Makarov said:
There is no general way. Try systematically drawing all possible derivations of length 0, 1, 2, ... until it is feasible. Looking at these examples may help you formulate a hypothesis about the language and later prove it. Try doing this for the language you gave.

Isn't it like that:
-when length=0,the word is \varnothing
-when length=1, it is either () or []
-when length=2,it is ()[]
Or am I wrong? :confused:
 
Re: Find the context-free grammar,that produces a language

The answer depends on how to count length. I'll assume it is the number of times any rule was applied.
evinda said:
Isn't it like that:
-when length=0,the word is \varnothing
If no rule was applied, we still have $I$.

evinda said:
-when length=1, it is either () or []
If you apply a rule just once and get a strings consisting of terminal symbols, it must be empty (the rule $I\to\varnothing$).

evinda said:
-when length=2,it is ()[]
Why do you think so?
 
Re: Find the context-free grammar,that produces a language

Evgeny.Makarov said:
The answer depends on how to count length. I'll assume it is the number of times any rule was applied.
If no rule was applied, we still have $I$.

If you apply a rule just once and get a strings consisting of terminal symbols, it must be empty (the rule $I\to\varnothing$).

Why do you think so?

Could you explain me why if we apply a rule just once, it must be empty (the rule $I\to\varnothing$) ? I haven't understood it :confused:

Also,how can I check,which is the word when we apply the rule 2 times?? :eek:
 
Re: Find the context-free grammar,that produces a language

evinda said:
Could you explain me why if we apply a rule just once, it must be empty (the rule $I\to\varnothing$) ?
This is true under the condition that the resulting string consists of terminal symbols. Why don't you try applying other rules just once to $I$ and see if you get a string consisting of all terminals?

evinda said:
Also,how can I check,which is the word when we apply the rule 2 times??
Look, there is a limit to what can be explained. I assume you know the definition of a grammar and how it generates words, i.e., how rules work. What kind of explanation do you need for this question? You check it by taking $I$ and then replacing it by the right-hand side of some rule. (Here all rules have left-hand side $I$, so they all apply.) For example, you get $(I)I$. Then again you pick one of the two $I$'s and replace it by the right-hand side of some rule. Please don't ask me to explain how to find $I$ in the string $(I)I$ and what it means to replace one substring by another.

Or are you confused by the form in which the rules are listed, using the vertical bar? The line
\[
I\rightarrow(I)I \mid I \mid \varnothing
\]
is an abbreviated way to write these three rules.
\[
\begin{aligned}
I&\rightarrow (I)I\\
I&\rightarrow I\\
I&\rightarrow \varnothing
\end{aligned}
\]
 
Re: Find the context-free grammar,that produces a language

Evgeny.Makarov said:
This is true under the condition that the resulting string consists of terminal symbols. Why don't you try applying other rules just once to $I$ and see if you get a string consisting of all terminals?

Look, there is a limit to what can be explained. I assume you know the definition of a grammar and how it generates words, i.e., how rules work. What kind of explanation do you need for this question? You check it by taking $I$ and then replacing it by the right-hand side of some rule. (Here all rules have left-hand side $I$, so they all apply.) For example, you get $(I)I$. Then again you pick one of the two $I$'s and replace it by the right-hand side of some rule. Please don't ask me to explain how to find $I$ in the string $(I)I$ and what it means to replace one substring by another.

Or are you confused by the form in which the rules are listed, using the vertical bar? The line
\[
I\rightarrow(I)I \mid I \mid \varnothing
\]
is an abbreviated way to write these three rules.
\[
\begin{aligned}
I&\rightarrow (I)I\\
I&\rightarrow I\\
I&\rightarrow \varnothing
\end{aligned}
\]


When we apply the rules,are some results these:[ ] , ( ) , [ ( ) ] , ( [ ] ) , [ ] ( ) , ( ) [ ] , ( [ ] ) ( ) [ ] ? Or am I wrong? :confused:

- - - Updated - - -

evinda said:
When we apply the rules,are some results these:[ ] , ( ) , [ ( ) ] , ( [ ] ) , [ ] ( ) , ( ) [ ] , ( [ ] ) ( ) [ ] ? Or am I wrong? :confused:

And if we apply the rule 2 times,is the result () or [] ? :eek:
 
Re: Find the context-free grammar,that produces a language

evinda said:
When we apply the rules,are some results these:[ ] , ( ) , [ ( ) ] , ( [ ] ) , [ ] ( ) , ( ) [ ] , ( [ ] ) ( ) [ ] ?
This is much better. Yes, it is correct. We can proceed to start guessing the common property that these words satisfy. Both $I\to(I)I$ and $I\toI$ introduce opening and closing delimiters in pairs, so by induction every generated word has the same number of opening and closing parentheses, and the same for square brackets. But it seems that more is true: delimiters in every generated string are balanced. For example, in "())(" the number of opening and closing parentheses is the same, but parentheses are not balanced. Formally this means that every prefix of a balanced string has at least as many opening parentheses as closing ones. In contrast, the prefix "())" of "())(" has 1 opening parenthesis and two closing ones.

evinda said:
And if we apply the rule 2 times,is the result () or [] ? :eek:
The string "()" requires three rule applications:
\[
I\to (I)I\to()I\to()
\]
 
Last edited:
Re: Find the context-free grammar,that produces a language

Evgeny.Makarov said:
This is much better. Yes, it is correct. We can proceed to start guessing the common property that these words satisfy. Both $I\to(I)I$ and $I\toI$ introduce opening and closing delimiters in pairs, so by induction every generated word has the same number of opening and closing parentheses, and the same for square brackets. But it seems that more is true: delimiters in every generated string are balanced. For example, in "())(" the number of opening and closing parentheses is the same, but parentheses are not balanced. Formally this means that every prefix of a balanced string has at least as many opening parentheses as closing ones. In contrast, the prefix "())" of "())(" has 1 opening parenthesis and twp closing ones.


I understand! :)

Evgeny.Makarov said:
The string "()" requires three rule applications:
\[
I\to (I)I\to()I\to()
\]

So,applying two rules,is the result: I\to (I)I\to()I ??Or not?
 

Similar threads

Replies
6
Views
1K
Replies
2
Views
5K
Replies
10
Views
7K
Replies
14
Views
3K
Replies
26
Views
2K
Back
Top