System Software Lab Programs ( Lex and Yaac Programs ) 5b

// June 13th, 2009 // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

5b. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (anbn, n>= 0).

5b.l

%{
#include<stdio.h>
#include “y.tab.h”
%}

%%
[a] return A;
[b] return B;
. return yytext[0];
n return yytext[0];
%%
——————————————

5b.y

%{
#include<stdio.h>
%}
%token A B
%%
input:expr ‘n’ {return 0;}
expr: X
X: A X B|;
%%

main()
{
printf(“nEnter string: “);
if(!yyparse())
{
printf(“nValid”);
exit(0);
}
}
int yyerror()
{
printf(“nInvalid”);
return 1;
}
int yywrap()
{
return 1;
}