Tag Archives: Compiler

Lex : Check Valid Email

%{
#include<stdio.h>
#include<stdlib.h>
int flag=0;
%}
%%
[a-z . 0-9]+@[a-z]+".com"|".in" { flag=1; }
%%
int main()
{
yylex();
if(flag==1)
printf("Accepted");
else
printf("Not Accepted");
}

/*
Enter input
press Enter & ctrl+d to get output
*/

Output :-
abc@gmail.com
Accepted

Lex : Number is prime or not

%{
#include<stdio.h>
#include<stdlib.h>
int flag,c,j;
%}
%%
[0-9]+ {c=atoi(yytext);}
%%
int main()
{
yylex();
if(c==2)
{
printf("\n Prime number");
}
else if(c==0 || c==1)
{
printf("\n Not a prime number");
}
else
{
for(j=2;j<c;j++)
{
if(c%j==0)
flag=1;
}
if(flag==1)
printf("\n Not a prime number");
else if(flag==0)
printf("\n Prime number");
}
}

Conversion of arithmatic expression into postfix expression for given grammar

Grammars Are
E=E+T      E=T    T=T*F    T=F    F=NUM

// ( .y  File )

%{
#include<stdlib.h>
#include<stdio.h>
void yyerror(char *s);
%}
%token NUM
%%
E : E '+' T {printf("+");}
| T ;
T : T '*' F {printf("*");}
| F ;
F : NUM {printf("%d",yylval);};
%%
int main()
{
yyparse();
}
void yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// (.l File )

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%

/* To Compile :-
1) yacc -d filename.y
2) lex filename.l
3) gcc lex.yy.c y.tab.c -ll
4) ./a.out
Enter Expression like:  1+2*3
*/

Lex : Check Whether number is Even or Odd

%{
#include<stdio.h>
#include<stdlib.h>
int i;
%}
%%
[0-9]+ {i=atoi(yytext); if(i%2==0) printf("Even !"); else printf("Odd !");};
%%
int main()
{
yylex();
}
/*  To Compile :-
1) lex filename.l
2) gcc lex.yy.c -ll
3) ./a.out
Enter Input and press enter
*/

Output :-
145
Odd !
204
Even !

Identify Keywords

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

void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0
||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("static",str)==0||
strcmp("switch",str)==0||strcmp("case",str)==0||strcmp("if",str)==0||strcmp("else",str)==0||strcmp("extern",str)==0||
strcmp("register",str)==0||strcmp("auto",str)==0||strcmp("struct",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("volatile",str)==0||strcmp("sizeof",str)==0||strcmp("goto",str)==0||strcmp("default",str)==0||strcmp("void",str)==0||
strcmp("signed",str)==0||strcmp("continue",str)==0||strcmp("unsigned",str)==0||strcmp("short",str)==0||strcmp("const",str)==0||
strcmp("union",str)==0||strcmp("return",str)==0||strcmp("typedef",str)==0||strcmp("enum",str)==0||strcmp("case",str)==0||
strcmp("long",str)==0||strcmp("break",str)==0)

printf("\n%s is a keyword",str);
}

int main()
{

FILE *fp;
fp=fopen("k.txt","r");
if(fp==NULL)
{
printf("Error !");
return 0;
}

char word[20],c;
int i=0,j=0;

while((c=(char)fgetc(fp))!=EOF)
{
if(c!=' '){
word[j++]=c;
}
else{
word[j]='\0';
keyword(word);
j=0;
}
}
fclose(fp);
return 0;
}

Input File ( In k.txt file)
hello if this is int else print

Output :-

Identify Keyword
Identify Keyword

 

Conversion from binary to decimal

<br />//(Yacc File : Save as .y)

//to compile and Run
//:-> yacc -d FileName.y
//:-> lex FileName.l
//:-> gcc lex.yy.c y.tab.c -ll
//:-> ./a.output
//:-> enter input as - 101
//:-> output:-> 5

