How to find a language generated by a given grammar

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Language
In summary: Re: Find the context-free grammar,that produces a languageWhen we apply the rules, some results these:[ ], ( ) , [ ( ) ] , ( [ ] ) , [ ] ( ) , ( ) [...], etc.?
  • #1
evinda
Gold Member
MHB
3,836
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 [tex]\epsilon[/tex] {(,) [,]}[tex]^{*}[/tex]:[tex] I\rightarrow(I)I | I | \varnothing \}[/tex] ,where I is the start symbol, how can I find the language?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
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.
 
  • #3
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 [tex] \varnothing [/tex]
-when length=1, it is either () or []
-when length=2,it is ()[]
Or am I wrong? :confused:
 
  • #4
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 [tex] \varnothing [/tex]
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?
 
  • #5
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:
 
  • #6
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}
\]
 
  • #7
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:
 
  • #8
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:
  • #9
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: [tex] I\to (I)I\to()I [/tex] ??Or not?
 

FAQ: How to find a language generated by a given grammar

How can I determine if a given grammar generates a language?

To determine if a given grammar generates a language, you can use a process called "parsing". This involves analyzing the rules and symbols in the grammar to determine if it can generate strings of symbols that are part of a language. If the grammar can generate all possible strings of a language, then it is said to be "complete" and can generate the entire language.

What is the difference between regular and context-free grammars?

Regular grammars are more limited in their ability to generate languages compared to context-free grammars. Regular grammars can only generate languages that can be recognized by a finite-state automaton, while context-free grammars can generate more complex languages that require a pushdown automaton. Regular grammars are also more restrictive in terms of their rules and symbols, while context-free grammars can have more flexible and complex rules.

Can a grammar generate multiple languages?

Yes, a grammar can generate multiple languages. This is because a language can have different sets of rules and symbols that can generate different strings of symbols. As long as the grammar can generate all possible strings of a language, it can be considered as generating that language.

How do I know if a language is generated by a given grammar?

If you are given a language and a grammar, you can test if the grammar generates that language by using a process called "derivation". This involves starting with the start symbol of the grammar and applying its rules to generate strings of symbols. If the final string of symbols matches the given language, then the grammar is said to generate that language.

Can a grammar generate an infinite language?

Yes, a grammar can generate an infinite language. This is because a grammar can have recursive rules that can generate an infinite number of strings of symbols. As long as the grammar can generate all possible strings of the language, including those that are infinitely long, it can be considered as generating that language.

Similar threads

Replies
1
Views
1K
Replies
14
Views
4K
Replies
1
Views
1K
Replies
2
Views
4K
Replies
10
Views
7K
Replies
14
Views
3K
Replies
13
Views
4K
Back
Top