Practice of LEX / YACC in compiler writing.
//Write a program to print “Compiler” when input is “Hi” else print “Wrong Input”
%{
#include <stdio.h>
%}
%%
^Hi$ { printf("Compiler\n"); }
.|\n { printf("Wrong Input\n"); }
%%
int main() {
yylex(); // Start lexical analysis
return 0;
}
int yywrap(){
return 0;
}
%{
#include <stdio.h>
%}
%%
[0-9]+ {
if (atoi(yytext) % 2 == 0)
printf("Even\n");
else
printf("Odd\n");}
.* { printf("Wrong Input\n"); }
%%
int main() {
yylex(); // Start lexical analysis
return 0;}
int yywrap(){
return 0;
}
%{
#include <stdio.h>
int a, b, c = 0;
%}
%%
[0-9]+ {
if (c == 0)
a = atoi(yytext);
else
b = atoi(yytext);
}
\n {
if (a > b)
printf("%d is greater\n", a);
else if (a < b)
printf("%d is greater\n", b);
else
printf("Both are equal\n");
c = 0;
}
. {
c = 1;
}
%%
int main() {
printf("Enter two space-separated integers: \n");
yylex();
return 0;
}
int yywrap() {
return 0;
}



Comments
Post a Comment