%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *s);
%}
%token ZERO ONE
%%
N: L {printf("\n%d",$$);}
L: L B {$$=$1*2+$2;}
| B {$$=$1;}
B:ZERO {$$=$1;}
|ONE {$$=$1;};
%%
int main()
{
while(yyparse());
}
yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// (Lex File : save as .l)

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
0 {yylval=0;return ZERO;}
1 {yylval=1;return ONE;}

[ \t] {;}
\n return 0;
. return yytext[0];
%%

Output :-
101
5

Find Execution time of your own Program

#include<stdio.h>
#include<time.h>

/*
The C library function clock_t clock(void) returns the number
of clock ticks elapsed since the program was launched.
To get the number of seconds used by the CPU, you will
need to divide by CLOCKS_PER_SEC.

*/


int main() {
clock_t start, end;
double total;
int i,j;
start = clock();
int c;
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {
c++;
}
}

end = clock();
printf("start = %d, end = %d\n", start, end);
printf("\nTotal = %ld",end-start);

return 0;
}

LEX & YACC : Calculator

<br />//    Yacc  File (.y)

//calculator

%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *s);
%}
%token NAME NUM
%%
statement: NAME'='expression
|expression{printf("\n%d\n",$1);};
expression:expression'+'NUM{$$=$1+$3;}
|expression'-'NUM{$$=$1-$3;}
|expression'/'NUM{$$=$1/$3;}
|expression'*'NUM{$$=$1*$3;}
|NUM{$$=$1;};
%%
int main()
{
while(yyparse());
}
yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// Lex file (.l)

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%

/*
To Compile :
1) yacc -d FileName.y
2) lex FileName.l
3) gcc lex.yy.c y.tab.c -ll
4) ./a.out
*/

Output :-
5+6
11

LEX : Count Vowels & Consonants in a String

%{
#include<stdio.h>
int vcount=0,ccount=0;
%}
%%
[a|i|e|o|u|E|A|I|O|U] {vcount++;}
[a-z A-Z (^a|i|e|o|u|E|A|I|O|U) ] {ccount++;}
%%
int main()
{
yylex();
printf("No. of Vowels :%d\n",vcount);
printf("No. of Consonants :%d\n",ccount);
return 0;
}

/* To Compile :
1) lex FileName.l
2) gcc lex.yy.c -ll
3) ./a.out
Type String and then press ctrl+D to get the result.
*/

Output :-

hello
No. of Vowels :2
No. of Consonants :3

Follow set of Given Grammer

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

int len=0,p,i,n,j;
char prod[10][10],c,fset[10];
void follow(char c);
void first(char c);

int main()
{
int i;
char ch;
printf("Enter The No. Of Productions:\n");
scanf("%d",&n);
printf("Enter The Productions:\n { Epsilon = # }\n");
for(i=0;i<n;i++)
{
scanf("%s",prod[i]);
}

printf("\nProductions are:\n");
for(i=0;i<n;i++)
{
printf("\t%s\n",prod[i]);
}

printf("\nWhose Follow you want to find:\n");
scanf("\n%c",&ch);
follow(ch);
printf("Follow of %c is : { ",ch);
for(i=0;i<len;i++)
{
printf("%c ",fset[i]);
}
printf("}\n");

return 0;
}

void follow(char c)
{
if(prod[0][0]==c)
fset[len++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(prod[i]);j++)
if(prod[i][j]==c)
{
if(prod[i][j+1]!='\0')
first(prod[i][j+1]);

else if(prod[i][j+1]=='\0' && prod[i][0]!=c)
follow(prod[i][0]);
}
}
}

void first(char c)
{

if(!(isupper(c)))
fset[len++]=c;

for(p=0;p<n;p++)
{
if(prod[p][0]==c)
{
if(prod[p][2]=='#')
follow(prod[i][0]);
else if(islower(prod[i][2]))
fset[len++]=prod[p][2];
else first(prod[p][2]);
}
}
}

First &amp; Follow