0

I've a problem. I have to write right linear context free grammar with alphapet={0,1} where the numbers of 0 will be even and the numbers od 1 will be odd. I tried write sth. but it's doesn't work.

s --> [1],a.
s --> [0],b.

a --> [].
a --> [1],c.
a --> [0],b.

c --> [1],k.
c --> [0],b.

b --> [0],k.
b --> [1],d.

d --> [1],b.
d --> [0],c.

k --> [].
k --> s.

Grammar should accept even amount of 0s and odd amount of 1s. Grammar context free is right linear when A->wB or A->w where w is any word under our alphabet and A,B is no terminals

false
  • 10,264
  • 13
  • 101
  • 209
Vous
  • 67
  • 5

2 Answers2

1

How about

s --> [1],oddOnesEvenZeros.
s --> [0],oddZerosEvenOnes.

oddOnesEvenZeros--> [].
oddOnesEvenZeros--> [1],s.
oddOnesEvenZeros--> [0],oddZerosOddOnes.

oddZerosEvenOnes--> [1],oddZerosOddOnes.
oddZerosEvenOnes--> [0],s.

oddZerosOddOnes --> [1],oddZerosEvenOnes.
oddZerosOddOnes --> [0],oddOnesEvenZeros.

The grammar is regular because you don't have to remember the parts you have already passed, you can only remember current state of each, i.e. four different states, from which one (odd ones, even zeros) is accepting. As a regular grammar, it is right linear CFG as well.

Tomas Pastircak
  • 2,867
  • 16
  • 28
  • Ok but it's work similar to my code. For s([0,0,1,0,0],[]). it's false :( I think it should work for this case – Vous Apr 06 '14 at 17:26
  • Sorry I had an error there on the last line, the nonterminal on the right side should be oddOnesEvenZeros – Tomas Pastircak Apr 06 '14 at 18:26
  • @TomasPastircak +1 for explaining why the grammar is regular. I think you are missing the corner case `[]` which has an even number of `0`s (since `0` is an even number). I have a slightly different grammar, which looks ahead to the next number using pushback lists. – Wouter Beek Apr 06 '14 at 18:38
  • He says that amount of 0s should be even and amount of 1s should be odd - that means e.g. 1, 100, 010, 111,11100 etc. If I understand that incorrectly, please correct me. – Tomas Pastircak Apr 06 '14 at 18:56
  • Thank you for help:) You are my master. I will need help with one more problem. Similar to this one where I have to write right linear context free grammar difference between amount of 0s and 1s is even. I will wirte about this in other Ask. – Vous Apr 06 '14 at 19:37
  • @TomasPastircak Since `0` is an even number, the empty list is an even number of anything. Thus `[]` is also an even number of `0`s. – Wouter Beek Apr 07 '14 at 07:07
  • @WouterBeek Yup, but it's not odd number of ones :) – Tomas Pastircak Apr 07 '14 at 07:20
  • @TomasPastircak You are indeed right! Sorry for misunderstanding your point. – Wouter Beek Apr 07 '14 at 07:55
0

Maybe something like this?

s --> [].
s --> even_zeros, s.
s --> odd_ones, s.

even_zeros([0,0], []).
even_zeros, [X] --> [0,0,X], {X \== 0}.
even_zeros --> [0,0], even_zeros.

odd_ones([1], []).
odd_ones, [X] --> [1,X], {X \== 1}.
odd_ones --> [1,1], odd_ones.

I've interpreted the question as asking for a grammar of sequences of 0s and 1s, where the number of consecutive 0s is always even and the number of consecutive 1s is always odd.

Wouter Beek
  • 3,307
  • 16
  • 29