![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
skeleton for lab1
Dependencies: AvailableMemory mbed-rtos mbed
Fork of helloaabbc by
Parser.cpp@1:55e99f6e2aa5, 2014-04-04 (annotated)
- Committer:
- mbed36372
- Date:
- Fri Apr 04 21:31:22 2014 +0000
- Revision:
- 1:55e99f6e2aa5
- Parent:
- 0:1c8f2727e9f5
SP14_lab1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y7jin | 0:1c8f2727e9f5 | 1 | #include "Parser.h" |
y7jin | 0:1c8f2727e9f5 | 2 | |
y7jin | 0:1c8f2727e9f5 | 3 | void Parser::parseSDFG(char *path, SDFG *sdfg){ |
y7jin | 0:1c8f2727e9f5 | 4 | // printf("Parsing SDF config... ", path); // Drive should be marked as removed |
y7jin | 0:1c8f2727e9f5 | 5 | FILE *fp=fopen(path, "r"); |
y7jin | 0:1c8f2727e9f5 | 6 | if(!fp) { |
y7jin | 0:1c8f2727e9f5 | 7 | error("ERROR: no file located at \"%s\"\r\n",path); |
y7jin | 0:1c8f2727e9f5 | 8 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 9 | } |
y7jin | 0:1c8f2727e9f5 | 10 | else |
y7jin | 0:1c8f2727e9f5 | 11 | { |
y7jin | 0:1c8f2727e9f5 | 12 | // printf("File opened!\n\r"); |
y7jin | 0:1c8f2727e9f5 | 13 | } |
y7jin | 0:1c8f2727e9f5 | 14 | int n1=-1, n2=-1; |
y7jin | 0:1c8f2727e9f5 | 15 | char line[1024], delim[]=" ", *cur=NULL, symbol; |
y7jin | 0:1c8f2727e9f5 | 16 | for(int i=0; i<1024; i++)line[i]=0; |
y7jin | 0:1c8f2727e9f5 | 17 | int nodeId=0; |
y7jin | 0:1c8f2727e9f5 | 18 | while(fgets(line, 1024, fp)!=NULL){ |
y7jin | 0:1c8f2727e9f5 | 19 | int temp, params[256], count=0; |
y7jin | 0:1c8f2727e9f5 | 20 | cur=strtok(line, delim); |
y7jin | 0:1c8f2727e9f5 | 21 | if(n1<0 && n2<0){ |
y7jin | 0:1c8f2727e9f5 | 22 | n1=atoi(cur); |
y7jin | 0:1c8f2727e9f5 | 23 | cur=strtok(NULL,delim); |
y7jin | 0:1c8f2727e9f5 | 24 | n2=atoi(cur); |
y7jin | 0:1c8f2727e9f5 | 25 | sdfg->setNumOfNodes(n1); |
y7jin | 0:1c8f2727e9f5 | 26 | sdfg->setNumOfFIFOs(n2); |
y7jin | 0:1c8f2727e9f5 | 27 | }else if(strlen(cur)!=1){ |
y7jin | 0:1c8f2727e9f5 | 28 | error("BAD SDFG FORMAT\r\n"); |
y7jin | 0:1c8f2727e9f5 | 29 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 30 | }else{ |
y7jin | 0:1c8f2727e9f5 | 31 | symbol = cur[0]; |
y7jin | 0:1c8f2727e9f5 | 32 | for(int i=0; i<256; i++)params[i]=0; |
y7jin | 0:1c8f2727e9f5 | 33 | count=0; |
y7jin | 0:1c8f2727e9f5 | 34 | cur=strtok(NULL,delim); |
y7jin | 0:1c8f2727e9f5 | 35 | while(cur){ |
y7jin | 0:1c8f2727e9f5 | 36 | temp=atoi(cur); |
y7jin | 0:1c8f2727e9f5 | 37 | params[count++]=temp; |
y7jin | 0:1c8f2727e9f5 | 38 | cur=strtok(NULL,delim); |
y7jin | 0:1c8f2727e9f5 | 39 | } |
y7jin | 0:1c8f2727e9f5 | 40 | switch (symbol){ |
y7jin | 0:1c8f2727e9f5 | 41 | case 'I': sdfg->getINode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 42 | break; |
y7jin | 0:1c8f2727e9f5 | 43 | case 'O': sdfg->getONode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 44 | break; |
y7jin | 0:1c8f2727e9f5 | 45 | case 'A': sdfg->getANode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 46 | break; |
y7jin | 0:1c8f2727e9f5 | 47 | case 'S': sdfg->getSNode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 48 | break; |
y7jin | 0:1c8f2727e9f5 | 49 | case 'M': sdfg->getMNode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 50 | break; |
y7jin | 0:1c8f2727e9f5 | 51 | case 'D': sdfg->getDNode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 52 | break; |
y7jin | 0:1c8f2727e9f5 | 53 | case 'U': sdfg->getUNode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 54 | break; |
y7jin | 0:1c8f2727e9f5 | 55 | case 'C': sdfg->getCNode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 56 | break; |
y7jin | 0:1c8f2727e9f5 | 57 | case 'F': sdfg->getFNode(nodeId++, params, count); |
y7jin | 0:1c8f2727e9f5 | 58 | break; |
y7jin | 0:1c8f2727e9f5 | 59 | case 'E': sdfg->addDelay(params, count); |
y7jin | 0:1c8f2727e9f5 | 60 | break; |
y7jin | 0:1c8f2727e9f5 | 61 | default: error("ERROR: BAD SDFG FORMAT\r\n"); |
y7jin | 0:1c8f2727e9f5 | 62 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 63 | break; |
y7jin | 0:1c8f2727e9f5 | 64 | } |
y7jin | 0:1c8f2727e9f5 | 65 | } |
y7jin | 0:1c8f2727e9f5 | 66 | for(int i=0; i<256; i++)params[i]=0; |
y7jin | 0:1c8f2727e9f5 | 67 | for(int i=0; i<1024; i++)line[i]=0; |
y7jin | 0:1c8f2727e9f5 | 68 | } |
y7jin | 0:1c8f2727e9f5 | 69 | //printf("Closing File...\r\n"); |
y7jin | 0:1c8f2727e9f5 | 70 | fclose(fp); |
y7jin | 0:1c8f2727e9f5 | 71 | sdfg->buildTopologyMatrix(); |
y7jin | 0:1c8f2727e9f5 | 72 | } |
y7jin | 0:1c8f2727e9f5 | 73 | |
y7jin | 0:1c8f2727e9f5 | 74 | void Parser::parseInput(char *path, RingBuffer *buf){ |
y7jin | 0:1c8f2727e9f5 | 75 | //printf("Parsing input...\r\n"); |
y7jin | 0:1c8f2727e9f5 | 76 | FILE *fp=fopen(path, "r"); |
y7jin | 0:1c8f2727e9f5 | 77 | if(!fp){ |
y7jin | 0:1c8f2727e9f5 | 78 | error("ERROR: no file located at \"%s\"\r\n",path); |
y7jin | 0:1c8f2727e9f5 | 79 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 80 | } |
y7jin | 0:1c8f2727e9f5 | 81 | int count=0; |
y7jin | 0:1c8f2727e9f5 | 82 | int N=0; |
y7jin | 0:1c8f2727e9f5 | 83 | char line[20]; |
y7jin | 0:1c8f2727e9f5 | 84 | for(int i=0; i<20; i++)line[i]=0; |
y7jin | 0:1c8f2727e9f5 | 85 | while(fgets(line, 20, fp)!=NULL){ |
y7jin | 0:1c8f2727e9f5 | 86 | if(count==0){ |
y7jin | 0:1c8f2727e9f5 | 87 | N=atoi(line); |
y7jin | 0:1c8f2727e9f5 | 88 | if(N<0){ |
y7jin | 0:1c8f2727e9f5 | 89 | error("ERROR: BAD INPUT FORMAT\r\n"); |
y7jin | 0:1c8f2727e9f5 | 90 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 91 | } |
y7jin | 0:1c8f2727e9f5 | 92 | }else{ |
y7jin | 0:1c8f2727e9f5 | 93 | buf->insert(atoi(line)); |
y7jin | 0:1c8f2727e9f5 | 94 | } |
y7jin | 0:1c8f2727e9f5 | 95 | count++; |
y7jin | 0:1c8f2727e9f5 | 96 | for(int i=0; i<20; i++) line[i]=0; |
y7jin | 0:1c8f2727e9f5 | 97 | } |
y7jin | 0:1c8f2727e9f5 | 98 | fclose(fp); |
y7jin | 0:1c8f2727e9f5 | 99 | if(N!=count-1){ |
y7jin | 0:1c8f2727e9f5 | 100 | error("ERROR: BAD INPUT FORMAT\r\n"); |
y7jin | 0:1c8f2727e9f5 | 101 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 102 | } |
y7jin | 0:1c8f2727e9f5 | 103 | else |
y7jin | 0:1c8f2727e9f5 | 104 | { |
y7jin | 0:1c8f2727e9f5 | 105 | // printf("File closed.\n\r"); |
y7jin | 0:1c8f2727e9f5 | 106 | } |
y7jin | 0:1c8f2727e9f5 | 107 | } |