%{ /* acfg.y */ #include #include %} %start S %token 'a' %token 'b' %% /* beginning of rules section (printf statements optional) */ S : 'a' A S { printf("S -> aAS \n"); } | 'a' { printf("S -> a \n"); } ; A : S 'b' A { printf("A -> SbA \n"); } | S S { printf("A -> SS a \n"); } | 'b' 'a' { printf("A -> ba \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; } /* FYI in CMSC 451 equivalent grammar P83 G = (V, T, P, S) V = { A, S } T= { a, b } S = S P is or S -> aAS S -> aAS | a S -> a A -> SbA A -> SbA | SS | ba A -> SS A -> ba */