Guest User

Untitled

a guest
Dec 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.61 KB | None | 0 0
  1. %{
  2. #undef yywrap
  3. #define yywrap() 1
  4. #include <math.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #define CODE 3
  8. #define ID 2
  9. #define CONST 1
  10. #define ERROR 0
  11.  
  12. int code;
  13.  
  14. typedef struct{
  15.     int type;//CODE,ID,CONST
  16.     int code;
  17.     char* text;
  18. }Token;
  19.  
  20. typedef struct{
  21.     int size;
  22.     void* elems;
  23. }Vector;
  24.  
  25. %}
  26.  
  27. CONSTANT ["][a-zA-Z0-9 :\*><+!=>><<::\.]*["]
  28. IDENTIFIER [a-zA-Z][a-zA-Z0-9]{0,7}
  29. DIGIT [0-9]
  30. FLOAT [1-9][0-9]*\.*[0-9]*
  31.  
  32. %%
  33. "{"  {code = 2;return CODE;}
  34. "}"  {code = 3;return CODE;}
  35. "if"  {code = 51;return CODE;}
  36. "else"  {code = 52;return CODE;}
  37. "("  {code = 71;return CODE;}
  38. ")"  {code = 72;return CODE;}
  39. "<"  {code = 73;return CODE;}
  40. ">"  {code = 74;return CODE;}
  41. "="  {code = 75;return CODE;}
  42. ";"  {code = 76;return CODE;}
  43. "*"  {code = 77;return CODE;}
  44. "+"  {code = 78;return CODE;}
  45. "-"  {code = 79;return CODE;}
  46. "#include"  {code = 89;return CODE;}
  47. "using namespace"  {code = 81;return CODE;}
  48. "int main"  {code = 82;return CODE;}
  49. "int"  {code = 83;return CODE;}
  50. "double"  {code = 84;return CODE;}
  51. "const"  {code = 85;return CODE;}
  52. "while"  {code = 86;return CODE;}
  53. "cout"  {code = 87;return CODE;}
  54. "endl"  {code = 88;return CODE;}
  55. "return"  {code = 89;return CODE;}
  56. "!="  {code = 90;return CODE;}
  57. "<<"  {code = 91;return CODE;}
  58. ">>"  {code = 92;return CODE;}
  59. "::"  {code = 93;return CODE;}
  60. ","  {code = 94;return CODE;}
  61. {CONSTANT} {code = 1;return CONST;}
  62. {DIGIT} {code = 1;return CONST;}
  63. {FLOAT} {code = 1;return CONST;}
  64. "0\."{DIGIT}+ {code =1 ;return CONST;}
  65. {IDENTIFIER} {code = 0;return ID;}
  66.  
  67. [ \t\n]+        /* eat up whitespace */
  68. {DIGIT}{IDENTIFIER} {printf("ERROR : %s",yytext);code = -1;return ERROR;}
  69. "0"{DIGIT}+ {printf("ERROR : %s",yytext);code = -1;return ERROR;}
  70. "0\." {printf("ERROR : %s",yytext);code = -1;return ERROR;}
  71. . {printf("ERROR : %s",yytext);code=-1;return ERROR;}
  72. %%
  73.  
  74. Vector* init_vector(){
  75.     Vector* vector = malloc(sizeof(Vector));
  76.     vector->elems = malloc(sizeof(Token)*300);
  77.     vector->size = 0;
  78.     return vector;
  79. }
  80.  
  81. int get_position(Vector* vector,char* text){
  82.     int position = 0;
  83.     while(position < vector->size){
  84.         if(strcmp(((Token*)vector->elems)[position].text,text)==0){
  85.             return ++position;
  86.         }
  87.         position++;
  88.     }
  89.     return -1;
  90. }
  91.  
  92. insert_vector(Vector* vector,int type){
  93.     if(get_position(vector,yytext)==-1){
  94.         ((Token*)vector->elems)[vector->size].type = type;
  95.         ((Token*)vector->elems)[vector->size].text = strdup(yytext);
  96.         ((Token*)vector->elems)[vector->size].code = code;
  97.         vector->size++;
  98.     }  
  99. }
  100.  
  101. sort_vector(Vector* vector){
  102.     int ok = 0,i=0,result = 0;
  103.     Token aux;
  104.     do{
  105.         ok=1;
  106.         for(i=0;i<vector->size-1;i++){
  107.             Token first =((Token*)vector->elems)[i];
  108.             Token second =((Token*)vector->elems)[i+1];
  109.             result = strcmp(first.text,second.text);
  110.             if(result>=0){
  111.                 ok = 0;
  112.                 aux.type = ((Token*)vector->elems)[i+1].type;
  113.                 aux.text = ((Token*)vector->elems)[i+1].text;
  114.                 aux.code = ((Token*)vector->elems)[i+1].code;
  115.  
  116.                 ((Token*)vector->elems)[i+1].type = ((Token*)vector->elems)[i].type;
  117.                 ((Token*)vector->elems)[i+1].text = ((Token*)vector->elems)[i].text;
  118.                 ((Token*)vector->elems)[i+1].code = ((Token*)vector->elems)[i].code;
  119.  
  120.                 ((Token*)vector->elems)[i].type = aux.type;
  121.                 ((Token*)vector->elems)[i].text = aux.text;
  122.                 ((Token*)vector->elems)[i].code = aux.code;
  123.             }
  124.         }
  125.     }while(ok !=1);
  126.  
  127. }
  128.  
  129. free_vector(Vector* vector){
  130.     int i =0;
  131.     for(i=0;i< vector->size;i++){
  132.         free(((Token*)vector->elems)[i].text);
  133.     }
  134.     free(vector->elems);
  135. }
  136.  
  137. main( argc, argv )
  138. int argc;
  139. char **argv;
  140. {
  141.     ++argv, --argc; /* skip over program name */
  142.     if ( argc > 0 )
  143.     yyin = fopen( argv[0], "r" );
  144.     else
  145.      yyin = stdin;
  146.     int result;
  147.     Vector* codes = init_vector();
  148.     Vector* TC = init_vector();
  149.     Vector* TS = init_vector();
  150.  
  151.     while ((result = yylex())) {
  152.         int index = codes->size;
  153.         Token* tokens = (Token*) codes->elems;
  154.         switch(result){
  155.             case CODE:
  156.                 // printf("token is CODE  text : %s code: %d\n",yytext,code);
  157.                 tokens[index].type = CODE;
  158.                 break;
  159.             case CONST:
  160.                 // printf("token is CONST  text : %s code: %d\n",yytext,code);
  161.                 tokens[index].type = CONST;
  162.                 insert_vector(TC,CONST);
  163.                 break;
  164.             case ID:
  165.                 // printf("token is ID  text : %s code: %d\n",yytext,code);
  166.                 tokens[index].type = ID;
  167.                 insert_vector(TS,ID);
  168.                 break;
  169.             case ERROR:
  170.                 printf("token is ERROR  text : %s code: %d\n",yytext,code);
  171.                 break;
  172.             default:
  173.                 printf("never should be here text : %s code: %d\n",yytext,code);
  174.             }
  175.             tokens[index].text = strdup(yytext);
  176.             tokens[index].code = code;
  177.             codes->size++;
  178.     }
  179.     if(code == -1){
  180.         return;
  181.     }
  182.     printf("yylex finished without error\n");
  183.     FILE* FIP = fopen("FIP","w");    
  184.     int i=0;
  185.     sort_vector(TC);
  186.     sort_vector(TS);
  187.     for(i=0;i<codes->size;i++){
  188.         Token* tokens = (Token*) codes->elems;
  189.             switch(tokens[i].type){
  190.                 case CODE:
  191.                     fprintf(FIP,"%d /\n",tokens[i].code);
  192.                     break;
  193.                 case CONST:
  194.                     fprintf(FIP,"%d %d\n",tokens[i].code,get_position(TC,tokens[i].text));
  195.                     break;
  196.                 case ID:
  197.                     fprintf(FIP,"%d %d\n",tokens[i].code,get_position(TS,tokens[i].text));
  198.                     break;
  199.             }
  200.     }
  201.     fclose(FIP);
  202.     printf("TC : ");
  203.     for(i=0;i<TC->size;i++){
  204.         Token token= ((Token*)TC->elems)[i];
  205.         printf("%s ,",token.text);
  206.     }
  207.     printf("\nTS : ");
  208.     for(i=0;i<TS->size;i++){
  209.         Token token= ((Token*)TS->elems)[i];
  210.         printf("%s ,",token.text);
  211.     }
  212.  
  213.     free_vector(codes);
  214.     free_vector(TC);
  215.     free_vector(TS);
  216. }
Add Comment
Please, Sign In to add comment