7

I am trying to solve for my exam coming up and have no clue how to generate the grammar for Context sensitive languages for example how do i proceed on this kind of question.

Give a context-sensitive (not just length-increasing) grammar for $\{www : w ∈ \{a, b\}^⋆\}$.

Ideas or approaches on how to deal with this kind of questions is much appreciated.

Raphael
  • 73,212
  • 30
  • 182
  • 400
User_1234
  • 145
  • 3
  • 6

3 Answers3

5

Intuitively you want to generate three intermediate symbols at a time, and allow the symbols to sort themselves. Let $S$ be the start symbol. The generation rules are:

$$S \rightarrow S A_1 A_2 A_3$$ $$S \rightarrow S B_1 B_2 B_3$$ The sorting rules are as follows: For symbols $X_i,Y_i \in \{A_1,A_2,A_3,B_1,B_2,B_3\}$ such that $j < i$ add the rule: $$X_i Y_j \rightarrow Y_jX_i$$ In the end we have to turn the intermediate symbols into terminal symbols. Turn the start symbol into another symbol $S_1$ to end the generation phase. $$S \rightarrow S_1$$ Push $S_1$ through the string converting symbols to terminal symbols on the way and increment the subscript when done with one word $w$. For all $i \in \{1,2,3\}$ add the rules: $$S_i A_i \rightarrow a S_i \; | \; a S_{i+1}$$ $$S_i B_i \rightarrow b S_i \; | \; b S_{i+1}$$ The subscript of $S$ ensures that the symbols are converted in the proper order as the subscript can not decrease. In the end remove $S_3$. $$ S_3 \rightarrow \varepsilon $$

jnalanko
  • 597
  • 5
  • 10
2

The answer for your specific example is there, but the general question remains.

In very many years, in both theoretical and applied computer science, I do not recall having to write a CS grammar as such.

Still, if I may give an advice, it should not be seen at all like producing a CF grammar, were you must contrive the rules to get everything in place and coordinated exactly right as it is produced.

CS grammars are a lot more algorithmic, and you can pretty much mimic a Turing machine (working in finite space, proportional to input size, which means Linear Bounded Automaton - LBA) in order to move things around as needed. So it is much more a programming exercise.

You can pretty much generates the first ingredients to build one of the words in the language, then move them around algorithmically. You can use special symbols (possibly in various flavors corresponding to finite state) to act as heads that you move around with appropriate rules, do as to check what is to be done. And so on.

A good reading may be to look at the proof of equivalence between CSG languages and LBA languages, i.e. the CS languages.

Remember that nearly all algorithms that we work with can be performed by a LBA, hence correspond to a CSG definable language. That should give you an idea of the available algorithmic power.

But a bit of imagination helps elegant solutions, as in the example you gave.

babou
  • 19,645
  • 43
  • 77
1

this is just an additional answer that can be thought of as a general solution. CSLs, context sensitive languages are modelled by LBAs, linear bounded automata. a LBA is a Turing machine that can accept or reject given a work tape that is no larger than a constant times the size of the input tape. so if you can figure out a computer program that can process the input in constant space, then its a CSL. idea: a program that would work for this problem would do something like enumerate permutations in constant space.

vzn
  • 11,162
  • 1
  • 28
  • 52