Advertisement
Nahid8195

Lexical Analyzer Using Flex

Dec 10th, 2022 (edited)
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. %{
  2.     #include<stdio.h>
  3.     int others = 0;
  4.     int keyword = 0;
  5.     int id = 0;
  6. %}
  7.  
  8. %%
  9. "int"|"double"|"float"|"char"|"do"|"while"|"main"|"return"|"printf"|"scanf"|"include"|"stdio.h" {
  10. keyword++;
  11. printf("\'%s\' is a keyword\n",yytext);}
  12.  
  13. [a-zA-Z_][a-zA-Z0-9_]* {
  14. id++;
  15. printf("\'%s\' is a identifier\n",yytext);}
  16.  
  17.  
  18. . {}
  19. %%
  20.  
  21. int main()
  22. {
  23.     yyin = fopen("input.c","r");
  24.    
  25.     yylex();
  26.     printf("Number of identifier is %d\n", id);
  27.     printf("Number of keyword is %d\n", keyword);
  28.     return 0;
  29. }
  30.  
  31.  
  32. int yywrap()
  33. {
  34.    return 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement