Advertisement
Guest User

Compiler

a guest
Jan 3rd, 2014
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 50.02 KB | None | 0 0
  1. /*
  2. Mark Klingberg
  3. Summer 2013
  4.  
  5. This program is a PL/0 compiler. PL/0 is a made up language similar to Pascal.
  6. This compiler takes a file containing PL/0 language as input, converts it into tokens, and
  7. then runs it on a PL/0 machine. The PL/0 machine is a simple stack machine.
  8. View the read me file for further details.
  9. */
  10.  
  11. ////readMe.txt
  12. //This program is a tiny PL/0 compiler. It takes PL/0 code as input and if the code is
  13. //syntactically correct it will display a message saying so as well any read or write commands
  14. //to the terminal. It prints the tokens to a file named lexerOutput.txt. It prints
  15. //the intermediate code to a file named parcerOutput.txt. It prints the virtual machine execution
  16. //trace to a file named output.txt. If you wish to print the any of the files above you can use
  17. //-l, -a, and -v to print them out respectively. Follow the instructions below for compile and run the
  18. //program on Eustis:
  19. //
  20. //  1. compile the program by typing: gcc main.c -o name
  21. //  2. run the program by typing: ./name name_of_input_file
  22. //  3. If you wish to see any of the output files run the program by typing:
  23. //      ./name name_of_input_file -l -a -v
  24. //
  25. //The order you type in the compiler directives is the order the files will print in. The name of the
  26. //input file must always the second entry in the list.
  27.  
  28. ////test0.txt
  29. //var x,w;
  30. //begin
  31. // x:=4;
  32. //if 5 > x then
  33. // x := x + 1
  34. //else
  35. // x := x + 2;
  36. //end.
  37.  
  38. ////test1.txt
  39. //const n = 13;
  40. //var i,h;
  41. //procedure sub;
  42. //  const k = 7;
  43. //  var j,h;
  44. //   begin
  45. //     j:=n;
  46. //     i:=1;
  47. //     h:=k;
  48. //   end;
  49. //  begin
  50. //   i:=3; h:=0;
  51. //   call sub;
  52. //  end.
  53.  
  54. ////test2.txt
  55. //const m = 7, n = 85;
  56. //var  i,x,y,z,q,r;
  57. //procedure mult;
  58. //   var a, b;
  59. //  begin
  60. //     a := x;  b := y; z := 0;
  61. //     while b > 0 do
  62. //     begin
  63. //        if odd x then z := z+a;
  64. //           a := 2*a;
  65. //           b := b/2;
  66. //     end
  67. //  end;
  68. //
  69. //begin
  70. //  x := m;
  71. //  y := n;
  72. //  call mult;
  73. //end.
  74.  
  75. ////test3.txt
  76. //var  x,result;
  77. //procedure fact;
  78. //  begin
  79. //     if x > 1 then
  80. //  begin
  81. //  result := result * x;
  82. //      x := x - 1;
  83. //  call fact;
  84. //  end;
  85. //  end;
  86. //
  87. //begin
  88. //  result := 1;
  89. //  x := 10;
  90. //  call fact;
  91. //  write result;
  92. //end.
  93.  
  94. ////error1.txt
  95. //var x;
  96. //begin
  97. // x := 5;
  98. //end
  99.  
  100. ////error2.txt
  101. //const = 1;
  102. //var x;
  103. //begin
  104. // x = 5;
  105. //end
  106.  
  107. ////error3.txt
  108. //const z 1;
  109. //var x;
  110. //begin
  111. // x = 5;
  112. //end
  113.  
  114. ////error4.txt
  115. //const z =;
  116. //var x;
  117. //begin
  118. // x = 5;
  119. //end
  120.  
  121. ////error5.txt
  122. //const z = 3
  123. //var x;
  124. //begin
  125. // x = 5;
  126. //end
  127.  
  128. ////error6.txt
  129. //begin
  130. // x = 5;
  131. //end
  132.  
  133. ////error7.txt
  134. //const x = 1;
  135. //begin
  136. // x = 5;
  137. //end
  138.  
  139. ////error8.txt
  140. //var y;
  141. //begin
  142. // y 5;
  143. //end
  144.  
  145. //error9.txt
  146. //const n = 13;
  147. //var i,h;
  148. //procedure sub;
  149. //  const k = 7;
  150. //  var j,h;
  151. //   begin
  152. //     j:=n;
  153. //     i:=1;
  154. //     h:=k;
  155. //   end;
  156. //  begin
  157. //   i:=3; h:=0;
  158. //   call ;
  159. //  end.
  160.  
  161. ////error10.txt
  162. //const n = 13;
  163. //var i,h;
  164. //procedure sub;
  165. //  const k = 7;
  166. //  var j,h;
  167. //   begin
  168. //     j:=n;
  169. //     i:=1;
  170. //     h:=k;
  171. //   end;
  172. //  begin
  173. //   i:=3; h:=0;
  174. //   call h;
  175. //  end.
  176.  
  177. ////error11.txt
  178. //var x,w;
  179. //begin
  180. // x:=4;
  181. //if 5 > x
  182. // x := x + 1
  183. //else
  184. // x := x + 2;
  185. //end.
  186.  
  187.  
  188. ////error12.txt
  189. //const m = 7, n = 85;
  190. //var  i,x,y,z,q,r;
  191. //procedure mult;
  192. //   var a, b;
  193. //  begin
  194. //     a := x;  b := y; z := 0;
  195. //     while b > 0
  196. //     begin
  197. //        if odd x then z := z+a;
  198. //           a := 2*a;
  199. //           b := b/2;
  200. //     end
  201. //  end;
  202. //
  203. //begin
  204. //  x := m;
  205. //  y := n;
  206. //  call mult;
  207. //end.
  208.  
  209. ////error13.txt
  210. //var  x,result;
  211. //procedure fact;
  212. //  begin
  213. //     if x > 1 then
  214. //  begin
  215. //  result := result * x;
  216. //      x := x - 1;
  217. //  call fact;
  218. //  end;
  219. //  end;
  220. //
  221. //begin
  222. //  result := 1;
  223. //  read x;
  224. //  call fact;
  225. //  write fact;
  226. //end.
  227.  
  228. ////error14.txt
  229. //var x,w;
  230. //begin
  231. // x:=4;
  232. //if 5 := x then
  233. // x := x + 1
  234. //else
  235. // x := x + 2;
  236. //end.
  237.  
  238. ////error15.txt
  239. //var x,w;
  240. //begin
  241. // x:=4;
  242. //if 5 > x then
  243. // x := (x + 1 * 2
  244. //else
  245. // x := x + 2;
  246. //end.
  247.  
  248. ////error16.txt
  249. //var x,w;
  250. //begin
  251. // x:= * + 1;
  252. //end.
  253.  
  254. ////error17.txt
  255. //var  x,result;
  256. //procedure fact;
  257. //  begin
  258. //     if x > 1 then
  259. //  begin
  260. //  result := result * x;
  261. //      x := x - 1;
  262. //  call fact;
  263. //  end;
  264. //  end;
  265. //
  266. //begin
  267. //  result := 1;
  268. //  read ;
  269. //  call fact;
  270. //  write result;
  271. //end.
  272.  
  273. ////error18.txt
  274. //var x;
  275. //begin
  276. // x := 5;
  277. //.
  278.  
  279.  
  280.  
  281. #include <stdio.h>
  282. #include <stdlib.h>
  283. #include <ctype.h>
  284. #include <string.h>
  285.  
  286. #define MAX_IDENTIFER_LENGTH 11
  287. #define MAX_NUMBER_LENGTH 5
  288. #define MAX_SYMBOL_TABLE_SIZE 1000
  289. #define MAX_CODE_TABLE_SIZE 1000
  290. #define CONSTANT 1
  291. #define VARIABLE 2
  292. #define PROCEDURE 3
  293. #define MAX_NAME_SIZE 11
  294. #define TRUE 1
  295. #define FALSE 0
  296. #define RESERVED_STACK_SIZE 4
  297. #define OP_LENGTH 3
  298. #define MAX_STACK_HEIGHT 2000
  299. #define MAX_CODE_LENGTH 500
  300. #define MAX_LEXI_LEVELS 3
  301.  
  302. typedef enum{
  303.     LIT = 1,
  304.     OPR,
  305.     LOD,
  306.     STO,
  307.     CAL,
  308.     INC,
  309.     JMP,
  310.     JPC,
  311.     SIOw,
  312.     SIOr
  313. } ISA;
  314.  
  315. typedef enum{
  316.     RET = 0,
  317.     NEG,
  318.     ADD,
  319.     SUB,
  320.     MUL,
  321.     DIV,
  322.     ODD,
  323.     MOD,
  324.     EQL,
  325.     NEQ,
  326.     LSS,
  327.     LEQ,
  328.     GTR,
  329.     GEQ
  330. } OPERATIONS;
  331.  
  332. typedef enum{
  333.     nulsym = 1,
  334.     identsym,
  335.     numbersym,
  336.     plussym,
  337.     minussym,
  338.     multsym,
  339.     slashsym,
  340.     oddsym,
  341.     eqlsym,
  342.     neqsym,
  343.     lessym,
  344.     leqsym,
  345.     gtrsym,
  346.     geqsym,
  347.     lparentsym,
  348.     rparentsym,
  349.     commasym,
  350.     semicolonsym,
  351.     periodsym,
  352.     becomessym,
  353.     beginsym,
  354.     endsym,
  355.     ifsym,
  356.     thensym,
  357.     whilesym,
  358.     dosym,
  359.     callsym,
  360.     constsym,
  361.     varsym,
  362.     procsym,
  363.     writesym,
  364.     readsym,
  365.     elsesym
  366. } token_type;
  367.  
  368. typedef enum {
  369.     ERR_PER_EXP,
  370.     ERR_IDENT_MISSING,
  371.     ERR_EQUAL_MISSING,
  372.     ERR_NUMBER_MISSING,
  373.     ERR_SEMI_COMMA_MISSING,
  374.     ERR_UNDECL_IDENT,
  375.     ERR_ASSIGN_NOT_ALLOWED,
  376.     ERR_ASSIGN_EXPECT,
  377.     ERR_IDENT_EXPECTED,
  378.     ERR_BAD_CALL,
  379.     ERR_THEN_EXPECTED,
  380.     ERR_DO_EXPECTED,
  381.     ERR_EXPRESS_ERROR,
  382.     ERR_REL_EXPECTED,
  383.     ERR_PAREN_EXPECTED,
  384.     ERR_BAD_SYMBOL,
  385.     ERR_ID_EXPECT_AFTER_READWRITE,
  386.     ERR_END_EXPECTED,
  387.     ERR_REPEAT
  388. } error_type;
  389.  
  390. typedef struct lexemeList{
  391.     int token;
  392.     int number; //use if token == 3;
  393.     char name[100]; //use if token == 2;
  394.     struct lexemeList* next;
  395. }lexemeList;
  396.  
  397. typedef struct node{
  398.     char procName[MAX_NAME_SIZE + 1];
  399.     struct node* next;
  400. } node;
  401.  
  402. typedef struct{
  403.     int kind;       // const = 1, var = 2, proc = 3
  404.     char name[MAX_NAME_SIZE + 1];   // name up to 11 chars
  405.     int val;        // number (ASCII value)
  406.     int level;      // L level
  407.     int addr;       // M address
  408.     node* head;     // Used to check to see if a variable/constant can be accessed
  409. } symbol;
  410.  
  411. typedef struct{
  412.     int op;
  413.     int l;
  414.     int m;
  415. }code;
  416.  
  417. typedef struct instruction{
  418.     int line; //line number
  419.     int op; //opcode
  420.     int l; //L
  421.     int m; //M
  422. }instruction;
  423.  
  424. int lexerMain(char** argv);
  425. int parcerMain();
  426. int virtualMachineMain();
  427. void runLexicalAnalyzer(char** argv);
  428. void getIDorRWtoken(FILE* ifp);
  429. int checkForReservedWord(char* word);
  430. void getNumber(FILE* ifp);
  431. void getSymbol(FILE* ifp);
  432. void printLexemeList();
  433. void initialTokenPrint();
  434. void initialSymbolPrint();
  435. void getsym(FILE* ifp);
  436. void getname(FILE* ifp);
  437. void getvalue(FILE* ifp);
  438. void enter(int offset);
  439. void error(int e);
  440. void gen(int OP, int L, int M);
  441. void PROGRAM(FILE* ifp);
  442. void BLOCK(FILE* ifp);
  443. void STATEMENT(FILE* ifp);
  444. void CONDITION(FILE* ifp);
  445. void EXPRESSION(FILE* ifp);
  446. void addFirstNode();
  447. void printParcerOutput();
  448. void printOutput();
  449. void initializeTables();
  450. void printCodeTable();
  451. int checkVariableAccess(int i);
  452. int RELATION(int token);
  453. void TERM();
  454. void FACTOR();
  455. void storeToMemory(FILE* ifp);
  456. void printAssembly(FILE* ofp);
  457. void runVM(FILE* ofp);
  458. void fetch();
  459. void execute(FILE* ofp);
  460. void initializeVM(FILE* ofp);
  461. void initialPrint(FILE* ofp);
  462. void ALUop();
  463. int base(int l);
  464. void print(FILE* ofp);
  465. void printStack(FILE* ofp);
  466. void checkRepeat();
  467. void testPrint();
  468. node* generateLinkedList();
  469.  
  470. lexemeList* head = NULL;
  471. lexemeList* tail = NULL;
  472. lexemeList* tailTrailer = NULL;
  473. char c; //current charactering being read
  474. node* Head;
  475. symbol symbol_table[MAX_SYMBOL_TABLE_SIZE];
  476. code code_table[MAX_CODE_TABLE_SIZE];
  477. int TOKEN;
  478. int Kind;
  479. char Name[MAX_NAME_SIZE + 1];
  480. int Value;
  481. int Level;
  482. int TableIndex;
  483. int CodeIndex;
  484. char CurrentProcedure[MAX_NAME_SIZE + 1];
  485. int Stack[MAX_STACK_HEIGHT];
  486. int SP;
  487. int BP;
  488. int PC;
  489. instruction IR;
  490. int memory[MAX_CODE_LENGTH][4]; //[Line, IR.op, IR.l, IR.m]
  491. int totalLinesOfCode;
  492. //used for printing, stores the length of each activation record
  493. int ARLengthStroage[1000];
  494. int ARLengthStroagePointer;
  495. //used to exit the fetch execute cycle
  496. int numOfCal;//number of function calls
  497. int numOfOpr;//number of returns
  498.  
  499. int main(int argc, char** argv){
  500.  
  501.     int i;
  502.  
  503.     lexerMain(argv);
  504.     parcerMain();
  505.  
  506. //    testPrint();
  507.  
  508.     virtualMachineMain();
  509.  
  510.     for(i=1; i<argc; i++){
  511.  
  512.         if(strcmp(argv[i], "-l") == 0){
  513.  
  514.             //print the list of lexems (lexer output)
  515.             initialTokenPrint();
  516.             initialSymbolPrint();
  517.         }
  518.         else if(strcmp(argv[i], "-a") == 0){
  519.  
  520.             //print the generated assembly code (parcer output)
  521.             printParcerOutput();
  522.         }
  523.         else if(strcmp(argv[i], "-v") == 0){
  524.  
  525.                 //print the VM output ot the console
  526.             printOutput();
  527.         }
  528.     }
  529.  
  530.     return 0;
  531. }
  532.  
  533. int lexerMain(char** argv){
  534.  
  535.     runLexicalAnalyzer(argv);
  536.  
  537.     printLexemeList();
  538.  
  539.     return 0;
  540. }
  541.  
  542. void runLexicalAnalyzer(char** argv){
  543.  
  544.     FILE* ifp;
  545.     ifp = fopen(argv[1], "r");
  546.     if(ifp == NULL){
  547.         printf("failed to open input file.\n");
  548.         exit(-1);
  549.     }
  550.  
  551.     tail = malloc(sizeof(lexemeList));
  552.     head = tail;
  553.  
  554.     c = fgetc(ifp);
  555.     while(c != EOF){
  556.  
  557.         if(isalpha(c) != 0){
  558.             //c is part of an identifier or reserved word
  559.             getIDorRWtoken(ifp);
  560.         }
  561.         else if(isdigit(c) != 0){
  562.             //c is part of a number, or bad identifier
  563.             getNumber(ifp);
  564.         }
  565.         else{
  566.             //c is a special symbol, invisible character, comment, or error
  567.             getSymbol(ifp);
  568.         }
  569.     }
  570.  
  571.     tailTrailer->next = NULL;
  572.     free(tailTrailer->next);
  573.  
  574. }
  575.  
  576. void getIDorRWtoken(FILE* ifp){
  577.  
  578.     int numberFoundFlag=0, counter=0, RWflag=0, i;
  579.     char buffer[100];
  580.  
  581.     for(i=0; i<100; i++)
  582.         buffer[i] = '\0';
  583.  
  584.     buffer[counter] = c;
  585.     c = fgetc(ifp);
  586.  
  587.     while((isalpha(c) != 0) || (isdigit(c) != 0)){
  588.  
  589.         if (isdigit(c) != 0)
  590.             numberFoundFlag = 1;
  591.  
  592.         counter++;
  593.  
  594.         buffer[counter] = c;
  595.  
  596.         c = fgetc(ifp);
  597.     }
  598.  
  599.     if(numberFoundFlag == 0){
  600.         //no nubers in word, check reserved word list.
  601.         RWflag = checkForReservedWord(buffer);
  602.     }
  603.  
  604.     if(RWflag == 0){
  605.         //identifier found.
  606.         tail->token = identsym;
  607.         strncpy(tail->name, buffer, MAX_IDENTIFER_LENGTH);
  608.         tail->next = malloc(sizeof(lexemeList));
  609.         tailTrailer = tail;
  610.         tail = tail->next;
  611.         if(counter>=MAX_IDENTIFER_LENGTH)
  612.             printf("Warning: Identifier length too long.\n");
  613.     }
  614. }
  615.  
  616. int checkForReservedWord(char* buffer){
  617.  
  618.     //if (flag == 1) reserved word found, else must be an identifier.
  619.     int flag = 0;
  620.  
  621.     if(strncmp(buffer, "const", MAX_IDENTIFER_LENGTH) == 0){
  622.  
  623.         tail->token = constsym;
  624.         tail->next = malloc(sizeof(lexemeList));
  625.         tailTrailer = tail;
  626.         tail = tail->next;
  627.  
  628.         flag = 1;
  629.     }
  630.  
  631.     else if(strncmp(buffer, "var", MAX_IDENTIFER_LENGTH) == 0){
  632.  
  633.         tail->token = varsym;
  634.         tail->next = malloc(sizeof(lexemeList));
  635.         tailTrailer = tail;
  636.         tail = tail->next;
  637.  
  638.         flag = 1;
  639.     }
  640.  
  641.     else if(strncmp(buffer, "procedure", MAX_IDENTIFER_LENGTH) == 0){
  642.  
  643.         tail->token = procsym;
  644.         tail->next = malloc(sizeof(lexemeList));
  645.         tailTrailer = tail;
  646.         tail = tail->next;
  647.  
  648.         flag = 1;
  649.     }
  650.  
  651.     else if(strncmp(buffer, "call", MAX_IDENTIFER_LENGTH) == 0){
  652.  
  653.         tail->token = callsym;
  654.         tail->next = malloc(sizeof(lexemeList));
  655.         tailTrailer = tail;
  656.         tail = tail->next;
  657.  
  658.         flag = 1;
  659.     }
  660.  
  661.     else if(strncmp(buffer, "begin", MAX_IDENTIFER_LENGTH) == 0){
  662.  
  663.         tail->token = beginsym;
  664.         tail->next = malloc(sizeof(lexemeList));
  665.         tailTrailer = tail;
  666.         tail = tail->next;
  667.  
  668.         flag = 1;
  669.     }
  670.  
  671.     else if(strncmp(buffer, "end", MAX_IDENTIFER_LENGTH) == 0){
  672.  
  673.         tail->token = endsym;
  674.         tail->next = malloc(sizeof(lexemeList));
  675.         tailTrailer = tail;
  676.         tail = tail->next;
  677.  
  678.         flag = 1;
  679.     }
  680.  
  681.     else if(strncmp(buffer, "if", MAX_IDENTIFER_LENGTH) == 0){
  682.  
  683.         tail->token = ifsym;
  684.         tail->next = malloc(sizeof(lexemeList));
  685.         tailTrailer = tail;
  686.         tail = tail->next;
  687.  
  688.         flag = 1;
  689.     }
  690.  
  691.     else if(strncmp(buffer, "then", MAX_IDENTIFER_LENGTH) == 0){
  692.  
  693.         tail->token = thensym;
  694.         tail->next = malloc(sizeof(lexemeList));
  695.         tailTrailer = tail;
  696.         tail = tail->next;
  697.  
  698.         flag = 1;
  699.     }
  700.  
  701.     else if(strncmp(buffer, "else", MAX_IDENTIFER_LENGTH) == 0){
  702.  
  703.         tail->token = elsesym;
  704.         tail->next = malloc(sizeof(lexemeList));
  705.         tailTrailer = tail;
  706.         tail = tail->next;
  707.  
  708.         flag = 1;
  709.     }
  710.  
  711.     else if(strncmp(buffer, "while", MAX_IDENTIFER_LENGTH) == 0){
  712.  
  713.         tail->token = whilesym;
  714.         tail->next = malloc(sizeof(lexemeList));
  715.         tailTrailer = tail;
  716.         tail = tail->next;
  717.  
  718.         flag = 1;
  719.     }
  720.  
  721.     else if(strncmp(buffer, "do", MAX_IDENTIFER_LENGTH) == 0){
  722.  
  723.         tail->token = dosym;
  724.         tail->next = malloc(sizeof(lexemeList));
  725.         tailTrailer = tail;
  726.         tail = tail->next;
  727.  
  728.         flag = 1;
  729.     }
  730.  
  731.     else if(strncmp(buffer, "read", MAX_IDENTIFER_LENGTH) == 0){
  732.  
  733.         tail->token = readsym;
  734.         tail->next = malloc(sizeof(lexemeList));
  735.         tailTrailer = tail;
  736.         tail = tail->next;
  737.  
  738.         flag = 1;
  739.     }
  740.  
  741.     else if(strncmp(buffer, "write", MAX_IDENTIFER_LENGTH) == 0){
  742.  
  743.         tail->token = writesym;
  744.         tail->next = malloc(sizeof(lexemeList));
  745.         tailTrailer = tail;
  746.         tail = tail->next;
  747.  
  748.         flag = 1;
  749.     }
  750.  
  751.     else if(strncmp(buffer, "odd", MAX_IDENTIFER_LENGTH) == 0){
  752.  
  753.         tail->token = oddsym;
  754.         tail->next = malloc(sizeof(lexemeList));
  755.         tailTrailer = tail;
  756.         tail = tail->next;
  757.  
  758.         flag = 1;
  759.     }
  760.  
  761.     return flag;
  762. }
  763.  
  764. void getNumber(FILE* ifp){
  765.  
  766.     int counter = 0, tempNumber, i, badIdentifierFlag = 0;
  767.     char buffer[100];
  768.  
  769.     for(i=0; i<100; i++)
  770.         buffer[i] = '\0';
  771.  
  772.     buffer[counter] = c;
  773.     c = fgetc(ifp);
  774.  
  775.     while(isdigit(c) != 0){
  776.  
  777.         counter++;
  778.  
  779.         buffer[counter] = c;
  780.  
  781.         c = fgetc(ifp);
  782.  
  783.         if(isalpha(c) != 0){
  784.             badIdentifierFlag = 1;
  785.         }
  786.     }
  787.  
  788.     if(isalpha(c) != 0){
  789.         badIdentifierFlag = 1;
  790.     }
  791.  
  792.     if(badIdentifierFlag == 0){
  793.  
  794.         tempNumber = atoi(buffer);
  795.  
  796.         tail->token = numbersym;
  797.         tail->number = tempNumber;
  798.         tail->next = malloc(sizeof(lexemeList));
  799.         tailTrailer = tail;
  800.         tail = tail->next;
  801.  
  802.         if(counter>=MAX_NUMBER_LENGTH)
  803.             printf("Warning: Number length too long.\n");
  804.     }
  805.  
  806.     else{
  807.  
  808.          while((isalpha(c) != 0) || (isdigit(c) != 0)){
  809.  
  810.             counter++;
  811.  
  812.             buffer[counter] = c;
  813.  
  814.             c = fgetc(ifp);
  815.          }
  816.  
  817.         tail->token = identsym;
  818.         strncpy(tail->name, buffer, MAX_IDENTIFER_LENGTH);
  819.         tail->next = malloc(sizeof(lexemeList));
  820.         tailTrailer = tail;
  821.         tail = tail->next;
  822.  
  823.     }
  824. }
  825.  
  826. void getSymbol(FILE* ifp){
  827.  
  828.     int commentFlag = 0;
  829.  
  830.     if(c == '+'){
  831.  
  832.         tail->token = plussym;
  833.         tail->next = malloc(sizeof(lexemeList));
  834.         tailTrailer = tail;
  835.         tail = tail->next;
  836.  
  837.         c = fgetc(ifp);
  838.     }
  839.  
  840.     else if(c == '-'){
  841.  
  842.         tail->token = minussym;
  843.         tail->next = malloc(sizeof(lexemeList));
  844.         tailTrailer = tail;
  845.         tail = tail->next;
  846.  
  847.         c = fgetc(ifp);
  848.     }
  849.  
  850.     else if(c == '*'){
  851.  
  852.         tail->token = multsym;
  853.         tail->next = malloc(sizeof(lexemeList));
  854.         tailTrailer = tail;
  855.         tail = tail->next;
  856.  
  857.         c = fgetc(ifp);
  858.     }
  859.  
  860.     else if(c == '/'){
  861.  
  862.         c = fgetc(ifp);
  863.  
  864.         //comment check
  865.         if(c == '*'){
  866.  
  867.             while(commentFlag == 0){
  868.  
  869.                 c = fgetc(ifp);
  870.  
  871.  
  872.                 if(c == '*'){
  873.  
  874.                     c = fgetc(ifp);
  875.  
  876.                     if(c == '/')
  877.                         commentFlag = 1;
  878.                 }
  879.             }
  880.  
  881.             c = fgetc(ifp);
  882.  
  883.         }
  884.         else{
  885.             //its a divides symbol
  886.             tail->token = slashsym;
  887.             tail->next = malloc(sizeof(lexemeList));
  888.             tailTrailer = tail;
  889.             tail = tail->next;
  890.         }
  891.     }
  892.  
  893.     else if(c == '('){
  894.  
  895.         tail->token = lparentsym;
  896.         tail->next = malloc(sizeof(lexemeList));
  897.         tailTrailer = tail;
  898.         tail = tail->next;
  899.  
  900.         c = fgetc(ifp);
  901.     }
  902.  
  903.     else if(c == ')'){
  904.  
  905.         tail->token = rparentsym;
  906.         tail->next = malloc(sizeof(lexemeList));
  907.         tailTrailer = tail;
  908.         tail = tail->next;
  909.  
  910.         c = fgetc(ifp);
  911.     }
  912.  
  913.     else if(c == '='){
  914.  
  915.         tail->token = eqlsym;
  916.         tail->next = malloc(sizeof(lexemeList));
  917.         tailTrailer = tail;
  918.         tail = tail->next;
  919.  
  920.         c = fgetc(ifp);
  921.     }
  922.  
  923.     else if(c == ','){
  924.  
  925.         tail->token = commasym;
  926.         tail->next = malloc(sizeof(lexemeList));
  927.         tailTrailer = tail;
  928.         tail = tail->next;
  929.  
  930.         c = fgetc(ifp);
  931.     }
  932.  
  933.     else if(c == '.'){
  934.  
  935.         tail->token = periodsym;
  936.         tail->next = malloc(sizeof(lexemeList));
  937.         tailTrailer = tail;
  938.         tail = tail->next;
  939.  
  940.         c = fgetc(ifp);
  941.     }
  942.  
  943.     else if(c == '<'){
  944.        //could be <, <=, or <>.
  945.        c = fgetc(ifp);
  946.  
  947.        if(c == '='){
  948.  
  949.             tail->token = leqsym;
  950.             tail->next = malloc(sizeof(lexemeList));
  951.             tailTrailer = tail;
  952.             tail = tail->next;
  953.  
  954.             c = fgetc(ifp);
  955.        }
  956.  
  957.        else if(c == '>'){
  958.  
  959.             tail->token = neqsym;
  960.             tail->next = malloc(sizeof(lexemeList));
  961.             tailTrailer = tail;
  962.             tail = tail->next;
  963.  
  964.             c = fgetc(ifp);
  965.        }
  966.  
  967.        else{
  968.  
  969.             tail->token = lessym;
  970.             tail->next = malloc(sizeof(lexemeList));
  971.             tailTrailer = tail;
  972.             tail = tail->next;
  973.        }
  974.     }
  975.  
  976.     else if(c == '>'){
  977.         //could be > or >=.
  978.         c = fgetc(ifp);
  979.  
  980.         if(c == '='){
  981.  
  982.             tail->token = geqsym;
  983.             tail->next = malloc(sizeof(lexemeList));
  984.             tailTrailer = tail;
  985.             tail = tail->next;
  986.  
  987.             c = fgetc(ifp);
  988.         }
  989.  
  990.         else{
  991.  
  992.             tail->token = gtrsym;
  993.             tail->next = malloc(sizeof(lexemeList));
  994.             tailTrailer = tail;
  995.             tail = tail->next;
  996.         }
  997.     }
  998.  
  999.     else if(c == ';'){
  1000.  
  1001.         tail->token = semicolonsym;
  1002.         tail->next = malloc(sizeof(lexemeList));
  1003.         tailTrailer = tail;
  1004.         tail = tail->next;
  1005.  
  1006.         c = fgetc(ifp);
  1007.     }
  1008.  
  1009.     else if(c == ':'){
  1010.  
  1011.        c = fgetc(ifp);
  1012.  
  1013.        if(c == '='){
  1014.  
  1015.             tail->token = becomessym;
  1016.             tail->next = malloc(sizeof(lexemeList));
  1017.             tailTrailer = tail;
  1018.             tail = tail->next;
  1019.  
  1020.             c = fgetc(ifp);
  1021.        }
  1022.  
  1023.   //     else
  1024. //            printf("Error: Expected '=' after ':'.\n");
  1025.  
  1026.     }
  1027.  
  1028.     else if(c == ' '){
  1029.         //space. skip and move on.
  1030.         c = fgetc(ifp);
  1031.     }
  1032.  
  1033.     else if(c == '\t'){
  1034.         //tab. skip and move on.
  1035.         c = fgetc(ifp);
  1036.     }
  1037.  
  1038.     else if((c == '\n') | (c == 0xd)){
  1039.         //newline. skip and move on.
  1040.         c = fgetc(ifp);
  1041.     }
  1042.  
  1043.     else{
  1044.         //error, unknown symbol.
  1045.   //      printf("0x%x \t Error: Unknown symbol\n", c);
  1046.         c = fgetc(ifp);
  1047.     }
  1048. }
  1049.  
  1050. void printLexemeList(){
  1051.  
  1052.     FILE* ofp;
  1053.     ofp = fopen("lexerOutput.txt", "w");
  1054.     if (ofp == NULL){
  1055.         printf("failed to open lexerOutput.txt.\n");
  1056.         exit(-1);
  1057.     }
  1058.  
  1059.     while(head != NULL){
  1060.  
  1061.         fprintf(ofp, "%d ", head->token);
  1062.  
  1063.         if(head->token == 2){
  1064.  
  1065.             fprintf(ofp, "%s ", head->name);
  1066.         }
  1067.  
  1068.         else if(head->token == 3){
  1069.  
  1070.             fprintf(ofp, "%d ", head->number);
  1071.         }
  1072.  
  1073.         head = head->next;
  1074.     }
  1075.  
  1076.     printf("\n");
  1077.  
  1078.     fclose(ofp);
  1079. }
  1080.  
  1081. int parcerMain(){
  1082.  
  1083.     initializeTables();
  1084.  
  1085.     FILE* ifp;
  1086.     ifp = fopen("lexerOutput.txt", "r");
  1087.     if(ifp == NULL){
  1088.         printf("Failed to open lexerOutput.txt #2.\n");
  1089.         exit(0);
  1090.     }
  1091.  
  1092.     PROGRAM(ifp);
  1093.  
  1094.     printCodeTable();
  1095.  
  1096.     printf("Program is syntactically correct.\n\n");
  1097.  
  1098.     fclose(ifp);
  1099.  
  1100.     return 0;
  1101. }
  1102.  
  1103. void initialTokenPrint(){
  1104.  
  1105.     char temp;
  1106.  
  1107.     FILE* ifp;
  1108.     ifp = fopen("lexerOutput.txt", "r");
  1109.     if(ifp == NULL){
  1110.         printf("failed to open lexerOutput.txt #3.\n");
  1111.         exit(-1);
  1112.     }
  1113.  
  1114.     while((temp = fgetc(ifp))!= EOF){
  1115.         printf("%c", temp);
  1116.     }
  1117.  
  1118.     printf("\n\n");
  1119.  
  1120.     fclose(ifp);
  1121. }
  1122.  
  1123. void initialSymbolPrint(){
  1124.  
  1125.     int token;
  1126.     int num;
  1127.     char buffer[MAX_NAME_SIZE +1];
  1128.  
  1129.     FILE* ifp;
  1130.     ifp = fopen("lexerOutput.txt", "r");
  1131.     if(ifp == NULL){
  1132.         printf("failed to open lexerOutput.txt #4.\n");
  1133.         exit(-1);
  1134.     }
  1135.  
  1136.     while(fscanf(ifp, "%d", &token) != EOF){
  1137.  
  1138.             switch(token){
  1139.  
  1140.                     case 1: printf("nulsym ");
  1141.                             break;
  1142.                     case 2: printf("identsym ");
  1143.                             fscanf(ifp, "%s", buffer);
  1144.                             printf("%s ", buffer);
  1145.                             break;
  1146.                     case 3: printf("numbersym ");
  1147.                             fscanf(ifp, "%d", &num);
  1148.                             printf("%d ", num);
  1149.                             break;
  1150.                     case 4: printf("plussym ");
  1151.                             break;
  1152.                     case 5: printf("minussym ");
  1153.                             break;
  1154.                     case 6: printf("multsym ");
  1155.                             break;
  1156.                     case 7: printf("slashsym ");
  1157.                             break;
  1158.                     case 8: printf("oddsym ");
  1159.                             break;
  1160.                     case 9: printf("eqlsym ");
  1161.                             break;
  1162.                     case 10: printf("neqsym ");
  1163.                             break;
  1164.                     case 11: printf("lessym ");
  1165.                             break;
  1166.                     case 12: printf("leqsym ");
  1167.                             break;
  1168.                     case 13: printf("gtrsym ");
  1169.                             break;
  1170.                     case 14: printf("geqsym ");
  1171.                             break;
  1172.                     case 15: printf("lparentsym ");
  1173.                             break;
  1174.                     case 16: printf("rparentsym ");
  1175.                             break;
  1176.                     case 17: printf("commasym ");
  1177.                             break;
  1178.                     case 18: printf("semicolonsym ");
  1179.                             break;
  1180.                     case 19: printf("periodsym ");
  1181.                             break;
  1182.                     case 20: printf("becomessym ");
  1183.                             break;
  1184.                     case 21: printf("beginsym ");
  1185.                             break;
  1186.                     case 22: printf("endsym ");
  1187.                             break;
  1188.                     case 23: printf("ifsym ");
  1189.                             break;
  1190.                     case 24: printf("thensym ");
  1191.                             break;
  1192.                     case 25: printf("whilesym ");
  1193.                             break;
  1194.                     case 26: printf("dosym ");
  1195.                             break;
  1196.                     case 27: printf("callsym ");
  1197.                             break;
  1198.                     case 28: printf("constsym ");
  1199.                             break;
  1200.                     case 29: printf("varsym ");
  1201.                             break;
  1202.                     case 30: printf("procsym ");
  1203.                             break;
  1204.                     case 31: printf("writesym ");
  1205.                             break;
  1206.                     case 32: printf("readsym ");
  1207.                             break;
  1208.                     case 33: printf("elsesym ");
  1209.                             break;
  1210.             }
  1211.        }
  1212.  
  1213.     printf("\n\n");
  1214.  
  1215.     fclose(ifp);
  1216.  
  1217. }
  1218.  
  1219. void initializeTables(){
  1220.  
  1221.     int i, j;
  1222.  
  1223.     Head = NULL;
  1224.     addFirstNode();
  1225.     strcpy(Head->procName, "Main");
  1226.  
  1227.     for(i=0; i<MAX_SYMBOL_TABLE_SIZE; i++){
  1228.  
  1229.         symbol_table[i].kind = 0;
  1230.         for(j=0; j<MAX_NAME_SIZE+1; j++)
  1231.             symbol_table[i].name[j] = '\0';
  1232.         symbol_table[i].val = 0;
  1233.         symbol_table[i].level = 0;
  1234.         symbol_table[i].addr = 0;
  1235.         symbol_table[i].head = NULL;
  1236.     }
  1237.  
  1238.     for(i=0; i< MAX_CODE_TABLE_SIZE; i++){
  1239.         code_table[i].op = 0;
  1240.         code_table[i].l = 0;
  1241.         code_table[i].m = 0;
  1242.     }
  1243.  
  1244.     TOKEN = 0;
  1245.     Kind = 0;
  1246.     for(i=0; i<MAX_NAME_SIZE+1; i++){
  1247.         Name[i] = '\0';
  1248.     }
  1249.     Value = 0;
  1250.     Level = 0;
  1251.     TableIndex = 0;
  1252.     CodeIndex = 0;
  1253.     strcpy(CurrentProcedure, "Main");
  1254. }
  1255.  
  1256. void getsym(FILE* ifp){
  1257.  
  1258.     fscanf(ifp, "%d", &TOKEN);
  1259. }
  1260.  
  1261. void getname(FILE* ifp){
  1262.  
  1263.     fscanf(ifp, "%s", Name);
  1264. }
  1265.  
  1266. void getvalue(FILE* ifp){
  1267.  
  1268.     fscanf(ifp, "%d", &Value);
  1269. }
  1270.  
  1271. void enter(int offset){
  1272.  
  1273.     int i;
  1274.  
  1275.     symbol_table[TableIndex].kind = Kind;
  1276.  
  1277.     if(Kind == CONSTANT){
  1278.  
  1279.         strcpy(symbol_table[TableIndex].name, Name);
  1280.         symbol_table[TableIndex].val = Value;
  1281.         symbol_table[TableIndex].head = Head;
  1282.     }
  1283.  
  1284.     else if(Kind == VARIABLE){
  1285.  
  1286.         strcpy(symbol_table[TableIndex].name, Name);
  1287.         symbol_table[TableIndex].level = Level;
  1288.         symbol_table[TableIndex].addr = offset;
  1289.         symbol_table[TableIndex].head = generateLinkedList();
  1290.     }
  1291.  
  1292.     else if(Kind == PROCEDURE){
  1293.  
  1294.         strcpy(symbol_table[TableIndex].name, Name);
  1295.         symbol_table[TableIndex].level = Level;
  1296.         symbol_table[TableIndex].addr = CodeIndex;
  1297.     }
  1298.  
  1299.     for(i=0; i<MAX_NAME_SIZE+1; i++){
  1300.         Name[i] = '\0';
  1301.     }
  1302.  
  1303.     TableIndex++;
  1304.  
  1305. }
  1306.  
  1307. void error(int e){
  1308.  
  1309.     switch(e){
  1310.  
  1311.         case ERR_PER_EXP:
  1312.             printf("Period expected.\n");
  1313.             break;
  1314.  
  1315.         case ERR_IDENT_MISSING:
  1316.             printf("const, var, procedure must be followed by identifier.\n");
  1317.             break;
  1318.  
  1319.         case ERR_EQUAL_MISSING:
  1320.             printf("Identifier must be followed by =.\n");
  1321.             break;
  1322.  
  1323.         case ERR_NUMBER_MISSING:
  1324.             printf("= must be followed by a number.\n");
  1325.             break;
  1326.  
  1327.         case ERR_SEMI_COMMA_MISSING:
  1328.             printf("Semicolon or comma missing.\n");
  1329.             break;
  1330.  
  1331.         case ERR_UNDECL_IDENT:
  1332.             printf("Undeclared identifier.\n");
  1333.             break;
  1334.  
  1335.         case ERR_ASSIGN_NOT_ALLOWED:
  1336.             printf("Assignment to constant or procedure is not allowed.\n");
  1337.             break;
  1338.  
  1339.         case ERR_ASSIGN_EXPECT:
  1340.             printf("Assignment operator expected.\n");
  1341.             break;
  1342.  
  1343.         case ERR_IDENT_EXPECTED:
  1344.             printf("Call must be followed by an identifier.\n");
  1345.             break;
  1346.  
  1347.         case ERR_BAD_CALL:
  1348.             printf("Call of a constant or variable is meaningless.\n");
  1349.             break;
  1350.  
  1351.         case ERR_THEN_EXPECTED:
  1352.             printf("then expected.\n");
  1353.             break;
  1354.  
  1355.         case ERR_DO_EXPECTED:
  1356.             printf("Do expected.\n");
  1357.             break;
  1358.  
  1359.         case ERR_EXPRESS_ERROR:
  1360.             printf("Expression must not contain a procedure identifier.\n");
  1361.             break;
  1362.  
  1363.         case ERR_REL_EXPECTED:
  1364.             printf("Relational operator expected.\n");
  1365.             break;
  1366.  
  1367.         case ERR_PAREN_EXPECTED:
  1368.             printf("Right parenthesis missing.\n");
  1369.             break;
  1370.  
  1371.         case ERR_BAD_SYMBOL:
  1372.             printf("The preceding factor cannot begin with this symbol.\n");
  1373.             break;
  1374.  
  1375.         case ERR_ID_EXPECT_AFTER_READWRITE:
  1376.             printf("Identifier expected after using read or write.\n");
  1377.             break;
  1378.  
  1379.         case ERR_END_EXPECTED:
  1380.             printf("End expected.\n");
  1381.             break;
  1382.  
  1383.         case ERR_REPEAT:
  1384.             printf("Variable name has already been used within the same level or procedure name has already been used.\n");
  1385.             break;
  1386.  
  1387.         default:
  1388.             printf("Unknown Error.\n");
  1389.             break;
  1390.     }
  1391.  
  1392.     exit(1);
  1393. }
  1394.  
  1395. void gen(int OP, int L, int M){
  1396.  
  1397.     code_table[CodeIndex].op = OP;
  1398.     code_table[CodeIndex].l = L;
  1399.     code_table[CodeIndex].m = M;
  1400.  
  1401.     CodeIndex++;
  1402. }
  1403.  
  1404. void addFirstNode(){
  1405.  
  1406.     int i;
  1407.  
  1408.     node* temp = Head;
  1409.  
  1410.     Head = malloc(sizeof(node));
  1411.     for(i=0; i<MAX_NAME_SIZE+1; i++)
  1412.         Head->procName[i] = '\0';
  1413.     Head->next = temp;
  1414. }
  1415.  
  1416. void deleteFirstNode(){
  1417.  
  1418.     Head = Head->next;
  1419.  
  1420. }
  1421.  
  1422. int position(){
  1423.  
  1424.     int i;
  1425.     for(i=MAX_SYMBOL_TABLE_SIZE-1; i>=0; i--){
  1426.             if(strcmp(Name, symbol_table[i].name)==0){
  1427.                 if(symbol_table[i].kind == VARIABLE){
  1428.                     if(Level >= symbol_table[i].level)
  1429.                         if(checkVariableAccess(i) == TRUE)
  1430.                             return i;
  1431.                 }
  1432.                 else{
  1433.                     return i;
  1434.                 }
  1435.             }
  1436.     }
  1437.  
  1438.     return i;
  1439. }
  1440.  
  1441. int checkVariableAccess(int i){
  1442.  
  1443.     int incrementCurrHead = Level - symbol_table[i].level;
  1444.     node* tempSymTabHead = symbol_table[i].head;
  1445.     node* tempCurrHead = Head;
  1446.  
  1447.     for( ; incrementCurrHead>0; incrementCurrHead--){
  1448.  
  1449.         tempCurrHead = tempCurrHead->next;
  1450.     }
  1451.  
  1452.     if(strcmp(tempSymTabHead->procName, tempCurrHead->procName)==0)
  1453.         return TRUE;
  1454.     else
  1455.         return FALSE;
  1456. }
  1457.  
  1458. void PROGRAM(FILE* ifp){
  1459.  
  1460.     getsym(ifp);
  1461.  
  1462.     //Initial Jump
  1463.     gen(JMP, 0, 0); //Place holder change M later
  1464.  
  1465.     BLOCK(ifp);
  1466.     if (TOKEN != periodsym)
  1467.         error(ERR_PER_EXP);
  1468.  
  1469.     //Final return
  1470.     gen(OPR, 0, RET);
  1471. }
  1472.  
  1473. void BLOCK(FILE* ifp){
  1474.  
  1475.     int i, offset;
  1476.     offset = RESERVED_STACK_SIZE;
  1477.  
  1478.     if (TOKEN == constsym){
  1479.  
  1480.         Kind = CONSTANT;
  1481.  
  1482.         do{
  1483.  
  1484.             getsym(ifp);
  1485.             if (TOKEN != identsym)
  1486.                 error(ERR_IDENT_MISSING);
  1487.             getname(ifp);
  1488.  
  1489.             getsym(ifp);
  1490.             if (TOKEN != eqlsym)
  1491.                 error(ERR_EQUAL_MISSING);
  1492.  
  1493.             getsym(ifp);
  1494.             if (TOKEN != numbersym)
  1495.                 error(ERR_NUMBER_MISSING);
  1496.             getvalue(ifp);
  1497.             checkRepeat();
  1498.             enter(offset);
  1499.  
  1500.             getsym(ifp);
  1501.  
  1502.         }while(TOKEN == commasym);
  1503.  
  1504.         if(TOKEN != semicolonsym)
  1505.             error(ERR_SEMI_COMMA_MISSING);
  1506.         getsym(ifp);
  1507.  
  1508.     }
  1509.  
  1510.     if (TOKEN == varsym){
  1511.  
  1512.         Kind = VARIABLE;
  1513.  
  1514.         do{
  1515.  
  1516.             getsym(ifp);
  1517.             if (TOKEN != identsym)
  1518.                 error(ERR_IDENT_MISSING);
  1519.             getname(ifp);
  1520.             checkRepeat();
  1521.             enter(offset);
  1522.             offset++;
  1523.  
  1524.             getsym(ifp);
  1525.  
  1526.         }while(TOKEN == commasym);
  1527.  
  1528.         if (TOKEN != semicolonsym)
  1529.             error(ERR_SEMI_COMMA_MISSING);
  1530.         getsym(ifp);
  1531.  
  1532.     }
  1533.  
  1534.     while (TOKEN == procsym){
  1535.  
  1536.         Kind = PROCEDURE;
  1537.  
  1538.         getsym(ifp);
  1539.         if (TOKEN != identsym)
  1540.             error(ERR_IDENT_MISSING);
  1541.         getname(ifp);
  1542.         for(i=0; i<MAX_NAME_SIZE+1; i++){
  1543.             CurrentProcedure[i] = '\0';
  1544.         }
  1545.  
  1546.         addFirstNode();
  1547.         strcpy(CurrentProcedure, Name);
  1548.         for(i=0; i<MAX_NAME_SIZE+1; i++){
  1549.             Head->procName[i] = '\0';
  1550.         }
  1551.         strcpy(Head->procName, Name);
  1552.  
  1553.         getsym(ifp);
  1554.         if (TOKEN != semicolonsym)
  1555.             error(ERR_SEMI_COMMA_MISSING);
  1556.         checkRepeat();
  1557.         enter(offset);
  1558.  
  1559.         //Jumps
  1560.         gen(JMP, 0, 0); //Place holder change M later.
  1561.  
  1562.         getsym(ifp);
  1563.  
  1564.         Level++;
  1565.         BLOCK(ifp);
  1566.         Level--;
  1567.         deleteFirstNode();
  1568.  
  1569.         if (TOKEN != semicolonsym)
  1570.             error(ERR_SEMI_COMMA_MISSING);
  1571.         getsym(ifp);
  1572.  
  1573.          //Return
  1574.         gen(OPR, 0, RET);
  1575.  
  1576.     }
  1577.  
  1578.     //Change M in jumps
  1579.     for(i=CodeIndex; i>=0; i--){
  1580.         if((code_table[i].op == JMP)&&(code_table[i].m==0)){
  1581.             code_table[i].m = CodeIndex;
  1582.             break;
  1583.         }
  1584.     }
  1585.  
  1586.     //Increments
  1587.     gen(INC, 0, offset); //Offset == Number of Variables + 4.
  1588.  
  1589.     STATEMENT(ifp);
  1590.  
  1591. }
  1592.  
  1593. void STATEMENT(FILE* ifp){
  1594.  
  1595.     int i, cx;
  1596.  
  1597.     if (TOKEN == identsym){
  1598.  
  1599.         //Stores
  1600.         getname(ifp);
  1601.         i = position();
  1602.         if (i==-1)
  1603.             error(ERR_UNDECL_IDENT);
  1604.  
  1605.         else if (symbol_table[i].kind!=VARIABLE) {
  1606.             error(ERR_ASSIGN_NOT_ALLOWED);
  1607.             i=-1;
  1608.         }
  1609.  
  1610.         getsym(ifp);
  1611.         if (TOKEN != becomessym)
  1612.             error(ERR_ASSIGN_EXPECT);
  1613.  
  1614.         getsym(ifp);
  1615.         EXPRESSION(ifp);
  1616.  
  1617.         //Stores
  1618.         if (i != -1)
  1619.             gen(STO, Level-symbol_table[i].level, symbol_table[i].addr);
  1620.  
  1621.     }
  1622.  
  1623.     else if (TOKEN == callsym){
  1624.  
  1625.         getsym(ifp);
  1626.         if (TOKEN != identsym)
  1627.             error(ERR_IDENT_EXPECTED);
  1628.  
  1629.         //Calls
  1630.         getname(ifp);
  1631.         i=position();
  1632.         if (i==-1)
  1633.             error(ERR_UNDECL_IDENT);
  1634.         else if (symbol_table[i].kind == PROCEDURE)
  1635.             gen(CAL, Level-symbol_table[i].level, symbol_table[i].addr);
  1636.  
  1637.         else
  1638.             error(ERR_BAD_CALL);
  1639.  
  1640.         getsym(ifp);
  1641.  
  1642.     }
  1643.  
  1644.     else if (TOKEN == beginsym){
  1645.  
  1646.         getsym(ifp);
  1647.         STATEMENT(ifp);
  1648.  
  1649.         while(TOKEN == semicolonsym){
  1650.  
  1651.             getsym(ifp);
  1652.             STATEMENT(ifp);
  1653.  
  1654.         }
  1655.  
  1656.         if (TOKEN != endsym)
  1657.             error(ERR_END_EXPECTED);
  1658.  
  1659.         getsym(ifp);
  1660.  
  1661.     }
  1662.  
  1663.     else if (TOKEN == ifsym){
  1664.  
  1665.         getsym(ifp);
  1666.  
  1667.         CONDITION(ifp);
  1668.  
  1669.         gen(JPC, 0, 0); //Place holder instruction. Change M later.
  1670.  
  1671.         if (TOKEN != thensym)
  1672.             error(ERR_THEN_EXPECTED);
  1673.  
  1674.         getsym(ifp);
  1675.  
  1676.         STATEMENT(ifp);
  1677.  
  1678.         gen(JMP, 0, 0); //Place holder instruction. Change M later.
  1679.  
  1680.         // change M for JPC.
  1681.         for(i=CodeIndex; i>=0; i--){
  1682.             if((code_table[i].op == JPC)&&(code_table[i].m==0)){
  1683.                 code_table[i].m = CodeIndex;
  1684.                 break;
  1685.             }
  1686.         }
  1687.  
  1688.         if (TOKEN == elsesym){
  1689.  
  1690.             getsym(ifp);
  1691.  
  1692.             STATEMENT(ifp);
  1693.         }
  1694.  
  1695.         for(i=CodeIndex; i>=0; i--){
  1696.             if((code_table[i].op == JMP)&&(code_table[i].m==0)){
  1697.                 code_table[i].m = CodeIndex;
  1698.                 break;
  1699.             }
  1700.         }
  1701.     }
  1702.  
  1703.     else if (TOKEN == whilesym){
  1704.  
  1705.         getsym(ifp);
  1706.  
  1707.         cx = CodeIndex;
  1708.         CONDITION(ifp);
  1709.  
  1710.         gen(JPC, 0, 0); //Place holder instruction. Change M later.
  1711.  
  1712.         if (TOKEN != dosym)
  1713.             error(ERR_DO_EXPECTED);
  1714.         getsym(ifp);
  1715.  
  1716.         STATEMENT(ifp);
  1717.  
  1718.         gen(JMP, 0, cx); //Place holder instruction. Change M later.
  1719.  
  1720.         for(i=CodeIndex; i>=0; i--){
  1721.             if((code_table[i].op == JPC)&&(code_table[i].m==0)){
  1722.                 code_table[i].m = CodeIndex;
  1723.                 break;
  1724.             }
  1725.         }
  1726.     }
  1727.  
  1728.     else if (TOKEN == writesym){
  1729.  
  1730.         getsym(ifp);
  1731.  
  1732.         if (TOKEN != identsym)
  1733.             error(ERR_ID_EXPECT_AFTER_READWRITE);
  1734.  
  1735.         getname(ifp);
  1736.         i = position();
  1737.         if (i==-1)
  1738.             error(ERR_UNDECL_IDENT);
  1739.         if (symbol_table[i].kind==CONSTANT)
  1740.             gen(LIT, 0, symbol_table[i].val);
  1741.         else if (symbol_table[i].kind==VARIABLE)
  1742.             gen(LOD, Level-symbol_table[i].level, symbol_table[i].addr);
  1743.         else
  1744.             error(ERR_EXPRESS_ERROR);
  1745.  
  1746.         gen(SIOw, 0, 1);
  1747.         getsym(ifp);
  1748.  
  1749.     }
  1750.  
  1751.     else if (TOKEN == readsym){
  1752.  
  1753.         gen(SIOr, 0, 2);
  1754.         getsym(ifp);
  1755.  
  1756.         if(TOKEN != identsym)
  1757.             error(ERR_ID_EXPECT_AFTER_READWRITE);
  1758.  
  1759.         getname(ifp);
  1760.         i = position();
  1761.         if (i==-1)
  1762.             error(ERR_UNDECL_IDENT);
  1763.  
  1764.         else if (symbol_table[i].kind!=VARIABLE) {
  1765.             error(ERR_ASSIGN_NOT_ALLOWED);
  1766.             i=-1;
  1767.         }
  1768.  
  1769.         //Stores
  1770.         if (i != -1)
  1771.             gen(STO, Level-symbol_table[i].level, symbol_table[i].addr);
  1772.         getsym(ifp);
  1773.  
  1774.     }
  1775.  
  1776.     else {
  1777.         //empty set
  1778.     }
  1779. }
  1780.  
  1781. void CONDITION(FILE* ifp){
  1782.  
  1783.     int m;
  1784.  
  1785.     if (TOKEN == oddsym){
  1786.  
  1787.         getsym(ifp);
  1788.         EXPRESSION(ifp);
  1789.         gen(OPR, 0, ODD);
  1790.     }
  1791.  
  1792.     else{
  1793.  
  1794.         EXPRESSION(ifp);
  1795.  
  1796.         m = (RELATION(TOKEN));
  1797.  
  1798.         if(m == FALSE)
  1799.             error(ERR_REL_EXPECTED);
  1800.  
  1801.         getsym(ifp);
  1802.         EXPRESSION(ifp);
  1803.  
  1804.         gen(OPR, 0, m);
  1805.  
  1806.     }
  1807.  
  1808. }
  1809.  
  1810. int RELATION(int token){
  1811.  
  1812.     if (token == eqlsym)
  1813.         return EQL;
  1814.     else if (token == neqsym)
  1815.         return NEQ;
  1816.     else if (token == lessym)
  1817.         return LSS;
  1818.     else if (token == leqsym)
  1819.         return LEQ;
  1820.     else if (token == gtrsym)
  1821.         return GTR;
  1822.     else if (token == geqsym)
  1823.         return GEQ;
  1824.     else
  1825.         return FALSE;
  1826. }
  1827.  
  1828. void EXPRESSION(FILE* ifp){
  1829.  
  1830.     int m=0;
  1831.  
  1832.     if ((TOKEN == plussym) || (TOKEN == minussym))
  1833.         getsym(ifp);
  1834.     if (TOKEN == minussym)
  1835.         m = NEG;
  1836.  
  1837.     TERM(ifp);
  1838.  
  1839.     if (m == NEG)
  1840.         gen(OPR, 0, NEG);
  1841.  
  1842.     while ((TOKEN == plussym) || (TOKEN == minussym)){
  1843.  
  1844.     if (TOKEN == plussym)
  1845.         m = ADD;
  1846.     else
  1847.         m = SUB;
  1848.  
  1849.         getsym(ifp);
  1850.         TERM(ifp);
  1851.  
  1852.         gen(OPR, 0, m);
  1853.  
  1854.     }
  1855.  
  1856. }
  1857.  
  1858. void TERM(FILE* ifp){
  1859.  
  1860.     int m;
  1861.  
  1862.     FACTOR(ifp);
  1863.  
  1864.     while ((TOKEN == multsym) || (TOKEN == slashsym)){
  1865.  
  1866.         if(TOKEN == multsym)
  1867.             m = MUL;
  1868.         else
  1869.             m = DIV;
  1870.         getsym(ifp);
  1871.         FACTOR(ifp);
  1872.         gen(OPR, 0, m);
  1873.     }
  1874. }
  1875.  
  1876. void FACTOR(FILE* ifp){
  1877.  
  1878.     int i;
  1879.  
  1880.     if (TOKEN == identsym){
  1881.  
  1882.         //Loads
  1883.         getname(ifp);
  1884.         i = position();
  1885.         if (i==-1)
  1886.             error(ERR_UNDECL_IDENT);
  1887.         if (symbol_table[i].kind==CONSTANT)
  1888.             gen(LIT, 0, symbol_table[i].val);
  1889.         else if (symbol_table[i].kind==VARIABLE)
  1890.             gen(LOD, Level-symbol_table[i].level, symbol_table[i].addr);
  1891.         else
  1892.             error(ERR_EXPRESS_ERROR);
  1893.  
  1894.         getsym(ifp);
  1895.  
  1896.     }
  1897.  
  1898.     else if (TOKEN == numbersym){
  1899.  
  1900.         //Load immeadiate
  1901.         getvalue(ifp);
  1902.         gen(LIT, 0, Value);
  1903.  
  1904.         getsym(ifp);
  1905.  
  1906.     }
  1907.  
  1908.     else if (TOKEN == lparentsym){
  1909.  
  1910.         getsym(ifp);
  1911.  
  1912.         EXPRESSION(ifp);
  1913.  
  1914.         if (TOKEN != rparentsym)
  1915.             error(ERR_PAREN_EXPECTED);
  1916.  
  1917.         getsym(ifp);
  1918.  
  1919.     }
  1920.  
  1921.     else
  1922.         error(ERR_BAD_SYMBOL);
  1923.  
  1924. }
  1925.  
  1926. void printCodeTable(){
  1927.  
  1928.     int i = 0;
  1929.     FILE* ofp;
  1930.     ofp = fopen("parcerOutput.txt", "w");
  1931.     if(ofp == NULL){
  1932.         printf("Failed to open parcerOutput.txt.\n");
  1933.         exit(-1);
  1934.     }
  1935.  
  1936.     while(code_table[i].op != 0){
  1937.  
  1938.         fprintf(ofp, "%d %d %d\n", code_table[i].op, code_table[i].l, code_table[i].m);
  1939.         i++;
  1940.     }
  1941.  
  1942.     fclose(ofp);
  1943. }
  1944.  
  1945. int virtualMachineMain(){
  1946.  
  1947.     FILE* ifp = fopen("parcerOutput.txt", "r");
  1948.     if(ifp == NULL){
  1949.         printf("Failed to open parcerInput.txt #2\n");
  1950.         exit(-1);
  1951.     }
  1952.     //file to print stack execution on.
  1953.     FILE* ofp = fopen("output.txt","w");
  1954.     if(ofp==NULL){
  1955.         printf("failed to open output.txt.\n");
  1956.         exit(-1);
  1957.     }
  1958.  
  1959.     storeToMemory(ifp);
  1960.     printAssembly(ofp);
  1961.     runVM(ofp);
  1962.  
  1963.     fclose(ofp);
  1964.     fclose(ifp);
  1965.  
  1966.     return 0;
  1967. }
  1968.  
  1969. void storeToMemory(FILE* ifp){
  1970.  
  1971.     int i = 0;
  1972.  
  1973.     while(fscanf(ifp, "%d %d %d", &memory[i][1], &memory[i][2], &memory[i][3]) != EOF){
  1974.         memory[i][0] = i;
  1975.         i++;
  1976.     }
  1977.  
  1978.     totalLinesOfCode = i;
  1979. }
  1980.  
  1981. void printAssembly(FILE* ofp){
  1982.  
  1983.     int i;
  1984.     char* OpAsString;
  1985.  
  1986.     fprintf(ofp, "Line\tOP\tL\tM\n");
  1987.  
  1988.     for(i = 0; i<totalLinesOfCode; i++)
  1989.     {
  1990.         switch(memory[i][1]){
  1991.  
  1992.                 case 1: OpAsString = "lit";
  1993.                         break;
  1994.                 case 2: OpAsString = "opr";
  1995.                         break;
  1996.                 case 3: OpAsString = "lod";
  1997.                         break;
  1998.                 case 4: OpAsString = "sto";
  1999.                         break;
  2000.                 case 5: OpAsString = "cal";
  2001.                         break;
  2002.                 case 6: OpAsString = "inc";
  2003.                         break;
  2004.                 case 7: OpAsString = "jmp";
  2005.                         break;
  2006.                 case 8: OpAsString = "jpc";
  2007.                         break;
  2008.                 case 9: OpAsString = "sio";
  2009.                         break;
  2010.                 case 10: OpAsString = "sio";
  2011.                          break;
  2012.         }
  2013.  
  2014.         fprintf(ofp, "%d\t%s\t%d\t%d\n", memory[i][0], OpAsString, memory[i][2], memory[i][3]);
  2015.  
  2016.     }
  2017.     fprintf(ofp, "\n");
  2018. }
  2019.  
  2020. void runVM(FILE* ofp){
  2021.  
  2022.     initializeVM(ofp);
  2023.  
  2024.     while(numOfOpr <= numOfCal){
  2025.         fetch();
  2026.         execute(ofp);
  2027.     }
  2028. }
  2029.  
  2030. void initializeVM(FILE* ofp){
  2031.  
  2032.     int i;
  2033.  
  2034.     SP = 0;
  2035.     BP = 1;
  2036.     PC = 0;
  2037.  
  2038.     initialPrint(ofp);
  2039.  
  2040.     IR.line = 0;
  2041.     IR.op = 0;
  2042.     IR.l = 0;
  2043.     IR.m = 0;
  2044.  
  2045.     for(i=0; i<MAX_STACK_HEIGHT; i++)
  2046.         Stack[i] = 0;
  2047.  
  2048.     for(i=0; i<1000; i++)
  2049.         ARLengthStroage[i] = 0;
  2050.     ARLengthStroagePointer = 0;
  2051.  
  2052.     numOfCal = 0;
  2053.     numOfOpr = 0;
  2054. }
  2055.  
  2056. void initialPrint(FILE* ofp){
  2057.     fprintf(ofp, "\t\t\t\tpc\tbp\tsp\tstack\n");
  2058.     fprintf(ofp, "initial values\t\t\t%d\t%d\t%d\n", PC, BP, SP);
  2059. }
  2060.  
  2061. void fetch(){
  2062.     IR.line = memory[PC][0];
  2063.     IR.op = memory[PC][1];
  2064.     IR.l = memory[PC][2];
  2065.     IR.m = memory[PC][3];
  2066.     PC++;
  2067. }
  2068.  
  2069. void execute(FILE* ofp){
  2070.  
  2071.     switch(IR.op){
  2072.         //Lit
  2073.         case 1: SP++;
  2074.                 Stack[SP] = IR.m;
  2075.                 ARLengthStroage[ARLengthStroagePointer]++;
  2076.                 break;
  2077.         //Opr
  2078.         case 2: ALUop();
  2079.                 break;
  2080.         //Lod
  2081.         case 3: SP++;
  2082.                 Stack[SP] = Stack[base(IR.l) + IR.m];
  2083.                 ARLengthStroage[ARLengthStroagePointer]++;
  2084.                 break;
  2085.         //Sto
  2086.         case 4: Stack[base(IR.l) + IR.m] = Stack[SP];
  2087.                 SP--;
  2088.                 ARLengthStroage[ARLengthStroagePointer]--;
  2089.                 break;
  2090.         //Cal
  2091.         case 5: Stack[SP + 1] = 0;
  2092.                 Stack[SP + 2] = base(IR.l);
  2093.                 Stack[SP + 3] = BP;
  2094.                 Stack[SP + 4] = PC;
  2095.                 BP = SP + 1;
  2096.                 PC = IR.m;
  2097.                 ARLengthStroagePointer++;
  2098.                 ARLengthStroage[ARLengthStroagePointer] = 0;
  2099.                 numOfCal++;
  2100.                 break;
  2101.         //Inc
  2102.         case 6: SP = SP + IR.m;
  2103.                 ARLengthStroage[ARLengthStroagePointer] += IR.m;
  2104.                 break;
  2105.         //Jmp
  2106.         case 7: PC = IR.m;
  2107.                 break;
  2108.         //Jpc
  2109.         case 8: if (Stack[SP]==0)
  2110.                     PC = IR.m;
  2111.                 SP--;
  2112.                 ARLengthStroage[ARLengthStroagePointer]--;
  2113.                 break;
  2114.         //Sio
  2115.         case 9: printf("%d\n", Stack[SP]);
  2116.                 SP--;
  2117.                 ARLengthStroage[ARLengthStroagePointer]--;
  2118.                 break;
  2119.         //Sio
  2120.         case 10: SP++;
  2121.                  printf("Please enter the value to put on the stack:\t");
  2122.                  scanf("%d", &Stack[SP]);
  2123.                  printf("\n");
  2124.                  ARLengthStroage[ARLengthStroagePointer]++;
  2125.                  break;
  2126.     }
  2127.  
  2128.     print(ofp);
  2129. }
  2130.  
  2131. void ALUop(){
  2132.  
  2133.     switch(IR.m){
  2134.         //Ret
  2135.         case 0: SP = BP - 1;
  2136.                 PC = Stack[SP + 4];
  2137.                 BP = Stack[SP + 3];
  2138.                 numOfOpr++;
  2139.                 ARLengthStroagePointer--;
  2140.                 break;
  2141.         //Neg
  2142.         case 1: Stack[SP] = 0 - Stack[SP];
  2143.                 break;
  2144.         //Add
  2145.         case 2: SP--;
  2146.                 Stack[SP] = Stack[SP] + Stack[SP + 1];
  2147.                 ARLengthStroage[ARLengthStroagePointer]--;
  2148.                 break;
  2149.         //Sub
  2150.         case 3: SP--;
  2151.                 Stack[SP] = Stack[SP] - Stack[SP + 1];
  2152.                 ARLengthStroage[ARLengthStroagePointer]--;
  2153.                 break;
  2154.         //Mul
  2155.         case 4: SP--;
  2156.                 Stack[SP] = Stack[SP] * Stack[SP + 1];
  2157.                 ARLengthStroage[ARLengthStroagePointer]--;
  2158.                 break;
  2159.         //Div
  2160.         case 5: SP--;
  2161.                 Stack[SP] = Stack[SP] / Stack[SP + 1];
  2162.                 ARLengthStroage[ARLengthStroagePointer]--;
  2163.                 break;
  2164.         //Odd
  2165.         case 6: Stack[SP] = Stack[SP] % 2;
  2166.                 break;
  2167.         //Mod
  2168.         case 7: SP--;
  2169.                 Stack[SP] = Stack[SP] % Stack[SP + 1];
  2170.                 ARLengthStroage[ARLengthStroagePointer]--;
  2171.                 break;
  2172.         //Eql
  2173.         case 8: SP--;
  2174.                 Stack[SP] = (Stack[SP] == Stack[SP + 1]);
  2175.                 ARLengthStroage[ARLengthStroagePointer]--;
  2176.                 break;
  2177.         //Neq
  2178.         case 9: SP--;
  2179.                 Stack[SP] = (Stack[SP] != Stack[SP + 1]);
  2180.                 ARLengthStroage[ARLengthStroagePointer]--;
  2181.                 break;
  2182.         //Lss
  2183.         case 10: SP--;
  2184.                  Stack[SP] = (Stack[SP] < Stack[SP + 1]);
  2185.                  ARLengthStroage[ARLengthStroagePointer]--;
  2186.                  break;
  2187.         //Leq
  2188.         case 11: SP--;
  2189.                  Stack[SP] = (Stack[SP] <= Stack[SP + 1]);
  2190.                  ARLengthStroage[ARLengthStroagePointer]--;
  2191.                  break;
  2192.         //Gtr
  2193.         case 12: SP--;
  2194.                  Stack[SP] = (Stack[SP] > Stack[SP + 1]);
  2195.                  ARLengthStroage[ARLengthStroagePointer]--;
  2196.                  break;
  2197.         //Geq
  2198.         case 13: SP--;
  2199.                  Stack[SP] = (Stack[SP] >= Stack[SP + 1]);
  2200.                  ARLengthStroage[ARLengthStroagePointer]--;
  2201.                  break;
  2202.     }
  2203. }
  2204.  
  2205. int base(l){
  2206.  
  2207.     int b1 = BP;
  2208.  
  2209.     while(l>0){
  2210.  
  2211.         b1 = Stack[b1 + 1];
  2212.         l--;
  2213.     }
  2214.  
  2215.     return b1;
  2216. }
  2217.  
  2218. void print(FILE* ofp){
  2219.  
  2220.     char* OpAsString;
  2221.  
  2222.     switch(IR.op){
  2223.  
  2224.                 case 1: OpAsString = "lit";
  2225.                         break;
  2226.                 case 2: OpAsString = "opr";
  2227.                         break;
  2228.                 case 3: OpAsString = "lod";
  2229.                         break;
  2230.                 case 4: OpAsString = "sto";
  2231.                         break;
  2232.                 case 5: OpAsString = "cal";
  2233.                         break;
  2234.                 case 6: OpAsString = "inc";
  2235.                         break;
  2236.                 case 7: OpAsString = "jmp";
  2237.                         break;
  2238.                 case 8: OpAsString = "jpc";
  2239.                         break;
  2240.                 case 9: OpAsString = "sio";
  2241.                         break;
  2242.                 case 10: OpAsString = "sio";
  2243.                          break;
  2244.         }
  2245.  
  2246.     fprintf(ofp, "%d\t%s\t%d\t%d\t%d\t%d\t%d\t", IR.line, OpAsString, IR.l, IR.m, PC, BP, SP);
  2247.     printStack(ofp);
  2248.     fprintf(ofp, "\n");
  2249. }
  2250.  
  2251. void printStack(FILE* ofp){
  2252.  
  2253.     int i, j, stackPos=0, ARsize;
  2254.  
  2255.     for ( i=0; i<=ARLengthStroagePointer; ++i )
  2256.     {
  2257.         ARsize = ARLengthStroage[i];
  2258.         if ( i != 0 )
  2259.         {
  2260.             fprintf(ofp, "| ");
  2261.             if ( ARsize == 0 )
  2262.             {
  2263.                 // we know we are in a call so print out 4 items
  2264.                 ARsize = 4;
  2265.             }
  2266.         }
  2267.  
  2268.         for ( j=0; j<ARsize; ++j )
  2269.         {
  2270.             fprintf(ofp, "%d ", Stack[j+stackPos+1]);
  2271.         }
  2272.  
  2273.         stackPos += ARsize;
  2274.     }
  2275. }
  2276.  
  2277. void printParcerOutput(){
  2278.  
  2279.     char temp;
  2280.  
  2281.     FILE* ifp;
  2282.     ifp = fopen("parcerOutput.txt", "r");
  2283.     if(ifp == NULL){
  2284.         printf("failed to open parcerOutput.txt.\n");
  2285.         exit(-1);
  2286.     }
  2287.  
  2288.     while((temp = fgetc(ifp))!= EOF){
  2289.         printf("%c", temp);
  2290.     }
  2291.  
  2292.     printf("\n");
  2293.  
  2294.     fclose(ifp);
  2295. }
  2296.  
  2297. void printOutput(){
  2298.  
  2299.     char temp;
  2300.  
  2301.     FILE* ifp;
  2302.     ifp = fopen("output.txt", "r");
  2303.     if(ifp == NULL){
  2304.         printf("failed to open output.txt.\n");
  2305.         exit(-1);
  2306.     }
  2307.  
  2308.     while((temp = fgetc(ifp))!= EOF){
  2309.         printf("%c", temp);
  2310.     }
  2311.  
  2312.     printf("\n");
  2313.  
  2314.     fclose(ifp);
  2315. }
  2316.  
  2317. void checkRepeat(){
  2318.  
  2319.     int i;
  2320.  
  2321.     if(Kind == VARIABLE){
  2322.         for(i=MAX_SYMBOL_TABLE_SIZE-1; i>=0; i--){
  2323.             if(strcmp(Name, symbol_table[i].name)==0)
  2324.                 if(Level == symbol_table[i].level)
  2325.                     error(ERR_REPEAT);
  2326.         }
  2327.     }
  2328.  
  2329.     else{
  2330.  
  2331.         for(i=MAX_SYMBOL_TABLE_SIZE-1; i>=0; i--)
  2332.             if(strcmp(Name, symbol_table[i].name)==0)
  2333.                     error(ERR_REPEAT);
  2334.     }
  2335.  
  2336. }
  2337.  
  2338. void testPrint(){
  2339.  
  2340.     int i=0;
  2341.     node* temp;
  2342.  
  2343.     for(i=0; i<10; i++){
  2344.  
  2345.         if(symbol_table[i].kind == VARIABLE){
  2346.  
  2347.             temp = symbol_table[i].head;
  2348.             printf("%s:", symbol_table[i].name);
  2349.             while(temp!=NULL){
  2350.                 printf("%s ", temp->procName);
  2351.                 temp = temp->next;
  2352.             }
  2353.             printf("\n");
  2354.         }
  2355.  
  2356.     }
  2357.  
  2358.     printf("\n");
  2359.  
  2360. }
  2361.  
  2362. node* generateLinkedList(){
  2363.  
  2364.     node* temp = Head;
  2365.     node* tempSymbolTemp;
  2366.     node* tempSymbolHead =  malloc(sizeof(node));
  2367.     node* follow;
  2368.     tempSymbolTemp = tempSymbolHead;
  2369.  
  2370.  
  2371.     while(temp != NULL){
  2372.  
  2373.         strcpy(tempSymbolTemp->procName, temp->procName);
  2374.         tempSymbolTemp->next = malloc(sizeof(node));
  2375.         follow = tempSymbolTemp;
  2376.         temp = temp->next;
  2377.         tempSymbolTemp = tempSymbolTemp->next;
  2378.     }
  2379.  
  2380.     follow->next = NULL;
  2381.  
  2382.     return tempSymbolHead;
  2383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement