Compiler Design Lab Manual
Compiler Design Lab Manual
Compiler Design Lab Manual
2 Design a lexical analyzer for given language .the lexical analyzer should ignore 4
redundant spaces, tabs and new lines.
7 Design a lex Program to convert the substring abc to ABC from the given input string. 22
1. Intel based destktop PC of 166MHz or faster processor with at least 64 MB RAM and
100 MB free disk space.
Objectives:
OUTPUT 2:
OUTPUT 3:
Date:
Excersice: Design a Lexical analyzer for identifying different types of token used in C language
2)Implement the Lexical Analyzer Using Lex Tool.
/* program name is lexp.l */
%{
/* program to recognize a c program */
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* { printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}
int |
float |
char |
double |
while |
for |
do | if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
goto {printf("\n\t%s is a KEYWORD",yytext);}
"/*" {COMMENT = 1;}
/*{printf("\n\n\t%s is a COMMENT\n",yytext);}*/
"*/" {COMMENT = 0;}
/* printf("\n\n\t%s is a COMMENT\n",yytext);}*/
{identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
\} {if(!COMMENT) printf("\n BLOCK ENDS");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT) printf("\n\t%s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n\t%s is a NUMBER",yytext);}
\)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
int main(int argc,char **argv)
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1],"r");
if(!file)
{
printf("could not open %s \n",argv[1]);
exit(0);
}
yyin = file;
} yylex();
printf("\n\n");
return 0;
}
int yywrap()
{
return 0;
}
Input:
$vi var.c
#include<stdio.h>
main()
{
int a,b;
}
Output:
$lex lex.l
$cc lex.yy.c
$./a.out var.c
#include<stdio.h> is a PREPROCESSOR DIRECTIVE
FUNCTION
Three Test Outputs
main (
)
BLOCK BEGINS
int is a KEYWORD
a IDENTIFIER
b IDENTIFIER
BLOCK ENDS
I NPUT 2:
OUTPUT 2:
INPUT 3:
OUTPUT 3:
OUTPUT 2:
OUTPUT 3:
Signature of the Lab Incharge
Date:
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[0-9]+ {yylval.dval=atof(yytext);
return DIGIT;
}
\n|. return yytext[0];
%%
<parser.y>
%{
/*This YACC specification file generates the LALR parser for the program
considered in experiment 4.*/
#include<stdio.h>
%}
%union
{
double dval;
}
%token <dval> DIGIT
%type <dval> expr
%type <dval> term
%type <dval> factor
%%
line: expr '\n' {
;
printf("%g\n",$1);
}
expr: expr '+' term {$$=$1 + $3 ;}
| term
;
term: term '*' factor {$$=$1 * $3 ;}
| factor
;
factor: '(' expr ')' {$$=$2 ;}
| DIGIT
;
%%
int main()
{
yyparse();
}
yyerror(char *s)
{
printf("%s",s);
}
OUTPUT 2:
OUTPUT 3:
OUTPUT 3:
Date:
Excersice: Program to convert Infix expression to Postfix
6). Write a LEX Program to convert the substring abc to ABC from the given input string.
Lex Program
%{
/*The Program replaces the substring abc by ABC from
the given input string*/
#include<stdio.h>
#include<sting.h>
int i;
%}
%%
[a-zA-Z]* {
for(i=0;i<=yyleng;i++)
{
If((yytext[i]==’a’)&&( yytext[i+1]==’b’)&&( yytext[i+2]==’c’))
{
yytext[i]=’A’;
yytext[i+1]=’B’;
yytext[i+2]=’C’;
}
}
Printf(“%s”, yytext);
}
[\t]*return;
.*{ECHO;}
\n {printf(“%s”, yytext);}
%%
main()
{
yytext();
}
int yywrap()
{
return 1;
}
Three Test Outputs
OUTPUT 1:
[root@localhost]#
OUTPUT 2:
OUTPUT 3:
Date:
Excersice: Write a program for Recursive Descent Calculator.
7). Write a lex program to find out total number of vowels and consonants from the
given input sting.
Lex Program
%{
/*Definition section*/
int vowel_cnt=0,consonant_cnt=0;
%}
vowel [aeiou]+
consonant [^aeiou]
eol \n
%%
{eol} return 0;
[\t]+ ;
{vowel} {vowel_cnt++;}
%%
int main()
{
printf(“\n Enter some input string\n”);
yylex();
printf(“vowels=%d and consonant=%d\n”,vowel_cnt,consonant_cnt);
return 0;
}
Int yywrap()
{
return 1;
}
OUTPUT 1:
#lex alphalex.l
#gcc lex.yy.c
#./a.out
This Lex program counts the number of vowels and consonants in given text.
Enter the text and terminate it with CTRL-d.
Iceream
Vowels =4, consonants =3.
Output:
For lexical analyzer
[root@localhost]# lex Mylex.l
[root@localhost]# gcc lex.yy.c
[root@localhost]# ./a.out
123
NUMBER
bar123
WORD
OUTPUT 2:
OUTPUT 3:
Date:
Excersice: