Professional Documents
Culture Documents
Compiler Lab Assign
Compiler Lab Assign
%{
#include <stdio.h>
%}
%%
%%
intmain() {
yylex();
getchar();
return 0;
%{
#include <stdio.h>
intnumLines = 0;
%}
%%
\n { ++numLines; }
.*
%%
intmain() {
yylex();
getchar();
return 0;
%{
#include <stdio.h>
intlineNumber = 1;
%}
%%
\n { lineNumber++; }
%%
yylex();
getchar();
return 0;
WAP to remove comments from a file i.e to print all data except comments.
%{
#include <stdio.h>
%}
%%
#.*
%%
yylex();
getchar();
return 0;
%{
#include <stdio.h>
%}
%%
[[:digit:]]*\.[[:digit:]]* { printf("Float\n"); }
[[:digit:]]* { printf("Integer\n"); }
%%
yylex();
getchar();
return 0;
%{
#include <stdio.h>
%}
%%
%%
yylex();
getchar();
return 0;
%{
#include <stdio.h>
%}
%%
^[0-9]*$ {
printf("Unsigned\n");
else
printf("Signed\n");
^\-[0-9]*$ {
printf("Signed\n");
%%
intmain(intargc, char const *argv[]) {
yylex();
getchar();
return 0;
%{
#include <stdio.h>
%}
%%
%%
yylex();
getchar();
return 0;
}
WAP to count number of lines, words and characters in text file.
%{
#include <stdio.h>
%}
%%
. { numChars++; }
\n { numLines++; REJECT; }
%%
yylex();
getchar();
return 0;
%{
#include <stdio.h>
#include <stdlib.h>
char hex[20];
int top = 0;
int remain;
while (num> 0) {
if (remain > 9)
else
str[i] = hex[top-i-1];
str[top] = '\0';
return str;
%}
%%
%%
yylex();
getchar();
return 0;