/* program to check whether the given string is a part of the language a^n b or not */
File Name : pr12.y
%{
#include<stdio.h>
int yylex(void);
int yyerror(char *);
%}
%token A B //tokens : the alphabets of language 'a' and 'b'
%%
//production rules for grammar
expr: s B
;
s : s A
| A
;
%%
int main()
{
printf("Enter the string \n");
yyparse();
return 0;
}
int yyerror(char *s)
{
printf("Invalid: Not a part of the language - a^n b \n");
}
File Name : pr12.l
%{
#include "y.tab.h"
extern int yylval;
%}
A [a]
B [b]
%%
{A} {yylval=yytext[0];return A;}
{B} {yylval=yytext[1];return B;}
\n {return 0;}
. {return yytext[0];}
%%
Output:
No comments:
Post a Comment