Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NUMARG 4
- #define FLAG_ARG 1
- #define IN_FILE_ARG 2
- #define OUT_FILE_ARG 3
- #define FLAG_F "-f"
- #define FLAG_V "-v"
- #define FLAG_B "-b"
- #define LINEMAX 80
- #define ARRAY_MAX 100
- typedef struct Node {
- /*Linked List Nodes*/
- char* data;//Line of MIPS code
- struct Node *next;
- } *nodePtr;//defines nodePtr as type struct Node*
- struct ID{
- /*Individual identifiers, each point to the head of a linked list*/
- char flag;//either f or v
- char* identifier;
- nodePtr head;//points to the head of a linked list of the lines of MAL codes that have the identifier
- };
- struct ID* idList[ARRAY_MAX];//Creates a list of ID stucts to store each identifier
- int listSize = 0;
- void printList(nodePtr head_ref);
- struct ID* newID(char s[], char f);
- nodePtr newNode ( char s[]);
- void addID( struct ID* id);
- void addNode(nodePtr* head_ref, nodePtr new);
- void addToID(char* id, char b[]);
- void addNode(nodePtr* head_ref, nodePtr new);
- struct ID* idLookup(char* s);
- void print();
- void destroyList();
- int main(){
- newID("Array", 'v');
- newID("Bool", 'v');
- newID("Char", 'f');
- newID("Int", 'f');
- addToID("Array", "one skdgbzkdjbvaksfuhqowifhalsdknf");
- addToID("Bool", "two askdvgaeofqw");
- addToID("Char", "threeasdfhavfjkhgwoiufw");
- addToID("Int", "four asdkhfgasjfgaekhbf");
- addToID("Array", "five dhafsifugeoih");
- addToID("Int", "six kasdbgkd, skdfhasj");
- print();
- destroyList();
- FILE *inFile;
- FILE *outFile;
- struct ID idList[ARRAY_MAX];//Creates a list of ID stucts to store each identifier
- char flag = argv[1][1];//collects flag from CL Arg
- if ((inFile =fopen(argv[IN_FILE_ARG], "r"))==NULL){
- //inputFile did not open correctly
- fprintf(stderr, "Could not open file %s for readingn", argv[IN_FILE_ARG]);
- exit(1);
- }
- else{
- if ((outFile =fopen(argv[OUT_FILE_ARG], "w"))==NULL){
- //outputFile did not open correctly
- fprintf(stderr, "Could not open file %s for writingn", argv[OUT_FILE_ARG]);
- exit(1);
- }
- else{
- char line[LINEMAX];
- char* token;
- while (fgets(line, LINEMAX, inFile)!=NULL){
- token = strtok(line, " t,:nr ");
- while(token!=NULL){
- (strstr(token, ".text"));
- }
- }
- switch(flag){
- case 'v':
- break;
- case 'f':
- break;
- case 'b':
- break;
- default:
- fprintf(stderr, "INVALID ARGUMENT FLAGn", );
- }
- }
- }
- }
- struct ID* newID(char s[], char f){
- //printf("newID Testn");
- struct ID * i = (struct ID *) malloc (sizeof (struct ID));
- i->identifier = (char*) malloc (sizeof(char)*strlen(s));
- strcpy(i->identifier, s);
- i->flag = f;
- i->head = NULL;
- addID(i);
- return i;
- }
- nodePtr newNode ( char s[])
- {
- //printf("newNode Testn");
- /*this function takes an int and a string and initalizes a node that
- contains these parameters
- */
- nodePtr listNode = (struct Node *) malloc (sizeof (struct Node));
- listNode->data = (char*) malloc (sizeof(char)*strlen(s));
- strcpy(listNode->data, s);
- listNode->next = NULL;
- return listNode;
- }
- void addID( struct ID* id ){
- printf("add Testn");
- fflush(stdout);
- struct ID* temp = id;
- idList[listSize] = temp;
- listSize++;
- }
- void addToID(char* id, char* b){//adds a string to the liked list associated with ID
- struct ID* a = idLookup(id);
- printf("addToID Testn");
- nodePtr temp = newNode(b);
- addNode(&(a->head), temp);
- }
- void addNode(nodePtr* head_ref, nodePtr new){//adds node onto liked list
- printf("addNode Testn");
- fflush(stdout);
- nodePtr temp = *head_ref;
- if(temp == NULL){
- new->next = temp;
- *head_ref = new;
- }
- else{
- while(temp->next != NULL){
- temp = temp->next;
- }
- temp->next = new;
- }
- }
- struct ID* idLookup(char* s){//checks the idList to see if the identifier has been used before
- if(idList[0]==NULL){
- return NULL;
- }
- else
- {
- //printf("IDLookup Testn");
- int i=0;
- while(i<=listSize){
- if(strcmp(idList[i]->identifier,s) == 0){
- return idList[i];
- }
- i++;
- }
- return NULL;
- }
- }
- void print(){//Prints idList with corresponding linked lists
- int i= 0;
- while(i<listSize){
- if(idList[i]->flag == 'f'){
- printf( "Flow Control ID--%s--n", idList[i]->identifier);
- fflush(stdout);
- if(idList[i]->head != NULL){
- printList(idList[i]->head);
- }
- i++;
- }
- else{
- printf( "Variable ID--%s--n", idList[i]->identifier);
- fflush(stdout);
- if(idList[i]->head != NULL){
- printList(idList[i]->head);
- }
- i++;
- }
- }}
- void printList(nodePtr head_ref){//prints a linked lists
- nodePtr tmp = head_ref;
- while (tmp != NULL)
- {
- printf ("%sn", tmp->data);
- fflush (stdout);
- tmp = tmp->next;
- }
- }
- void destroyList ()
- {
- int i;
- for(i=0;i<listSize;i++){
- nodePtr curr = idList[i]->head;
- nodePtr next;
- while (curr != NULL)
- {
- next = curr->next;
- free (curr);
- curr = next;
- }
- printf ("Destroyedn");
- fflush (stdout);
- }
- }
Add Comment
Please, Sign In to add comment