Posts Tagged ‘6th sem vtu. 6th sem computer science’

System Software Lab Programs ( Lex and Yaac Programs ) 6a

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

6) Program to recognize the grammar (anb, n>= 10).

6.l

/* 6.l */
%{
#include<stdio.h>
#include “y.tab.h”
%}
%%
[aA] return A;
[bB] return B;
.|n return yytext[0];
%%

—————————————–

6.y

/* 6.y */
%{
#include<stdio.h>
%}
%token A B
%%
stmt: S ‘n’ {printf(“nValid expression”);exit(1);}
;
S: X B
X: X A | A A A A A A A A A A
%%
main()
{
printf(“nEnter the expression: “);
if(yyparse())
{
printf(“nValid expression”);
exit(0);
}
}
int yyerror()
{
printf(“nInvalid expressionn”);
return 1;
}

int yywrap()
{
return 1;
}

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

// June 13th, 2009 // Comments // 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;
}

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

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

5) a. Program to evaluate an arithmetic expression involving operators +, -, * and /.

5a.l

%{
#include “y.tab.h”
extern int yylval;
%}

%%
[0-9]+ { yylval=atoi(yytext);return NUM;}
[ t];
n return 0;
. return yytext[0];
%%

————————————————-

5a.y
/* 5a.y */
%{
#include<stdio.h>
%}
%token NUM
%left ‘-”+’
%left ‘*”/’

%%
start: expr    {printf(“nValid expression.nValue: %d”,$1);}

expr: expr’+'expr   {$$=$1+$3;}
|expr’-'expr    {$$=$1-$3;}
|expr’*'expr    {$$=$1*$3;}
|expr’/'expr     {if($3==0)
yyerror(“Divide by zero”);
else
$$=$1/$3;
}
|’(‘ expr ‘)’  {$$=$2;}
|NUM
;
%%

main()
{
printf(“nEnter an expression: “);
yyparse();
return(0);
}

int yyerror(char *msg)
{
printf(“n%s”,msg);
printf(“nInvalid expression”);
exit(0);
}

System Software Lab Programs ( Lex and Yaac Programs ) 4a

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

4) a. Program to recognize a valid arithmetic expression that uses operators+, -, * and /.

4a.l

%{
#include “y.tab.h”
extern int yylval;
%}

%%
[0-9]+ {yylval=atoi(yytext); return NUM; }
[ t] ;
n return 0;
. return yytext[0];
%%

———————————————–

4a.y

%{
#include<stdio.h>
%}

%%

%token NUM
%left ‘+’ ‘-’
%left ‘+’ ‘-’
%nonassoc UMINUS

%%

e:e’+'e |
e’-'e |
e’*'e |
e’/'e {if($3==0)  {printf(“n DIVIDE BY ZERO ERROR”); exit(0);}} |
‘(‘e’)’ |
‘-’e %prec UMINUS |
NUM
;
%%

yyerror()
{
printf(“invalid exppressionn”);
exit(0);
}

int main()
{
printf(“enter an expression:”);
yyparse();
printf(“valid expression”);
return 0;
}



System Software Lab Programs ( Lex and Yaac Programs ) 3.l

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

3) Program to recognize and count the number of identifiers in a given
input file.

%{
int id=0;
%}

ID [_a-zA-Z][a-zA-Z0-9]*
DECLN “int”|”float”|”char”|”short”|”double”|”long”|”unsigned”
%x DEFN

%%
{DECLN}    {BEGIN DEFN;}
<DEFN>{ID}, id++;
<DEFN>{ID}; id++;
<*>n ;
<*>. ;
%%

main(int argc, char **argv)
{
if(argc==2)
{
yyin=fopen(argv[1],”r”);
yylex();
printf(“n Number of identifiers : %dn”,id);
}
else
printf(“n Usage :%s <file>n”,argv[0]);
}

System Software Lab Programs ( Lex and Yaac Programs )2b.l

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

2b. b. Program to recognize whether a given sentence is simple or compound.

%{
#include<stdio.h>
int F0=0,F1=0,F2=0,error=0,l1=0,l2=0;
%}

verb am|run|sit|did|study|is|large|go|come
subject [a-zA-Z]+
compnd “and”|”but”|”also”|”either”|”neither”|”yet”|”still”|”consequences”

