%{ /* acfg2.y */ #include #include %} %start S %token 'a' %token 'b' %% /* beginning of rules section */ S : 'a' B { printf("S -> aB \n"); } | 'b' A { printf("S -> Ba \n"); } ; A : 'a' { printf("A -> a \n"); } | 'a' S { printf("A -> aS \n"); } | 'b' A A { printf("A -> bAA \n"); } ; B : 'b' { printf("B -> b \n"); } | 'b' S { printf("B -> bS \n"); } | 'a' B B { printf("B -> aBB \n"); } ; %% /* start of program, need to provide yylex that delivers tokens */ yylex() { int c; c=getchar(); printf("read %c \n",c); /* can be commented out */ if(!islower(c)) c=0; /* cause parser to stop */ yylval=c; return c; } /* G = ( V, T, P, S ) V = { A, B, S } T = { a, b } S = S P is S -> aB | bA A -> a | aS | bAA B -> b | bS | aBB any string with equal number of a's and b's */