fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. // Variables for part (a)
  7. int max_length = 0;
  8. char longest_word[100] = "";
  9.  
  10. // Variables for part (b)
  11. int vowels = 0, consonants = 0;
  12.  
  13. // Functions for part (c)
  14. void check_pascal_identifier(const char *str) {
  15. printf("Pascal Identifier: %s\n", str);
  16. }
  17.  
  18. void check_unsigned_number(const char *str) {
  19. printf("Unsigned Number: %s\n", str);
  20. }
  21. %}
  22.  
  23. %%
  24. [a-zA-Z]+ {
  25. // Part (a): Find the longest word
  26. int length = strlen(yytext);
  27. if (length > max_length) {
  28. max_length = length;
  29. strcpy(longest_word, yytext);
  30. }
  31.  
  32. // Part (b): Count vowels and consonants
  33. for (int i = 0; i < length; i++) {
  34. if (strchr("aeiouAEIOU", yytext[i]))
  35. vowels++;
  36. else
  37. consonants++;
  38. }
  39. }
  40.  
  41. [0-9]+\.[0-9]+([eE][+-]?[0-9]+)? {
  42. // Part (c): Match floating-point numbers with optional scientific notation
  43. check_unsigned_number(yytext);
  44. }
  45.  
  46. [0-9]+ {
  47. // Part (c): Match integers
  48. check_unsigned_number(yytext);
  49. }
  50.  
  51. [a-zA-Z_][a-zA-Z0-9_]* {
  52. // Part (c): Match Pascal identifiers
  53. check_pascal_identifier(yytext);
  54. }
  55.  
  56. [^a-zA-Z0-9]+ { /* Ignore non-alphanumeric characters */ }
  57.  
  58. \n|\t|\r { /* Ignore whitespace */ }
  59.  
  60. .|\n { /* Catch-all rule */ }
  61. %%
  62.  
  63. int main() {
  64. printf("Enter the input string: ");
  65. yylex();
  66. printf("\nLongest Word: %s (Length: %d)\n", longest_word, max_length);
  67. printf("Vowel Count: %d\n", vowels);
  68. printf("Consonant Count: %d\n", consonants);
  69. return 0;
  70. }
  71.  
Success #stdin #stdout #stderr 0.03s 6892KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/oaZWFM/prog:70:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit