Advertisement
Guest User

15

a guest
Jul 6th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1.  
  2.  
  3. %{
  4.  
  5. #include<stdlib.h>
  6. #include<stdio.h>
  7. #include<string.h>
  8.  
  9. struct sym
  10. {
  11. char name [10];
  12. }STAB[20];
  13. int m=0,k=0;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. %}
  21.  
  22. %%
  23. "#include"[ \t]*"<"|\"[ \t]*[^\n"][ \t]*">"|\" { printf("\n #include found: %s",yytext);insert(yytext); }
  24.  
  25. "(" { printf("\n Parenthesis: %s",yytext);}
  26. ")" { printf("\n Parenthesis: %s",yytext);}
  27. [0-9] { printf("\n Number: %s",yytext);}
  28. "int"|"float"|"char"|"double"|"main" { printf("\n Keyword: %s",yytext);insert(yytext);}
  29. "," { printf("\n Comma: %s",yytext);}
  30. ";" { printf("\n Semicolon: %s",yytext);}
  31. "::" { printf("\n Scope resolution: %s",yytext);}
  32. "*"|"+"|"-"|"/" { printf("\n Operator: %s",yytext);}
  33. [
  34. "//"[^\n] { printf("\n Single line Comment: %s",yytext);}
  35. "/*" { printf("\n Multi line Comment Starts: %s",yytext);}
  36. "*/" { printf("\n Single line Comment Ends: %s",yytext);}
  37. %%
  38.  
  39.  
  40. int insert(char *yytext)
  41. {
  42. int i=0;
  43. int l,k;
  44.  
  45. for(l=0; l<=m;l++)
  46. {
  47.  
  48. if(strcmp(STAB[l].name,yytext)==0)
  49. {
  50.  
  51. i=1;
  52. break;
  53. }
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60. if(i==0)
  61. {
  62. printf("\nIdentifier not found. Inserting\n");
  63. strcpy(STAB[m].name,yytext);
  64. m++;
  65. }
  66. else
  67. printf("\nIdentifier found\n");
  68.  
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75. main()
  76. {
  77. FILE *file;
  78. file=fopen("code.c","r");
  79. yyin=file;
  80. yylex();
  81. printf("Symbol table is:\n");
  82.  
  83. for(k=0;k<m;k++)
  84. printf("%s\n",STAB[k].name);
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement