File Name : pr12.y
/* program to check whether the given string is a part of the language a^n b or not */
/* Shows nothing if string is a part of language */
/* shows a message if string is not a part of language */
%{
#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 of the Above Program.
|
Program in YACC to recognize the language (a^n b , n>=10). (output to say input is valid or not) |
just a few changes required; In Yacc file:
ReplyDelete1) add a flag(for validity) and integer(to count till 10)
2) in production "expr: s B {if(count>=10) return 0; else yyerror("invalid");}
3) in production "expr: s: s A {count++;} | A {count++;};
4) set flag change in yyerror definition
count would throw yyerror till it becomes 10.
This comment has been removed by the author.
ReplyDelete