How to find a language generated by a given grammar

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Language
Click For Summary
SUMMARY

This discussion focuses on determining the language generated by a specific context-free grammar defined as {w ∈ {(,) [,]}^{*}: I → (I)I | [I] I | ∅}. Participants emphasize that there is no general method for identifying the language; instead, they recommend systematically drawing derivations of increasing lengths. Key insights include that applying the rule I → ∅ results in an empty string when no other rules are applied, and that balanced strings can be generated by ensuring equal numbers of opening and closing delimiters. The discussion concludes with the clarification that generating the string "()" requires three rule applications.

PREREQUISITES
  • Understanding of context-free grammars and their rules
  • Familiarity with the concepts of derivation and string generation
  • Knowledge of balanced parentheses and bracket notation
  • Ability to analyze formal language properties
NEXT STEPS
  • Study the properties of context-free languages and their grammars
  • Learn about the Chomsky hierarchy and its implications for language generation
  • Explore techniques for proving language properties, such as induction
  • Investigate parsing algorithms for context-free grammars, such as CYK or LL parsing
USEFUL FOR

Students of formal languages, computer scientists working with compilers, and anyone interested in the theoretical aspects of grammar and language generation.

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 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 10 ·
Replies
10
Views
7K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 397 ·
14
Replies
397
Views
20K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 26 ·
Replies
26
Views
2K