Using a language to define a grammar

87 Views Asked by At

I'm currently having trouble understanding how to use a language to generate a grammar.

Using the language:

$$L=\{a^n b^m | n, m \geq 1\}$$ as an example:

I know (from my notes) that this language creates the grammar:
Grammar: G
Terminals: $a, b$
Non-terminal: $S, T$
Start symbol: $S$
Productions: $S \rightarrow Sb, S \rightarrow Tb, T \rightarrow Ta, T \rightarrow a$

But, i'm unsure how the grammar was actually determined (especially the productions) in the way that they are. I think I might have missed some information when researching but i'm unsure of what.
Could someone please point me in the right direction, or give me a brief explanation.

Thanks,

UPDATE

Given the following language:

$$ L_2 = \{a^n b^{2n} c^n | 1 \leq n \leq 10\} $$

I can conclude that this is a regular language because n is bounded so i want to create a left-linear or right-linear grammar for the language

1

There are 1 best solutions below

3
On BEST ANSWER

It's the other way around, the grammar defines the language. If you start with the starting symbol $S$ and apply finitly many reductions on it, you can obtain strings over the alphabeth $\{a,b\}$. For example: $$ S \rightarrow Tb \rightarrow ab$$

This means that $ab \in L$ where $L$ is your language. Your language is nothing more than the set of strings that val be created with this grammar, and it's usually denoted $L(G)$ where $G$ is your grammar.

When you start from a language, and ask which grammar it is defined by, there can be many solutions, and there can be none.

To create for example a right linear grammar for the second language, you can use only these production rules: $$A \rightarrow cB$$ $$A \rightarrow c$$ $$A \rightarrow \epsilon$$

with $A, B$ nonterminals and $c$ a terminal. To make sure there is atleast one $a$ we can start with the rule $$S \rightarrow aA_1 $$

Now we can either go to $B$ or add an extra $a$ $$A_1 \rightarrow aA_2 | bB_1 $$ $$\vdots$$ As you can see I'm keeping track of the amount of letters used, this way I can make sure it's less than 10 for each letter.