I am trying to do a simple lex&yacc program that can recognise the three sound of pikachu: pi, pika and pikachu. My only rule is that one token can not appear 3 times in a row in line. I have tried this:
%token PI PIKA PIKACHU
%%
program : program line '\n'
|
;
line: PI piWords
| PIKA pikaWords
| PIKACHU pikachuWords
;
piWords: PI
| PI pikaWords
| PI pikachuWords
;
pikaWords: PIKA
| PIKA piWords
| PIKA pikachuWords
;
pikachuWords: PIKACHU
| PIKACHU piWords
| PIKACHU pikaWords
;
%%
but this doesn't work on all combinations, for example Pika Pikachu pikachu. How could I rewrite this?
Also tried limiting the tokens in lex like {0,2} but when I write pi pi pi for example it still works even if it shouldn't.
UPDATE.
Based on ideas below, I did managed to finish this. I was pretty close, all I needed was to keep track is there was 2 repetitions also. I did smth like this:
program : program line '\n'
|
;
line: piWords
| pikaWords
| pikachuWords
;
piWords: PI
| PI pikaWords
| PI PI pikaWords
| PI pikachuWords
| PI PI pikachuWords
;
pikaWords: PIKA
| PIKA piWords
| PIKA PIKA piWords
| PIKA pikachuWords
| PIKA PIKA pikachuWords
;
pikachuWords: PIKACHU
| PIKACHU piWords
| PIKACHU PIKACHU piWords
| PIKACHU pikaWords
| PIKACHU PIKACHU pikaWords
;