Compiler Design Lab Record
Exp. 01
/*Lex program to recognize keywords, identifier, Digit or Constant, Operator, Special Symbol*/
%{
#include<stdio.h>
%}
%%
int | float | char {printf("%s is a Keyword", yytext);} [0-9]+ {printf("%s is a Number", yytext);}
[a-zA-Z][a-zA-Z|0-9]* {printf("%s is an Identifier", yytext);} [+ | - | *
| /] {printf("%s is a Operator", yytext);} [@|#|$|&] {printf("%s is a Special
Symbol", yytext);}
. | \n {ECHO;}
%%
int main()
{
printf("Please enter the string..!"); yylex();
}
int yywrap()
{
return 1;
}
OUTPUT:-
Exp. 02
/*Lex program to find and count Vowels and Consonants*
%{
#include<stdio.h> int Vow=0,
Cons=0;
%}
%%
[ \t\n]+ ;
[aeiouAEIOU]
{printf("It is a vowel\n"); Vow++;}
[^aeiouAEIOU] {printf("It is a consonant\n"); Cons++;}
%%
int main()
{
printf("Enter a string:\n"); yylex();
printf("Number of Vowel=%d and Number of Consonant=%d\n\n",Vow, Cons);
}
int yywrap()
{
return 1;
}
OUTPUT:-
Exp. 03
/*Lex program to find and count Upper Case and Lower Case*/
%{
#include<stdio.h> int l=0;
int u=0;
%}
%%
[A-Z] {printf("Uppercase\n");u++;}
[a-z] {printf("Lowercase\n");l++;}
%%
int main()
{
printf("Enter a string:\n"); yylex();
printf("Uppercase=%d and Lowercase=%d\n\n",u,l);
}
int yywrap()
{
return 1;
}
OUTPUT:-
Exp. 04
/* Lex Program to recognize Small and Capital Words */
%{
#include<stdio.h>
%}
%%
[A-Z]+[
\t\n] {printf("%s a capital word\n", yytext);} [a-z]+[ \t\n] {printf("%s a small word\n", yytext);}
%%
int main()
{
printf("Enter a string:\n"); yylex();
}
int yywrap()
{
return 1;
}
OUTPUT:-
Exp. 05
/*
Lex Program to count number
lines, spaces, words and character */
%{
#include<stdio.h>
int lc=0, sc=0, wc=0,
cc=0;
%}
%%
[\n]
{lc++; cc+=yyleng;} [ \t] {sc++; cc+=yyleng;}
[^\t\n ]+ {wc++; cc+=yyleng;}
%%
int main()
{
printf("Enter the input:\n"); yylex();
printf("No. of New Lines = %d\n", lc); printf("No. of Spaces = %d\n",
sc); printf("No. of Words =
%d\n", wc); printf("No. of Characters = %d\n", cc);
}
int yywrap()
{
return 1;
}
OUTPUT:-
Exp. 06
/*
Lex program to recognize simple
and compound sentence
*/
%{
#include<stdio.h> int flag=0;
%}
%%
(""[aA][nN][dD]"")|(""[oO][rR]"")|(""[bB][uU][tT]"") {flag=1;}
\n {return
0;}
%%
int main()
{
printf("Enter the sentence:\n"); yylex();
if(flag==0)
{
printf("It is a simple
sentence");
}
else
{
printf("It is a compound
sentence");
}
}
int yywrap()
/*
Lex Part: Binary to Decimal
Conversion*/ %{ /* Definition section */ #include<stdio.h> #include<stdlib.h> #include"y.tab.h" extern
int yylval; %}
/* Rule Section */ %% 0
{yylval=0;return
ZERO;} 1 {yylval=1;return ONE;}
[ \t] {;} \n return 0; . return yytext[0]; %%
int yywrap() { return 1; } |
/*
YACC Part: Binary to Decimal Conversion */ %{ /* Definition section */ #include<stdio.h> #include<stdlib.h> void
yyerror(char *s); %} %token ZERO
ONE
/* Rule Section */ %% N: L
{printf("\n%d", $$);} L:
L B {$$=$1*2+$2;} | B {$$=$1;} B:ZERO
{$$=$1;} |ONE {$$=$1;}; %%
//driver code int
main() { while(yyparse()); }
yyerror(char *s) { fprintf(stdout,
"\n%s", s); } |
OUTPUT:-
Exp. 08
/* Lex Part: Conversion of Infix to Postfix expression */ %{ /* Definition section */ %} ALPHA [A-Z a-z] DIGIT
[0-9]
/* Rule Section */ %% {ALPHA}({ALPHA}|{DIGIT})* return ID; {DIGIT}+ {yylval=atoi(yytext); return
ID;} [\n \t] yyterminate(); . return
yytext[0]; %% |
/* YACC Part: Conversion of Infix to Postfix expression */ %{ /* Definition section */ #include
<stdio.h> #include <stdlib.h> %}
%token ID %left '+'
'-' %left '*'
'/' %left UMINUS
/* Rule Section */ %%
S : E E : E'+'{A1();}T{A2();} | E'-'{A1();}T{A2();} | T ; T : T'*'{A1();}F{A2();} | T'/'{A1();}F{A2();} | F ; F : '('E{A2();}')' | '-'{A1();}F{A2();} | ID{A3();} ;
%% /* YACC Part:
Conversion of Infix
to Postfix expression */ #include"lex.yy.c" char
st[100]; int top=0;
//driver code int
main() { printf("Enter infix
expression: "); yyparse(); printf("\n"); return 0; } A1() { st[top++]=yytext[0]; }
A2() { printf("%c", st[--top]); } A3() { printf("%c", yytext[0]); } |
OUTPUT:-
Exp. 09
/*Lex Part: Strings that starts and ends with 0
or 1 */ %{ /* Definition section
*/ extern int
yylval; %}
/* Rule Section */ %%
0 {yylval = 0; return
ZERO;}
1 {yylval = 1; return
ONE;}
.|\n {yylval
= 2; return 0;}
%% |
/*
YACC Part: Strings that starts
and ends with 0 or 1 */ %{ /* Definition section
*/ #include<stdio.h> #include <stdlib.h> void yyerror(const char *str) { printf("\nSequence Rejected\n"); }
%}
%token ZERO
ONE
/* Rule Section */ %%
r : s {printf("\nSequence Accepted\n\n");} ;
s : n | ZERO a | ONE b ;
a : n a | ZERO ;
b : n b | ONE ;
n : ZERO | ONE ;
%%
#include"lex.yy.c" //driver code int
main() { printf("\nEnter Sequence of Zeros and Ones
: "); yyparse(); printf("\n"); return 0; } |
OUTPUT:-
Exp. 10
/* Lex Part: Recognize string with grammar { anbn
| n≥0 } */ %{ /* Definition section */ #include "y.tab.h" %}
/* Rule Section */ %% [aA] {return
A;} [bB] {return B;} \n {return
NL;} . {return yytext[0];} %%
int yywrap() { return 1; } |
/* YACC
Part: Recognize string
with grammar { anbn | n≥0 } */ %{ /* Definition section */ #include<stdio.h> #include<stdlib.h> %}
%token A B NL
/* Rule Section */ %% stmt: S NL { printf("valid string\n"); exit(0); } ; S: A S B | ; %%
int yyerror(char *msg) { printf("invalid string\n");
exit(0); }
//driver code main() { printf("enter
the string\n"); yyparse(); } |
OUTPUT:-
Exp. 11
/* Lex Part: Calculator */ %{ /* Definition section */ #include<stdio.h> #include "y.tab.h" extern
int yylval; %}
/* Rule Section */ %% [0-9]+ { yylval=atoi(yytext); return NUMBER;
} [\t] ;
[\n] return
0;
. return yytext[0];
%%
int yywrap() { return 1; } |
/*
YACC Part: Calculator */ %{ /* Definition
section */ #include<stdio.h> int flag=0; %}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
/* Rule Section */ %%
ArithmeticExpression: E{ printf("\nResult=%d\n", $$);
return 0; }; E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%
//driver code void
main() { printf("\nEnter Any Arithmetic Expression which can have operations Addition, Subtraction, Multiplication, Division, Modulus and Round brackets:\n");
yyparse(); if(flag==0) printf("\nEntered arithmetic expression is Valid\n\n");
|
OUTPUT:-
Exp. 12
/*Lex Part:
To check whether given string is Palindrome or not */ %{ /* Definition section */ #include <stdio.h> #include <stdlib.h> #include "y.tab.h" %}
/* %option noyywrap */
/* Rule Section */ %%
[a-zA-Z]+ {yylval.f = yytext; return STR;} [-+()*/] {return yytext[0];} [ \t\n] {;}
%%
int yywrap() { return -1; } |
/*YACC Part:
To check whether
given string is
Palindrome or not
*/ %{ /* Definition section */ #include <stdio.h> #include <string.h> #include <stdlib.h> extern
int yylex();
void
yyerror(char *msg); int flag;
int i; int k =0; %}
%union { char*
f; }
%token <f> STR %type <f> E
/* Rule Section */ %%
S : E { flag = 0; k = strlen($1) - 1; if(k%2==0){
for (i = 0; i
<= k/2; i++) { if ($1[i]
== $1[k-i]) { } else { flag
= 1; } } if (flag == 1)
printf("Not palindrome\n"); else
printf("palindrome\n"); printf("%s\n", $1);
}else{
for (i = 0; i < k/2; i++) { if ($1[i] == $1[k-i]) { } else { flag = 1; } } if (flag == 1) printf("Not palindrome\n"); else printf("palindrome\n"); printf("%s\n", $1); } } ;
E : STR {$$ = $1;} ;
%%
void yyerror(char *msg) { fprintf(stderr,
"%s\n", msg); exit(1); } //driver code int main() { yyparse(); return 0; } |
OUTPUT:-