Cs6109 - Compiler Design: Lab Assignment
Cs6109 - Compiler Design: Lab Assignment
LAB ASSIGNMENT
Vezha Venthan T
2019503570
CODE:
%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}
%%
([a-zA-Z0-9])* {i++;} /
int yywrap(void){}
int main()
{
yylex();
return 0;
}
OUTPUT:
CODE:
%{
#include<stdio.h>
int lc=0, sc=0, tc=0, ch=0; /*Global variables*/
%}
/*Rule Section*/
%%
\n lc++; //line counter
([ ])+ sc++; //space counter
\t tc++; //tab counter
. ch++; //characters counter
%%
int main()
{
// The function that starts the analysis
yylex();
}
OUTPUT:
3) Lex program to take input from file and remove multiple spaces, lines
and tabs.
CODE:
%%
[ \n\t]+ {fprintf(yyout, "");}
. { fprintf(yyout, "%s", yytext); }
%%
int yywrap(){}
// driver code
int main()
{
INPUT:
OUTPUT:
CODE:
%{
int line_number = 1; // initializing line number to 1
%}
int yywrap(){}
return 0;
}
Input file:
#include <stdio.h>
int main() {
printf("Hello, World!");
printf("This is vezha");
printf("reg.no 2019503570");
return 0;
}
OUTPUT:
CODE:
%{
int n = 0 ;
%}
%%
. ;
%%
int main()
yylex();
OUTPUT:
CODE:
%{
#include <stdio.h>
%}
%%
^[a-zA-Z_][a-zA-Z0-9_]* {printf("Valid Identifier");}
^[^A-Za-z_][0-9A-za-z] {printf("Invalid Identifier");}
.;
%%
int yywrap(void)
{
return 1;
}
void main()
{
printf("\nEnter any input :");
yylex();
}
OUTPUT: