Posts

identify a sentence and return the length of each word.

%{ int a=0; int a2; %} %% " " {a++;} [a-zA-Z]+ {printf("word: %s \t\t letters in the word: %d\n",yytext,yyleng);} %% main() { yylex(); a=a+1; printf("Sentence lenght: %d",a); }

String starts with 'a' and should contain at least 3 b's

%{ %include<stdio.h> %} %% [a]+[b]+[a]*[b]+[a]*[b]+[ab]* {prinf("valid");} %% main() { yylex(); }

Identifying words of length 4-8 characters long.

%{ #include<stdio.h.> int a=0; %} %% [a-zA-Z0-9._]+ {a=yyleng; if(a==4||a==5||a==6||a==7||a==8) printf("Word accepted");} %% main() { yylex(); }

To find number of blank spaces and new lines

%{ #include<stdio.h> int a=0; int b=0' %} %% [""] {a++;} [\n] {b++;} %% main() { yylex(); printf("No. of spaces: %d",a); printf("No. of new lines: %d",b); }

Finding the number of operators in a given C file

%{ #include<stdio.h> int a; FILE **yyin; %} %% [+*/-] {a++;} %% main() { yyin=fopen("operators.txt","r");  //file name of a C file yylex(); printf("No. of operators are: %d",a); }

Identifying single line and multi line comments.

%{ #include<stdio.h> %} %% ['//]+{2} [a-zA-Z0-9]+ {printf("single line comment");} [/][*][a-zA-Z0-9]+[*][/] {printf("multi line comment");} %% main() { yylex(); }

Program to identify valid indian phone number.

%{ #include<stdio.h> %} %% ([789][0-9]+{9}) {printf("it is valid phone number");} %% main() { yylex(); }