%{
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Variables for part (a)
int max_length = 0;
char longest_word[100] = "";
// Variables for part (b)
int vowels = 0, consonants = 0;
// Functions for part (c)
void check_pascal_identifier(const char *str) {
printf("Pascal Identifier: %s\n", str);
}
void check_unsigned_number(const char *str) {
printf
("Unsigned
Number: %s\n", str);}
%}
%%
[a-zA-Z]+ {
// Part (a): Find the longest word
int length = strlen(yytext);
if (length > max_length) {
max_length = length;
strcpy(longest_word, yytext);
}
// Part (b): Count vowels and consonants
for (int i = 0; i < length; i++) {
if (strchr("aeiouAEIOU", yytext[i]))
vowels++;
else
consonants++;
}
}
[0-9]+\.[0-9]+([eE][+-]?[0-9]+)? {
// Part (c): Match floating-point numbers with optional scientific notation
check_unsigned_number(yytext);
}
[0-9]+ {
// Part (c): Match integers
check_unsigned_number(yytext);
}
[a-zA-Z_][a-zA-Z0-9_]* {
// Part (c): Match Pascal identifiers
check_pascal_identifier(yytext);
}
[^a-zA-Z0-9]+ { /* Ignore non-alphanumeric characters */ }
\n|\t|\r { /* Ignore whitespace */ }
.|\n { /* Catch-all rule */ }
%%
int main() {
printf("Enter the input string: ");
yylex();
printf("\nLongest Word: %s (Length: %d)\n", longest_word, max_length);
printf("Vowel Count: %d\n", vowels);
printf("Consonant Count: %d\n", consonants);
return 0;
}