lex program to eliminate single and multiline comments

%{ #include ;; %} %% “//”([a-z]|[0-9]|[A-Z]|” “)* {} “/*”([a-z]|[0-9]|[A-Z]|” “\””\n”)+”*/” {} %% main() { yylex(); return 0; } int yywrap() { } Sample output single line comment //testing single line comment test line /*multi*/ test line

Categories Lex

lex program to recognize identifier, keyword and number

%{ #include <stdio.h>; %} %% if|else|while|int|switch|for|char {printf(“keyword”);} [a-z]([a-z]|[0-9])* {printf(“identifier”);} [0-9]* {printf(“number”);} .* {printf(“invalid”);} %% main() { yylex(); return 0; } int yywrap() { } Sample output else keyword humble identifier 9876 number

Categories Lex

lex program to find words beginning and ending with a

%{ #include<stdio.h> %} %% (a|A)[a-z]*[0-9]*(a|A) {printf(“matching”);} (a|A)+ {printf(“matching”);} .* {printf(“not matching”);} %% main() { yylex(); return 0; } int yywrap() { } Sample output anna matching asssdf not matching

Categories Lex

lex program to prepend line number to each line

%{ #include&amp #include&amp int ln=0; %} %% “\n” {} .* {ln++;fprintf(yyout,”\n%d:%s”,ln,yytext);} %% main() { yyin=fopen(“try1.txt”,”r”); yyout=fopen(“try2.txt”,”w”); yylex(); return 0; } int yywrap() { } Sample output try1.txt hai hello world test file language processor try2.txt 1:hai 2:hello world 3:test file 4:language processor  

Categories Lex

lex program to count the number of words

%{ #include <stdio.h>; #include <string.h>; int i=0; %} %% ([a-z]|[A-Z][0-9])*    {i++;} “\n” {printf(“%d\n”,i);i=0;} %% main() { yylex(); return 0; } int yywrap() { } Sample output pls check the output 4

Categories Lex

lex program to find the size of a word

%{ #include<stdio.h> #include<stdlib.h> %} %% ([a-z]|[A-Z])*    {printf(“%d”,strlen(yytext));} %% main() { yylex(); return 0; } int yywrap() { } Sample output test line processed 4 4 9  

Categories Lex

lex program to eliminate white spaces

%{ #include<stdio.h> %} %% (” “) {} %% main() { yylex(); return 0; } int yywrap() { } Sample output this is test file thisistestfile  

Categories Lex

lex program to eliminate html tags

%{ #include<stdio.h> %} %% (“<“|”<\\”)[a-z|A-Z|0-9]*”>”    {printf(” “);} %% main() { yylex(); return 0; } int yywrap() { } Sample output <html>test file<\html> test file

Categories Lex

lex program to count alphanumeric words , alphabets, numbers and lines

%{ #include<stdio.h> #include<string.h> int al=0,aln=0,n=0,ln=0; %} %% ([a-z]|[A-Z])* {al++;} (” “)*.(\n) {} [0-9]* {n++;} (“\n”)*(” “)*(“\n”) {ln++;} ([a-z]|[A-Z]|[0-9])* {aln++;} %% main() { yyin=fopen(“inp.txt”,”r”); yylex(); printf(“Alphanumeric:%d\n alphabets:%d\n numbers:%d\n line:%d\n”,aln,al,n,ln); return 0; } int yywrap() { } Sample output inp.txt abc123 abcd efgh567 dfdfdgdf 1234 Alphanumeric:2 alphabets:2 numbers:1 line:4

Categories Lex

lex program to display line nos with string hello

%{ #include #include int ln=1; %} %% “\n” {} .*hello.* {fprintf(yyout,”\n%d:%s”,ln++,yytext);} .* {ln++;} %% main() { yyin=fopen(“try1.txt”,”r”); yyout=fopen(“try2.txt”,”w”); yylex(); return 0; } int yywrap() { } Sample output try1.txt hai hello world test file language processor hello try2.txt 2:hello world 5:hello