Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.71 KB | None | 0 0
  1. %{
  2.    
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define COUNT_PREDEF 33
  7.  
  8. char PREDEF_TABLE[][20]  = {
  9.     "ID", "CONST", "#include", "<iostream>", "using", "namespace", "std", "int", "main", "float", "return",
  10.     "cin", "cout", "while", "if", "+", ";", ",", "{", "}", "[", "]", "(", ")", "-", "%", "/", ">>", "<<",
  11.     "=", "<", ">", "*"
  12. };
  13.  
  14. /**
  15.  * Function for searching an element into the predef table
  16.  * @param elem - element to be found
  17.  * @return: the position if the element exists, -1 otherwise
  18.  **/
  19. int findInPredef(char* elem){
  20.     for(int i = 0; i<COUNT_PREDEF; i++){
  21.         if(strcmp(PREDEF_TABLE[i], elem) == 0){
  22.             return i;
  23.         }
  24.     }
  25.     return -1;
  26. }
  27.  
  28. /**
  29.  * Struct for table entry
  30.  * @param hash: hashCode of element
  31.  * @param key: element's key
  32.  * @param value: element's value
  33.  **/
  34. struct TableEntry{
  35.     int hash;
  36.     int key;
  37.     char* value;
  38. };
  39. typedef struct TableEntry TableEntry;
  40.  
  41. /**
  42.  * Structure for HashTable - open addressing
  43.  * @param DEFAULT_SIZE - starting size of the hashTable
  44.  * @param EMPTY_VALUE - empty value of the hashTable
  45.  * @param DELETED_VALUE - deleted value of the hashTable
  46.  * @param LOAD_FACTOR - load factor of the hashTable
  47.  * @param MIN_FACTOR - minimum load factor of the hashTable
  48.  * @param container - container to store TableEntries
  49.  **/
  50. struct HashTable{
  51.     int DEFAULT_SIZE;
  52.     TableEntry* EMPTY_VALUE;
  53.     TableEntry* DELETED_VALUE;
  54.     double LOAD_FACTOR;
  55.     double MIN_FACTOR;
  56.     int size;
  57.     int containerSize;
  58.     int deletedSize;
  59.     TableEntry** container;
  60. };
  61.  
  62. typedef struct HashTable HashTable;
  63.  
  64. /**
  65.  * Function to insert a new element into a hashtable
  66.  * @param table - table to insert into
  67.  * @param key - key of the element
  68.  * @param value - value of the element
  69.  * @return:
  70.  */
  71. int put(HashTable* table, int key, char* value);
  72.  
  73. /**
  74.  * Function to delete an element from the HashTable
  75.  * @param key - key to be deleted
  76.  * @return - 0 if the element is deleted, -1 it there is no element with that key
  77.  */
  78. int delete(HashTable* table, int key);
  79.  
  80. /**
  81.  * Function to calculate a hash based on a key
  82.  * @param key- key whose hash must be calculated
  83.  * For simplicty we'll consider hash(key) = key
  84.  **/
  85. int getHash(int key){
  86.     return key;
  87. }
  88.  
  89.  
  90. /**
  91.  * Function to create an empty TableEntry
  92.  * @return: pointer to TableEntry
  93.  **/
  94. TableEntry* createEmptyEntry(){
  95.     TableEntry* entry = (TableEntry*)malloc(sizeof(TableEntry));
  96.     entry->hash = -1;
  97.     entry->key = -1;
  98.     char* tmp = (char*)malloc(2* sizeof(char));
  99.     strcpy(tmp, "-1");
  100.     entry->value = tmp;
  101.     return entry;
  102. }
  103.  
  104. /**
  105.  * Function to create a deleted TableEntry
  106.  * @return: pointer TableEntry
  107.  */
  108. TableEntry* createDeletedEntry(){
  109.     TableEntry* entry = (TableEntry*)malloc(sizeof(TableEntry));
  110.     entry->hash = -2;
  111.     entry->key = -2;
  112.     char* tmp = (char*)malloc(2* sizeof(char));
  113.     strcpy(tmp, "-2");
  114.     entry->value = tmp;
  115.     return entry;
  116. }
  117.  
  118. /**
  119.  * Function to create an entry given a key and a value
  120.  * @param key - entry key
  121.  * @param value - entry value
  122.  */
  123. TableEntry* createEntry(int key, char* value){
  124.     TableEntry* tmp = (TableEntry*)malloc(sizeof(TableEntry));
  125.     tmp->key = key;
  126.     tmp->hash = getHash(key);
  127.     char* tmpChar = (char*)malloc(strlen(value)* sizeof(char));
  128.     strcpy(tmpChar, value);
  129.     tmp->value = tmpChar;
  130.     return tmp;
  131. }
  132.  
  133. /**
  134.  * Function to check if two table entries are equal
  135.  * @param e1 - first entry
  136.  * @param e2 - second entry
  137.  * @return - 1 if the entries are equal, 0 otherwise
  138.  */
  139. int equalsEntry(TableEntry* e1, TableEntry* e2){
  140.     if(0 == strcmp(e1->value, e2->value) && e1->key == e2->key && e1->hash == e2->hash){
  141.         return 1;
  142.     }
  143.     return 0;
  144. }
  145.  
  146.  
  147. /**
  148.  * Function to copy an entry
  149.  * @param entry - entry to be copied
  150.  * @return - new entry
  151.  */
  152. TableEntry* copyEntry(TableEntry* entry){
  153.     TableEntry* newEntry = (TableEntry*)malloc(sizeof(TableEntry));
  154.     newEntry->key = entry->key;
  155.     newEntry->hash = entry->hash;
  156.     char* tmp = (char*)malloc(sizeof(char)*strlen(entry->value));
  157.     strcpy(tmp, entry->value);
  158.     newEntry->value = tmp;
  159.     return newEntry;
  160. }
  161.  
  162. /**
  163.  * Function to print a TableEntry
  164.  * @param entry - enrey to be printed
  165.  */
  166. void printEntry(TableEntry* entry){
  167.     printf("%d : %s", entry->key, entry->value);
  168. }
  169.  
  170. /**
  171.  * Function to destroy a table entry
  172.  * @param tmp - TableEntry
  173.  **/
  174. void destroyEntry(TableEntry* tmp){
  175.     free(tmp);
  176. }
  177.  
  178. /**
  179.  * Function to create a HashTable
  180.  * @return: pointer to Hashtable
  181.  **/
  182. HashTable* createHashtable(){
  183.     HashTable* hashTable = (HashTable*)malloc(sizeof(HashTable));
  184.     hashTable->DEFAULT_SIZE = 8;
  185.     hashTable->DELETED_VALUE = createDeletedEntry();
  186.     hashTable->EMPTY_VALUE = createEmptyEntry();
  187.     hashTable->LOAD_FACTOR = (double)2/3;
  188.     hashTable->MIN_FACTOR = (double) 1/3;
  189.     hashTable->size = 0;
  190.     hashTable->containerSize = hashTable->DEFAULT_SIZE;
  191.     hashTable->deletedSize = 0;
  192.     TableEntry** tmp = (TableEntry**)malloc(hashTable->DEFAULT_SIZE*sizeof(TableEntry*));
  193.     for(int i = 0; i<hashTable->DEFAULT_SIZE; i++){
  194.         TableEntry* tmpEntry;
  195.         tmpEntry = createEmptyEntry();
  196.         tmp[i] = tmpEntry;
  197.     }
  198.     hashTable->container = tmp;
  199.     return hashTable;
  200. }
  201.  
  202. /**
  203.  * Function to check if the hashtable contains a given key
  204.  * @param key - key to be searched
  205.  * @param table - hashTable to be searched into
  206.  * @return - 1 if the key is in the hashtable, 0 otherwise
  207.  */
  208. int contains(HashTable* table, int key){
  209.     int hashCode = getHash(key);
  210.     int rootIndex = hashCode;
  211.     for(int offset = 0; offset < table->containerSize; ++offset){
  212.         int index = (rootIndex + offset) % table->containerSize;
  213.         TableEntry* element = table->container[index];
  214.         if(element->hash == hashCode && element->key == key){
  215.             return 1;
  216.         }
  217.     }
  218.     return 0;
  219. }
  220.  
  221. /**
  222.  * Function to get the insert position of a new element into the hashtable
  223.  * @param table - Hashtable to insert into
  224.  * @param key - key to insert
  225.  * @return: integer - representing the position to insert
  226.  */
  227. int getInsertPos(HashTable* table, int key){
  228.     int hashCode = getHash(key);
  229.     int rootIndex = hashCode;
  230.     for(int offset = 0; offset < table->containerSize; ++offset){
  231.         int index = (rootIndex + offset) % table->containerSize;
  232.         TableEntry* element = table->container[index];
  233.         if(1 == equalsEntry(element, table->EMPTY_VALUE)){
  234.             return index;
  235.         }
  236.     }
  237.     return -1;
  238. }
  239.  
  240. /**
  241.  * Function to copy the content of a hashTable into a new table
  242.  * @param dest - new table
  243.  * @param table - old table
  244.  */
  245. TableEntry**  copyContainer(HashTable* table){
  246.     TableEntry** newTabale = (TableEntry**)malloc(table->containerSize* sizeof(TableEntry*));
  247.     for(int i = 0; i<table->containerSize; i++){
  248.         newTabale[i] = copyEntry(table->container[i]);
  249.     }
  250.     return newTabale;
  251. }
  252.  
  253.  
  254. /**
  255.  * Function to resize a hashTable
  256.  * @param table - hashTable to be resized
  257.  */
  258. void resize(HashTable* table){
  259.     TableEntry** oldContainer = copyContainer(table);
  260.     int oldContainerSize = table->containerSize;
  261.     for(int i = 0; i<table->containerSize; ++i){
  262.         destroyEntry(table->container[i]);
  263.     }
  264.     free(table->container);
  265.     int oldSize = table->size;
  266.     table->containerSize = oldSize/table->MIN_FACTOR;
  267.     TableEntry** tmp = (TableEntry**)malloc(table->containerSize*sizeof(TableEntry*));
  268.     for(int i = 0; i<table->containerSize; i++){
  269.         tmp[i] = createEmptyEntry();
  270.     }
  271.     table->container = tmp;
  272.     for(int i = 0; i<oldContainerSize; i++){
  273.         if(0 == equalsEntry(oldContainer[i], table->DELETED_VALUE) && 0 == equalsEntry(oldContainer[i], table->EMPTY_VALUE)) {
  274.             put(table, oldContainer[i]->key, oldContainer[i]->value);
  275.         }
  276.         destroyEntry(oldContainer[i]);
  277.     }
  278.     free(oldContainer);
  279. }
  280.  
  281. /**
  282.  * Function to insert a new element into a hashtable
  283.  * @param table - table to insert into
  284.  * @param key - key of the element
  285.  * @param value - value of the element
  286.  * @return:
  287.  */
  288. int put(HashTable* table, int key, char* value){
  289.     if(0 == contains(table, key)){
  290.         int index = getInsertPos(table, key);
  291.         if(-1 != index){
  292.             table->container[index] = createEntry(key, value);
  293.             table->size ++;
  294.         }
  295.     }
  296.     double factor = (double)(table->deletedSize + table->size) / table->containerSize;
  297.     if(factor > table->LOAD_FACTOR){
  298.         resize(table);
  299.     }
  300. }
  301.  
  302. /**
  303.  * Function to delete an element from a hashTable
  304.  * @param table - table to delete from
  305.  * @param key - key to be deleted
  306.  */
  307. int delete(HashTable* table, int key){
  308.     if(1 == contains(table, key)){
  309.        int index = -1;
  310.        for(int i = 0; i<table->containerSize; i++){
  311.            if(table->container[i]->key == key){
  312.                index = i;
  313.                break;
  314.            }
  315.        }
  316.        if(index != -1){
  317.            table->container[index] = table->DELETED_VALUE;
  318.            table->size --;
  319.            table->deletedSize ++;
  320.        }
  321.     }
  322.     return -1;
  323. }
  324.  
  325.  
  326. /**
  327.  * Function to print a hashtable
  328.  * @param table - table to be printed
  329.  */
  330. void printTable(HashTable* table){
  331.     printf("{\n");
  332.     for(int i = 0; i<table->containerSize; i++){
  333.         if(0 == equalsEntry(table->container[i], table->EMPTY_VALUE) && 0 == equalsEntry(table->container[i], table->DELETED_VALUE)) {
  334.             printEntry(table->container[i]);
  335.             printf("\n");
  336.         }
  337.     }
  338.     printf("}");
  339. }
  340.  
  341.  
  342. /**
  343.  * Function to destroy a hashTable
  344.  * @param table - HashTable to be destroyed
  345.  **/
  346. void destroyHashtable(HashTable* table){
  347.     for(int i = 0; i<table->size; i++){
  348.         destroyEntry(table->container[i]);
  349.     }
  350.     destroyEntry(table->EMPTY_VALUE);
  351.     destroyEntry(table->DELETED_VALUE);
  352.     free(table);
  353. }
  354.  
  355.  
  356. /**
  357.  * Function to calculate the ascii sum of a string
  358.  * @param str - string whose ascii sum we need to calculate
  359.  * @return - integer representing ascii sum of str characters
  360.  */
  361. int asciiSum(char* str){
  362.     int sum = 0;
  363.     for(int i = 0; i<strlen(str); ++i){
  364.         sum += (str[i] - 'a') + 97;
  365.     }
  366.     return sum;
  367. }
  368.  
  369. enum{INTEGER, FLOAT, IDENTIFIER, CONSTANT};
  370.  
  371. %}
  372.  
  373. digit [0-9]*
  374. integer 0|([1-9])[0-9]*
  375. character [a-zA-Z]
  376.  
  377.  
  378. %%
  379. {integer}|{integer}\.{digit}+ {return CONSTANT;}
  380. {character}|{character}({character}{digit})* {return IDENTIFIER;}
  381. %%
  382.  
  383. int yywrap(){
  384.     return 1;
  385. }
  386.  
  387. main(){
  388.     int result;
  389.     int running = 1;
  390.     while(running){
  391.         result = yylex();  
  392.         switch(result){
  393.             case CONSTANT: printf("constant"); break;
  394.             case IDENTIFIER: printf("identifier"); break;
  395.         }
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement