%{
#include <stdio.h>

int yylex(void);
void yyerror(const char *s);
%}

%token A B C

%%

S : A S B
  | B S A
  | C
  ;

%%

int yylex()
{
    int ch;

    ch = getchar();

    if (ch == 'a')
        return A;
    if (ch == 'b')
        return B;
    if (ch == 'c')
        return C;

    return 0;
}

void yyerror(const char *s)
{
    printf("Invalid string\n");
}

int main()
{
    printf("Enter string: ");

    if (yyparse() == 0)
        printf("Valid string\n");

    return 0;
}
