Guest User

Untitled

a guest
Nov 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NUMARG 4
  6. #define FLAG_ARG 1
  7. #define IN_FILE_ARG 2
  8. #define OUT_FILE_ARG 3
  9.  
  10. #define FLAG_F "-f"
  11. #define FLAG_V "-v"
  12. #define FLAG_B "-b"
  13.  
  14. #define LINEMAX 80
  15. #define ARRAY_MAX 100
  16.  
  17. typedef struct Node {
  18. /*Linked List Nodes*/
  19.  
  20. char* data;//Line of MIPS code
  21. struct Node *next;
  22.  
  23. } *nodePtr;//defines nodePtr as type struct Node*
  24.  
  25. struct ID{
  26. /*Individual identifiers, each point to the head of a linked list*/
  27. char flag;//either f or v
  28. char* identifier;
  29. nodePtr head;//points to the head of a linked list of the lines of MAL codes that have the identifier
  30. };
  31. struct ID* idList[ARRAY_MAX];//Creates a list of ID stucts to store each identifier
  32. int listSize = 0;
  33.  
  34.  
  35. void printList(nodePtr head_ref);
  36. struct ID* newID(char s[], char f);
  37. nodePtr newNode ( char s[]);
  38. void addID( struct ID* id);
  39. void addNode(nodePtr* head_ref, nodePtr new);
  40. void addToID(char* id, char b[]);
  41. void addNode(nodePtr* head_ref, nodePtr new);
  42. struct ID* idLookup(char* s);
  43. void print();
  44. void destroyList();
  45.  
  46.  
  47. int main(){
  48. newID("Array", 'v');
  49. newID("Bool", 'v');
  50. newID("Char", 'f');
  51. newID("Int", 'f');
  52. addToID("Array", "one skdgbzkdjbvaksfuhqowifhalsdknf");
  53. addToID("Bool", "two askdvgaeofqw");
  54. addToID("Char", "threeasdfhavfjkhgwoiufw");
  55. addToID("Int", "four asdkhfgasjfgaekhbf");
  56. addToID("Array", "five dhafsifugeoih");
  57. addToID("Int", "six kasdbgkd, skdfhasj");
  58.  
  59. print();
  60. destroyList();
  61.  
  62. FILE *inFile;
  63. FILE *outFile;
  64. struct ID idList[ARRAY_MAX];//Creates a list of ID stucts to store each identifier
  65. char flag = argv[1][1];//collects flag from CL Arg
  66.  
  67.  
  68.  
  69. if ((inFile =fopen(argv[IN_FILE_ARG], "r"))==NULL){
  70. //inputFile did not open correctly
  71. fprintf(stderr, "Could not open file %s for readingn", argv[IN_FILE_ARG]);
  72. exit(1);
  73. }
  74. else{
  75.  
  76. if ((outFile =fopen(argv[OUT_FILE_ARG], "w"))==NULL){
  77. //outputFile did not open correctly
  78. fprintf(stderr, "Could not open file %s for writingn", argv[OUT_FILE_ARG]);
  79. exit(1);
  80. }
  81. else{
  82. char line[LINEMAX];
  83. char* token;
  84. while (fgets(line, LINEMAX, inFile)!=NULL){
  85. token = strtok(line, " t,:nr");
  86. while(token!=NULL){
  87. (strstr(token, ".text"));
  88.  
  89.  
  90. }
  91. }
  92. switch(flag){
  93. case 'v':
  94. break;
  95. case 'f':
  96. break;
  97. case 'b':
  98. break;
  99. default:
  100. fprintf(stderr, "INVALID ARGUMENT FLAGn", );
  101.  
  102. }
  103. }
  104. }
  105. }
  106. struct ID* newID(char s[], char f){
  107. //printf("newID Testn");
  108. struct ID * i = (struct ID *) malloc (sizeof (struct ID));
  109. i->identifier = (char*) malloc (sizeof(char)*strlen(s));
  110. strcpy(i->identifier, s);
  111. i->flag = f;
  112. i->head = NULL;
  113. addID(i);
  114. return i;
  115. }
  116.  
  117. nodePtr newNode ( char s[])
  118. {
  119. //printf("newNode Testn");
  120. /*this function takes an int and a string and initalizes a node that
  121. contains these parameters
  122. */
  123. nodePtr listNode = (struct Node *) malloc (sizeof (struct Node));
  124. listNode->data = (char*) malloc (sizeof(char)*strlen(s));
  125.  
  126.  
  127.  
  128. strcpy(listNode->data, s);
  129.  
  130. listNode->next = NULL;
  131.  
  132. return listNode;
  133. }
  134. void addID( struct ID* id ){
  135. printf("add Testn");
  136. fflush(stdout);
  137.  
  138. struct ID* temp = id;
  139.  
  140. idList[listSize] = temp;
  141.  
  142. listSize++;
  143. }
  144.  
  145.  
  146.  
  147.  
  148. void addToID(char* id, char* b){//adds a string to the liked list associated with ID
  149. struct ID* a = idLookup(id);
  150. printf("addToID Testn");
  151. nodePtr temp = newNode(b);
  152. addNode(&(a->head), temp);
  153. }
  154. void addNode(nodePtr* head_ref, nodePtr new){//adds node onto liked list
  155. printf("addNode Testn");
  156. fflush(stdout);
  157. nodePtr temp = *head_ref;
  158. if(temp == NULL){
  159.  
  160. new->next = temp;
  161. *head_ref = new;
  162. }
  163. else{
  164.  
  165. while(temp->next != NULL){
  166. temp = temp->next;
  167. }
  168. temp->next = new;
  169.  
  170. }
  171. }
  172. struct ID* idLookup(char* s){//checks the idList to see if the identifier has been used before
  173. if(idList[0]==NULL){
  174. return NULL;
  175. }
  176. else
  177. {
  178.  
  179.  
  180. //printf("IDLookup Testn");
  181. int i=0;
  182. while(i<=listSize){
  183. if(strcmp(idList[i]->identifier,s) == 0){
  184.  
  185. return idList[i];
  186. }
  187. i++;
  188. }
  189. return NULL;
  190. }
  191. }
  192. void print(){//Prints idList with corresponding linked lists
  193.  
  194. int i= 0;
  195. while(i<listSize){
  196.  
  197. if(idList[i]->flag == 'f'){
  198. printf( "Flow Control ID--%s--n", idList[i]->identifier);
  199. fflush(stdout);
  200. if(idList[i]->head != NULL){
  201. printList(idList[i]->head);
  202. }
  203. i++;
  204. }
  205. else{
  206. printf( "Variable ID--%s--n", idList[i]->identifier);
  207. fflush(stdout);
  208. if(idList[i]->head != NULL){
  209. printList(idList[i]->head);
  210. }
  211.  
  212. i++;
  213. }
  214. }}
  215.  
  216. void printList(nodePtr head_ref){//prints a linked lists
  217. nodePtr tmp = head_ref;
  218. while (tmp != NULL)
  219. {
  220. printf ("%sn", tmp->data);
  221. fflush (stdout);
  222. tmp = tmp->next;
  223. }
  224. }
  225. void destroyList ()
  226. {
  227. int i;
  228. for(i=0;i<listSize;i++){
  229.  
  230.  
  231. nodePtr curr = idList[i]->head;
  232. nodePtr next;
  233. while (curr != NULL)
  234. {
  235. next = curr->next;
  236. free (curr);
  237. curr = next;
  238. }
  239.  
  240.  
  241. printf ("Destroyedn");
  242. fflush (stdout);
  243. }
  244.  
  245.  
  246. }
Add Comment
Please, Sign In to add comment