Lab No. 02 - Advanced Lexical Analyzer: Lab Manual CSI 412 - Compiler Sessional
Lab No. 02 - Advanced Lexical Analyzer: Lab Manual CSI 412 - Compiler Sessional
Complete Code:
Now, let’s combine the given patterns into a complete program.
1 %{
2 //For today we do not need any variables
3 %}
4 %%
5 [0-9]+ {printf("%s - Found a decimal number.\n",yytext);}
6 [0-9]+\.[0-9]+ {printf("%s - Found a double number.\n",yytext);}
7 123[0-5]|12[0-2][0-9]|1[0-1][0-9][0-9]|[1-9][0-9][0-9]|[8-9][0-9]|7[2-9]
{printf("%s - Found a decimal number within 72 - 1235.\n",yytext);}
8 12\.(0[2-9]|[1-9][0-9])|1[3-9]\.[0-9][0-9]|[2-8][0-9]\.[0-9][0-
9]|9[0-8]\.[0-9][0-9]|99\.([0-8][0-9]|9[0-3]) {printf("%s - Found a
double number within 12.02 - 99.93.\n",yytext);}
9 ([0-9]|[0-9][0-9]|1[0-1][0-9]|12[0-7])(\.(25[0-5]|2[0-4][0-9]|1[0-
9][0-9]|[0-9][0-9]|[0-9])){3} {printf("%s - Found a class A IP
address.\n",yytext);}
10 ^[AEIOU][a-zA-Z]* {printf("%s - Found an alphabet string that
starts with a capital letter vowel.\n",yytext);}
11 [a-zA-Z]*[aeiou]$ {printf("%s - Found an alphabet string that ends
with a small letter vowel.\n",yytext);}
12 [a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]* {printf("%s - Found a string that has
a number in it.\n",yytext);}
13 %%
14 int main(){
15 FILE *file;
16 file = fopen("code.c", "r") ;
17 if (!file) {
18 printf("couldnot open file");
19 exit (1);
20 }
21 else {
22 yyin = file;
23 }
24 yylex();
25 }
Now write some possible tokens in a file called “code.c” and run the program to check which lexemes are
accepted.