Advertisement
Guest User

AASHAY

a guest
Jul 6th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. %{
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. #include<string.h>
  5.  
  6. struct sym
  7. {
  8. char sname [15];
  9.  
  10. }s[20];
  11.  
  12. int refer = 0;
  13.  
  14. int insert(char *yytext)
  15. { int i = 0 ;
  16. for(i = 0;i < 20; i++)
  17. if(strcmp(s[i].sname,yytext) == 0)
  18. { printf("\n%s keyword already there",yytext);
  19. return 0;
  20. }
  21.  
  22. strcpy(s[refer++].sname,yytext);
  23. return 1;
  24. }
  25.  
  26. void display()
  27. { int i = 0;
  28. for(i = 0 ; i<20; i++)
  29. printf("%s\n",s[i].sname);
  30. }
  31.  
  32. %}
  33.  
  34. %%
  35. "#include"[ \t]*"<"|\"[ \t]*[^\n"][ \t]*">"|\" { printf("\n #include found: %s",yytext); insert(yytext);}
  36. "main" { printf("\n Main starts: %s",yytext);insert(yytext);}
  37. "(" { printf("\n Parenthesis: %s",yytext);insert(yytext);}
  38. ")" { printf("\n Parenthesis: %s",yytext);insert(yytext);}
  39. [0-9] { printf("\n Number: %s",yytext);insert(yytext);}
  40. "int"|"float"|"char"|"double" { printf("\n Keyword: %s",yytext);insert(yytext);}
  41. "," { printf("\n Comma: %s",yytext);insert(yytext);}
  42. ";" { printf("\n Semicolon: %s",yytext);insert(yytext);}
  43. "::" { printf("\n Scope resolution: %s",yytext);insert(yytext);}
  44. "*"|"+"|"-"|"/" { printf("\n Operator: %s",yytext);insert(yytext);}
  45. "}" { display();}
  46. "//"[^\n] { printf("\n Single line Comment: %s",yytext);insert(yytext);}
  47. "/*" { printf("\n Multi line Comment Starts: %s",yytext);insert(yytext);}
  48. "*/" { printf("\n Single line Comment Ends: %s",yytext);insert(yytext);}
  49. %%
  50.  
  51.  
  52. main()
  53. {
  54. FILE *file;
  55. file=fopen("code.c","r");
  56. yyin=file;
  57. yylex();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement