VTU - SS and OS programs -LEX AND YACC


LEX PROGRAMS ::

                                          1a.l

%{
int lines=0,words=0,chars=0,blanks=0;
%}
%%
[\t] blanks++;
[\n] lines++;
[^ \t\n]+ {words++;chars+=yyleng;}
%%
yywrap()
{
printf("charcter=%d,lines=%d,blanks=%d,words=%d\n",chars,lines,blanks,words);
}
int main(int argc,char*argv[])
{
yyin=fopen(argv[1],"r");
yylex();
return 0;
}

                                             1b.l

%{
  int comment=0;
%}

%%
[/*].*[*/] {comment++;}
.echo ;
%%

int yywrap()
{
 return 1;
}

main(int argc,char**argv)
{
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("no of comment lines %d\n",comment);
}
---------------------------------------------------------------------------------

                                             2a.l

%{
#include<stdio.h>
int nplus=0,nminus=0,nmul=0,ndiv=0,id=0,flag1=0,flag2=0;
%}
%%
[(] {flag1++;}
[)] {flag1--;}
[a-zA-Z0-9]+ {flag2++;id++;}
[+] {flag2--;nplus++;}
[-] {flag2--;nminus++;}
[*] {flag2--;nmul++;}
[/] {flag2--;ndiv++;}
%%
main()
 printf("\nenter a valid arithematic expression\n");
 yylex();
 if(flag1!=0||flag2!=1)
     printf("invalid expression");
      else 
{
    printf("\nValid expression\n"); 
    printf("addition(+)=%d\n",nplus);
    printf("subtraction(-)=%d\n",nminus); 
    printf("multiplication(*)=%d\n",nmul);
    printf("division(/)=%d\n",ndiv); 
    printf("\nIDENTIFIER=%d\n",id);
}
}

                                              2b.l

%{
int valid;
%}
%%
[a-zA-Z][ ](and|but|or)[ ][a-zA-Z] { valid=1; }
.|[\n] ;
%%
main()
{    
printf("\nEnter the sentence ");
yylex();
if(valid)
{
printf("\nStatement is compound!\n");
}
else
{
printf("\nStatement is simple!\n");
}
}
------------------------------------------------------------------------------------

                                               3.l

%{
#include<stdio.h>
int count=0,flag=0;
%}

%%
"int "|"float"|"char"|"long" {flag=1;}
","|","|";" {if(flag) count++;}
%%
int main(int arg,char*argv[])
{
 yyin=fopen(argv[1],"r");
 yylex();
 printf("No of identifiers=%d\n",count);
}


LEX AND YACC PROGRAMS::

                                       4a.l

%{
#include"y.tab.h"
%}
%%
[0-9]+ return NUMBER;
[a-zA-Z] return ID;
[\t];
[\n]; 
. return yytext[0];
%%

                                    4a.y

%{
#include<stdio.h>
#include<stdlib.h>
%}
%token NUMBER ID
%left '+''-'
%left '*''/'
%left '('')'
%%
e:e'+'e|e'-'e|e'*'e|e'/'e|'('e')'|NUMBER|ID;
%%
main()
{
 printf("Enter the expression\n");
 yyparse();
 printf("valid expression\n");
}
yyerror()
{
 printf("invalid expression\n");
 exit(0);
}
-------------------------------------------------------------------

                                    4b.l

%{
#include"y.tab.h"
%}
%%
[a-zA-Z] return ALPHA;
[0-9] return DIGIT;
[\t];
[\n];
. return yytext[0];
%%

                                   4b.y

%{
#include<stdio.h>
#include<stdlib.h>
%}

%token ALPHA DIGIT
%%
e:ALPHA|e DIGIT|e ALPHA;
%%

main()
{
 printf("Enter the expression\n");
 yyparse();
 printf("valid expression\n");
}
yyerror()
{
 printf("invalid expression\n");
 exit(0);
}
--------------------------------------------------------------

                                 5a.l

%{
  #include"y.tab.h"
  int yylval;
%}

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

                                5a.y

%{
  #include<ctype.h>
  #include<stdio.h>
%}
%token NUM
%left '+''-'
%left '*''/'

%%
exp:e {printf("Value of the expression is %d\n",$1);}
e:e'+'e {$$=$1+$3;}
e:e'-'e {$$=$1-$3;}
e:e'*'e {$$=$1*$3;}
e:e'/'e {if($3==0) printf("Invalid\n"); $$=$1/$3;}
e:NUM {$$=$1;}
e:'-'e {$$=-$2;}
%%

main()
{
 printf("Enter the expression\n");
 yyparse();
 printf("valid expression\n");
}
yyerror()
{
 printf("invalid expression\n");
 exit(0);
}
---------------------------------------------------------------------

                                       5b.l

%{
 #include"y.tab.h"
%}

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

                                         5b.y

%{
#include<stdio.h>       
#include<stdlib.h>       
%}
%token A B
%%
S:A S B
| ;
%%
main()
{
 printf("Enter the expression\n");
 yyparse();
 printf("valid expression\n");
}
yyerror()
{
 printf("invalid expression\n");
 exit(0);
}
---------------------------------------------------------------

                                        6.l

%{
#include"y.tab.h"
%}

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

                                       6.y

%token A B
%%
S:X B
X:A A A A A A A A A A T;
T:T A |;
%%
main()
{
 printf("Enter the string\n");
 yyparse();
 printf("Valid\n");
}
 yyerror()
{
 printf("Invalid\n");
 exit(0);
}







13 comments:

  1. Replies
    1. http://ise123.blogspot.in/2012/11/vtu-ss-and-os-programs-2012.html
      - OS Programs

      Delete
  2. really helpfull blog during day before lab....

    ReplyDelete
  3. Great work. I think everybody should use this. I was really helpful to me. Thanks for putting this up.

    ReplyDelete
  4. le baddatthade fibonacci program na ubuntu li run maadodu hege helo

    ReplyDelete
  5. Guldu ..program run madak baralla program etkond yen madthiya :-P

    ReplyDelete
  6. where are the rest of the programs

    ReplyDelete
  7. U can find it in the next page Or use the SEARCH Bar ;-)

    ReplyDelete
  8. tutorials for this anywhere..??

    ReplyDelete