Posts Tagged ‘system software lab’

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

// June 14th, 2009 // 1 Comment » // Educational, Engineering, Unix and System Software Lab // Written by

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 expression\n”);
return 1;
}

int yywrap()
{
return 1;
}

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

// June 13th, 2009 // No Comments » // Educational, Engineering, Unix and System Software Lab // Written by

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 // No Comments » // Educational, Engineering, Unix and System Software Lab // Written by

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 // 1 Comment » // Educational, Engineering, Unix and System Software Lab // Written by

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 exppression\n”);
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 // No Comments » // Educational, Engineering, Unix and System Software Lab // Written by

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 : %d\n”,id);
}
else
printf(“\n Usage :%s <file>\n”,argv[0]);
}

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

// June 9th, 2009 // No Comments » // Educational, Engineering, Unix and System Software Lab // Written by

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 sentence\n”);
else
printf(” \nSimple sentence\n”);
}

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

// June 9th, 2009 // 2 Comments » // Educational, Engineering, Unix and System Software Lab // Written by

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 // 3 Comments » // Educational, Engineering, Unix and System Software Lab // Written by

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 ( UNIX Programs ) – 7b

// June 7th, 2009 // 3 Comments » // Educational, Engineering, Unix and System Software Lab // Written by

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

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

7b) C program to prompt the user for the name of an environment variable and print its value if it is defined and a suitable message otherwise; and to repeat the process if user wants it.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

int main()
{
char env[10],*p;
int ch=1;

while(ch!=0)
{
printf(“\n Enter name of evironment variable :”);
scanf(“%s”,env);

p=getenv(env);
printf(“%s”,p);
printf(“\n Want to change ? 1 for yes & 0 for no :”);
scanf(“%d”,&ch);
}
return 0;
}

[Click Here to get other programs by email ]

System Software Lab Programs ( UNIX Programs ) – 7a

// June 7th, 2009 // No Comments » // Educational, Engineering, Unix and System Software Lab // Written by

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

[Click Here to get other programs by email ]

………………………………………………………………………………………………………………………….
PART -B

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

7. a) Shell script to implement terminal locking. It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for password confirmation (to retype the password). If a match occurs, it must lock the terminal and promp for the password. If the proper password is entered, the terminal must be unlocked. Note the script must be written to disregard BREAK, Control-D etc. No time limit need be implemented for the lock duration.

stty -echo
echo “Enter the password: ”
read pw
echo “Confirm password: ”
read cpw
if["$pw" = "$cpw"]
then
trap “.” 1 2 15 18 30
echo “Keyboard locked”
fi
echo “Enter password to resume
read npw
while["$pw" = "$npw"]
do
echo “Enter password”
read npw
done
echo “Terminal unlock”
fi
stty sane

[Click Here to get other programs by email ]

Get Adobe Flash playerPlugin by wpburn.com wordpress themes