%%
{verb} { if(F2==1)
l2=1;
F2=1;
if(F1==0)
error=1;
}

{compnd} { F0=1; }
{subject} { if(F1!=0)
l1=1;
F1++;
}
%%

main()
{
printf(“n Enter a sentence: “);
yylex();

if(error==1 || F2==0 || F1==0)
{
printf(“n Invalid sentence”);
exit(0);
}

if(F0==1 && l1==1 && l2==1)
printf(“n Compound sentencen”);
else
printf(” nSimple sentencen”);
}

System Software Lab Programs ( Lex and Yaac Programs )2a.l

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

2) a. Program to recognize a valid arithmetic expression and to recognize
the identifiers and operators present. Print them separately.

%{
#include<stdio.h>
#include<string.h>
#define max 20
int flag,i,j,k,top,b;
char stack[max],ident[max],oper[max],brac[max];
%}

%%
[a-zA-Z0-9] {j++;strcat(ident,yytext);}
[a-zA-Z0-9]+ {flag=1;}
“+” {oper[k++]=’+';}
“-” {oper[k++]=’-';}
“*” {oper[k++]=’*';}
“/” {oper[k++]=’*';}
“$” {oper[k++]=’$';}
“^” {oper[k++]=’^';}
“%” {oper[k++]=’%';}
“(” { stack[++top]=’(‘;brac[b++]=’(‘;}
“)” { if (stack[top]==’(‘ && top!=-1) top–;flag=0;brac[b++]=’)';}
%%

int  main()
{
int i=j=k=b=flag=0;
top=-1;

printf(“nEnter the Expression : “);
yylex();
printf(“nThe identifiers are :  “);

for(i=0;i<j;i++)
printf(“t%c”,ident[i]);

printf(“nNo.of identifiers are : %d”,j);
printf(“nThe operators are :n”);

for(i=0;i<k;i++)
printf(“t%c”,oper[i]);

printf(“nNo. of operators are :%d”,k);
if(flag==0 && top==-1 && j==k+1)
printf(“nValid expression”);
else
printf(“nInvalid expression”);

return 0;
}

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

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

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

1b. Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file.

%{
int c=0,state=1;
%}

%%
“/*” { state=0;}
“*/” { c++; if (!state) state=1;}
{ if (state==1)
fprintf(yyout,”%s”,yytext);
}
%%

FILE * fp;
main(int argc,char ** argv)
{
if(argc<=1)
{
printf(“nNo file”);
exit(1);
}

fp=fopen(argv[1],”w”);

if(!fp)
{
printf(“nNo output file”);
exit(1);
}

yyout=fp;

fp=fopen(argv[1],”r”);

if(!fp)
{
printf(“nNo inpput file”);
exit(1);
}

yyin=fp;
yylex();
printf(“nNumber of comment lines : %d”,c);
}

yywrap()
{
if(state==0)
{
printf(“nUnterminated commennt”);
return 1;
}
}

System Software Lab Programs ( Lex and Yaac Programs )exa

// February 21st, 2009 // Comments // Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

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

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

1 a) Lex Program to count the number of characters, words, spaces and lines in a given input file.

% {

#include<stdio.h>
int (cc=0,lc=10,wc=0,sc=0)

% }

[^ t n]+ {wc++;cc+=yyleng;}
[/t]      {sc++;}
n        {lc++}

%%

main(int arg c, char * argv[])
{
FILE * file;
if(argc>1)

file=fopen(argv[1],”r”);
if(!file)
{
printf(“Could not open file :) ;
exit(1);
}
yyin=file;
}
yylex();

printf(“n Number of Spaces = %d “,sc);
printf(“n Number of Words = %d “,wc);
printf(“n Number of Characters = %d “,cc);
printf(“n Number of Lines  = %d “,lc);

return 0;

}

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

SAMPLE OUTPUT

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

FILE CONTENT :

HELLO WORLD,

WELCOME TO MY FIRST LEX PROGRAM

PROGRAM OUTPUT :

Number of Spaces = 6

Number of Words = 8
Number of Characters = 38
Number of Lines  = 2

 

[Request : If you are going to post this program on any other website please link back to original post ]