Program in YACC to recognize the language (a^n b , n>=10). (output to say input is valid or not)

/* 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

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...