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

@pycon 2013 bangalore..

Pycon 2013 India conference was held on Aug 30,31 & Sep 1st in Bangalore . This is the 5th year of PyCON India, conference was attended by more than 1100 or I would say Bangalore was filled with python enthusiasts 🙂 . The keynotes, sessions, workshops and panel discussion (on Python in education) were good …

Read more

Install bit torrent client ( transmission) for fedora ..

Friday or drive by post here 🙂 How to install and use bit torrent clients in fedora? its as simple as that: You need to install ‘transmission’ package to get bit torrent client for fedora. Once you installed it, bit torrent client is ready to use for torrent action. You can either use ‘transmission” cli …

Read more

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