Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. /* This is a lex program to identify and count identifiers, keywords, numbers and operators in a c program */
  2. %{
  3. int numcount=0,idcount=0, keycount=0, opcount=0;
  4. %}
  5. id [a-zA-Z][A-Za-z0-9]*
  6. num [0-9]+
  7. keywords "auto"|"break"|"case"|"char"|"const"|"continue"|"default"|"do"|"double"|"else"|"enum"|"extern"|"float"|"for"|"goto"|"if"|"int"|"long"|"register"|"return"|"short"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"void"|"volatile"|"while"
  8. test "eureka"
  9.  
  10. preprocessor [#][a-z A-Z0-9\.<>]*
  11. operators [+\-=/*]
  12.  
  13.  
  14. %%
  15.  
  16. /*"quit" {
  17. printf("There are %d numbers.\n",numcount);
  18. printf("There are %d identifiers.\n",idcount);
  19. yyterminate();
  20. }*/
  21.  
  22. {preprocessor} {
  23. printf("Skipping preprocessor directives %s\n",yytext);
  24. }
  25.  
  26. {keywords} {
  27. printf("%s is a keyword.\n",yytext);
  28. keycount++;
  29. }
  30.  
  31. {test} {
  32. printf("This is a test.\n");
  33. }
  34.  
  35. {id} {
  36. printf("%s is an Identifier\n",yytext);
  37. idcount++;
  38. }
  39.  
  40.  
  41. {num} {
  42. printf("%s is a Number\n",yytext);
  43. numcount++;
  44. }
  45.  
  46. {operators} {
  47. printf("%s is an operator.\n",yytext);
  48. opcount++;
  49. }
  50.  
  51. . {;}
  52.  
  53. %%
  54.  
  55. int
  56. main(int argc, char **argv){
  57. if(argc==2){
  58. yyin=fopen(argv[1],"r");
  59. }
  60. yylex();
  61. printf("There are %d numbers.\n",numcount);
  62. printf("There are %d identifiers.\n",idcount);
  63. printf("There are %d keywords.\n",keycount);
  64. printf("There are %d operators.\n",opcount);
  65. return;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement