Posts

lex program to implement a simple calculator.

 /*lex program to implement  a simple calculator.*/ %{ int op = 0,i; float a, b; %} %option noyywrap dig [0-9]+|([0-9]*)"."([0-9]+) add "+" sub "-" mul "*" div "/" pow "^" ln \n %% {dig} {compute();} {add} {op=1;} {sub} {op=2;} {mul} {op=3;} {div} {op=4;} {pow} {op=5;} {ln} {printf("\n The Answer :%f\n\n",a);} %% compute() { if(op==0) /* atof() is used to convert  the ASCII input to float */ a=atof(yytext); else { b=atof(yytext); switch(op) { case 1:a=a+b;         break; case 2:a=a-b;         break; case 3:a=a*b;         break; case 4:a=a/b;         break; case 5:for(i=a;b>1;b--)         a=a*i;         break; } op=0; } } main() { yylex(); } Execution $flex cal.lex $ gcc lex.yy.c $ ./a.out 2+3 The Answer :5.000000 2.2/1.2  The Answer :1.833333 2.54-1.23  The Answer :1.310000 2/3  The Answer :0.666667

Lex program to search for a word in a file

Lex program to search a word in a file - This program will search a word in the file input.txt and display the message FOUND or NOT FOUND ************************************* %{ #include<string.h> void search(char *); %} %option noyywrap /* Rule Section */ %% [a-zA-Z]+ search(yytext); %% int main() {         // The function that starts the analysis         yylex();         return 0; } void search(char *str) {/* fp as pointer         of File type */         FILE *fp;         char temp[30];         /* fp points to the file input.txt         and opens it in read mode */         fp=fopen("input.txt", "r");         while((fscanf(fp, "%s", temp))!=EOF)         {                 if(!(strcmp(temp, str)))         ...

lex program to determine identifiers

lex program to determine valid identifiers.Users can enter an identifier and the program will display whether it is valid or not. ******************************************************************************** /*lex code to determine whether input is an identifier or not*/ %option noyywrap %% ^[a-zA-Z_][a-zA-Z0-9_]*           printf("Valid Identifier"); ^[^a-zA-Z _]                                  printf("Invalid Identifier"); .*                                                          printf("Invalid Indentifier"); %% main() {         yylex(); } Execution $ flex id3.lex $ gcc lex.yy.c $ ./a.out 2ghdd Invalid Indentifier 46shs Invalid Indentifier dre45 Valid Identifier dhd_dg...

lex program to count number of lines , words and characters

lex program to count number of lines, words  and characters *******************************************  %{ int num_lines=0,num_words=0,num_chars=0; %} %option noyywrap %% \n            ++num_lines; ++num_chars;++num_words; " "            ++num_words;++num_chars; .               ++num_chars; %% main() { yylex(); printf( "No of lines = %d, No of words=%d,No of chars = %d\n",num_lines,num_words, num_chars); }

Lex program to add line numbers to a given file and create a new file

Lex program to add line numbers to a given file and create a new file ************************************************** %{ int line_number = 1; %} %option noyywrap line .*\n %% {line} { printf("%10d %s", line_number, yytext); fprintf(yyout,"%10d %s",line_number,yytext);line_number++; } %% int main() { yyin = fopen("test.c","r"); yyout=fopen("testnew.c","w"); yylex(); return 0; } Execution $cat test.c # input file #include <stdio.h> void main() {  int a=2,b=4;  printf("%d",a+b); } $flex lnumber.lex $ gcc lex.yy.c $ ./a.out          1 #include <stdio.h>          2 void main()          3 {          4  int a=2,b=4;          5  printf("%d",a+b);          6 } $cat testnew.c # new file created             1 #include <stdio.h>          2 void main() ...

lex program to find the largest words and its length

lex code to find the largest words and its length  *************************** %{ int wordlen = 0,i=0; char lwords[100][100]; %} %option noyywrap %% [a-zA-Z]+ {         if (yyleng > wordlen)                        wordlen = yyleng;         strcpy(lwords[i++],yytext);         } %% int main() { int j; yylex(); printf("Maximum  length is %d ", wordlen); printf("largest length words\n"); for ( j=0;j<i;j++)  if(strlen(lwords[j])==wordlen)     printf("%s\n",lwords[j]); return 0; } Execution flex lword.lex $ gcc lex.yy.c $ ./a.out this is a program to find largest words from the input Maximum  length is 7 largest length words program largest

Converting BNF rules for arithmetic expression into yacc form and generating trees

 st.lex ****************** %{         #include <bits/stdc++.h>         #include "st.tab.h" %} %option noyywrap %% [a-zA-Z_][a-zA-Z_0-9]*  yylval.str=strdup(yytext);return ID ; ([0-9]*\.?[0-9]*)|(\".*?\")|(\'.\')     yylval.str=strdup(yytext);return VAL ; ";"             yylval.str=strdup(yytext);return SC ; "+"             yylval.str=strdup(yytext);return PL ; "-"             yylval.str=strdup(yytext);return MI ; "*"             yylval.str=strdup(yytext);return MUL ; "/"             yylval.str=strdup(yytext);return DIV ; "="             yylval.str=strdup(yytext);return EQ ; "("             yylval.str=strdup(yytext);return OP ; ")"            ...