You are on page 1of 5

Experiment 6a

%{

#include<stdio.h>

#include<stdlib.h>

int com=0;

%}

%%

"/*"[a-zA-Z0-9' '\t\n]+"*/" {com++;}

"//"[^\n]+"//" {com++;}

"//".* {com++;}

%%

int main()

yyin = fopen("Source.c","r");

yyout = fopen("Destination.c","w");

yylex();

fclose(yyin);

fclose(yyout);

printf("Total number of comments in the given file = %d\n",com);

}
Experiment 6b

%{

#include<stdio.h>

#include<stdlib.h>

#include"y.tab.h"

%}

%%

[0-9a-zA-Z] { printf("identifiers is:%s\n",yytext);return ID;}

[+|-|/|*|<|>|{|}|(|)|%|=] {printf("operators is %s\n",yytext);return OP;}

if|else|void|main|int|float|double|char|for|return {printf("keywords is %s\n",yytext);return KEY;}

%%
%{

#include<stdio.h>

#include<stdlib.h>

int yylex();

void yyerror(const char *s);

int id=0,op=0,key=0;

%}

%token ID OP KEY

%%

prog: ID prog{++id;}

|OP prog{++op;}

|KEY prog{++key;}

|ID {++id;}

|OP{++op;}

|KEY{++key;}

%%

extern FILE*yyin;

int main()

yyin = fopen("Source.c","r");

yyparse();

printf("Identifier : %d\n",id);

printf("Operator :%d\n",op);

printf("Keyword : %d\n",key);
return 0;

void yyerror(const char *s)

printf("Some Error Occured.\n");

exit(0);

You might also like