AIM:Program to recognize the grammar (anb, n>= 10).

DESCRIPTION:

The program has three parts:definition section,rule section,subroutine section. The program uses a variable count to keep track of the number of A's in the input string and B is accepted only once according to the rule described.So count must be more than 9.

ALGORITHM:

CODE:

%{
#include
#include
int count=0;
%}
%token A B;
%%
S: X B
X: X A {count++;}
 |
 ;
%% 
yyerror()
{
    printf("Invalid\n");
    exit(0);
}
main()
{
    printf("enter a string of a's and b's\n");
    yyparse();
    if(count>=10)
    printf("valid grammar\n");
    else
    printf("Invalid grammar\n");
}

LEX SPECIFICATION:

%{
#include"y.tab.h"
%}
%%
a return A;
b return B;
. return yytext[0];
\n return 0;
%%

OUTPUT:

1) enter a string of a's and b's

aaaaaaaaaab

valid grammar

2) enter a string of a's and b's

aaaaaab

invalid grammar