Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Lex program to display the same line as read from keyboard.

%{

#include <stdio.h>

%}

%%

%%

intmain() {

yyin = fopen("stdin", "r");

yylex();

getchar();

return 0;

Write a program to display only digits.

%{

#include <stdio.h>

intnumLines = 0;

%}

%%

\n { ++numLines; }

.*

%%
intmain() {

yyin = fopen("stdin", "r");

yylex();

printf("Number of lines: %d", numLines);

getchar();

return 0;

WAP to print line number of each line in a file.

%{

#include <stdio.h>

intlineNumber = 1;

%}

%%

([^\n]*) { printf("%d.\t%s\n", lineNumber, yytext); }

^\n { printf("%d.\n", lineNumber++); }

\n { lineNumber++; }

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("prac.txt", "r");

yylex();

getchar();
return 0;

WAP to remove comments from a file i.e to print all data except comments.

%{

#include <stdio.h>

%}

%%

#.*

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("stdin", "r");

yylex();

getchar();

return 0;

WAP to check entered number is Integer or Float.

%{

#include <stdio.h>

%}

%%
[[:digit:]]*\.[[:digit:]]* { printf("Float\n"); }

[[:digit:]]* { printf("Integer\n"); }

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("stdin", "r");

yylex();

getchar();

return 0;

WAP to check a given substring in text.

WAP to convert text into uppercase.

%{

#include <stdio.h>

%}

%%

[a-z] {printf("%c", (*yytext) + 'A' - 'a');}

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("stdin", "r");

yylex();
getchar();

return 0;

WAP to check for valid identifier.

WAP to check given string is verb or noun.

WAP to identify signed or unsigned integer.

%{

#include <stdio.h>

int max = -1;

%}

%%

^[0-9]*$ {

if (((long) atol(yytext)) > ((long) max))

printf("Unsigned\n");

else

printf("Signed\n");

^\-[0-9]*$ {

printf("Signed\n");

%%
intmain(intargc, char const *argv[]) {

while (max < 0) max--;

yyin = fopen("stdin", "r");

yylex();

getchar();

return 0;

WAP to print a string that starts and ends with “a”.

%{

#include <stdio.h>

%}

%%

^a.*a$ {printf("%s\n", yytext);}

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("stdin", "r");

yylex();

getchar();

return 0;

}
WAP to count number of lines, words and characters in text file.

%{

#include <stdio.h>

intnumChars = 0, numWords = 0, numLines = 0;

%}

%%

. { numChars++; }

[a-zA-Z]* { numWords++; REJECT; }

\n { numLines++; REJECT; }

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("prac.txt", "r");

yylex();

printf("Number of characters: %d\n", numChars);

printf("Number of words: %d\n", numWords);

printf("Number of lines: %d\n", ++numLines);

getchar();

return 0;

WAP to convert decimal to hexadecimal.

%{
#include <stdio.h>

#include <stdlib.h>

intnumChars = 0, numWords = 0, numLines = 0;

char* dtoh(long num) {

char hex[20];

int top = 0;

int remain;

while (num> 0) {

remain = num % 16;

num = num / 16;

if (remain > 9)

hex[top++] = 'A' + remain - 10;

else

hex[top++] = '0' + remain;

char* str = (char*) malloc((top+1) * sizeof(char));

for (inti = 0; i< top; ++i)

str[i] = hex[top-i-1];

str[top] = '\0';

return str;

%}
%%

[[:digit:]]+ { printf("%s\n", dtoh(atol(yytext))); }

%%

intmain(intargc, char const *argv[]) {

yyin = fopen("stdin", "r");

yylex();

getchar();

return 0;

You might also like