download required software : click here
write a yacc program and save it as prg1.y:
%{
// yacc program to implement the grammar
/*
S->(S)
|a
*/
#include <stdio.h>
#include <stdlib.h>
%}
%%
Stmt1 : Stmt1 '\n' { printf ("\n.. Valid Expression.. \n");
exit(0); }
| S
| error { printf ("\n..Invalid ..\n");
exit(0); }
;
S : '(' S ')'
| 'a'
;
%%
main ( )
{
printf("Enter a string to validate : ");
yyparse ( );
}
yylex()
{ int ch;
while(ch=getchar())
return ch;
}
yyerror(char *s)
{
printf( "%s", s );
}
write a yacc program and save it as prg1.y:
%{
// yacc program to implement the grammar
/*
S->(S)
|a
*/
#include <stdio.h>
#include <stdlib.h>
%}
%%
Stmt1 : Stmt1 '\n' { printf ("\n.. Valid Expression.. \n");
exit(0); }
| S
| error { printf ("\n..Invalid ..\n");
exit(0); }
;
S : '(' S ')'
| 'a'
;
%%
main ( )
{
printf("Enter a string to validate : ");
yyparse ( );
}
yylex()
{ int ch;
while(ch=getchar())
return ch;
}
yyerror(char *s)
{
printf( "%s", s );
}
compile the program using the command: bison prg1.y
with this command a C program is created in the current folder with name prg1.tab.c
now compile and run the C program:


No comments:
Post a Comment