Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. //
  2. // main.c
  3. // BetterParsing
  4. //
  5. // Created by Victor Martins on 31/05/19.
  6. // Copyright © 2019 Victor Martins. All rights reserved.
  7. //
  8.  
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. #include <string.h>
  12.  
  13. #define msg_invalidComm "Comando inválido"
  14. #define show(s, idx) printf("%s %d\n", s, idx)
  15.  
  16. static void error (const char *msg, int line) {
  17. fprintf(stderr, "%s na linha %d\n", msg, line);
  18. exit(EXIT_FAILURE);
  19. }
  20.  
  21. int main (void) {
  22. int line = 1;
  23. int currentChar;
  24. /// Flag usada para checar a presença de um comentário
  25. char commentFlag; // TODO: Considerar remover
  26. FILE *inputFile;
  27.  
  28. if ((inputFile = fopen ("fact", "r")) == NULL) {
  29. perror ("Não foi possível abrir o arquivo!");
  30. exit(1);
  31. }
  32.  
  33. while ((currentChar = fgetc(inputFile)) != EOF) {
  34. switch (currentChar) {
  35. case 'r': { /* retorno */
  36. /// Tipo do valor a ser retornado
  37. char varType;
  38. /// Índice da variável ou valor da constante a ser retornada
  39. int idxOrConst;
  40. // Lê o final da instrução procurando por 1 valor
  41. if (fscanf(inputFile, "et %c%d", &varType, &idxOrConst) != 2)
  42. error(msg_invalidComm, line);
  43.  
  44. switch (varType) {
  45. case '$': show("Constante", idxOrConst); break;
  46. case 'v': show("Variável", idxOrConst); break;
  47. default: error(msg_invalidComm, line);
  48. }
  49. printf("%d | ret %c%d\n", line, varType, idxOrConst);
  50. break;
  51. }
  52. case 'v': { /* Atribuição ou Op. Aritmética */
  53. /// Índice da variável que vai receber um novo valor
  54. int destIdx;
  55. /// Tipo da expressão (atribuição ou operação aritmética)
  56. char asignmOrEq;
  57.  
  58. // Lê mais uma parte da instrução procurando um índice e
  59. // um operador ('<' ou '=').
  60. if (fscanf(inputFile, "%d %c", &destIdx, &asignmOrEq) != 2)
  61. error(msg_invalidComm, line);
  62.  
  63. if (asignmOrEq == '<') { /* Atribuição */
  64. /// Tipo do valor a ser atribuído
  65. char srcType;
  66. /// Índice ou a constante a ser atribuída
  67. int idxOrConst;
  68.  
  69. // Lê o restante do comando procurando por 1 operando
  70. if (fscanf(inputFile, " %c%d", &srcType, &idxOrConst) != 2)
  71. error(msg_invalidComm, line);
  72.  
  73. switch (srcType) {
  74. case 'v': show("Variável", idxOrConst); break;
  75. case 'p': show("Parâmetro", idxOrConst); break;
  76. case '$': show("Constante", idxOrConst); break;
  77. default: error(msg_invalidComm, line);
  78. }
  79. printf("%d | v%d < %c%d\n", line, destIdx, srcType, idxOrConst);
  80. }
  81. else if (asignmOrEq == '=') { /* Operação Aritmética */
  82. /// Operador aritmético
  83. char op;
  84. /// Tipo dos valores a serem manipulados
  85. char fstType, secType;
  86. /// Índices ou constantes a serem manipulados
  87. int fstIdxOrConst, secIdxOrConst;
  88.  
  89. // Lê o restante do comando procurando por dois valores e um
  90. // operando entre eles.
  91. if (fscanf(inputFile, " %c%d %c %c%d", &fstType, &fstIdxOrConst, &op, &secType, &secIdxOrConst) != 5)
  92. error(msg_invalidComm, line);
  93.  
  94.  
  95. printf("%d | v%d = %c%d %c %c%d\n",
  96. line, destIdx, fstType, fstIdxOrConst, op, secType, secIdxOrConst);
  97. } else {
  98. error(msg_invalidComm, line);
  99. }
  100. break;
  101. }
  102. case 'i': { /* desvio condicional */
  103. /// Índice da variável a ser checada
  104. int varIndex;
  105. /// Número da linha para a qual a execução será desviada caso
  106. /// a condicional seja verdadeira.
  107. int targetLine;
  108.  
  109. // Lê o restante do comando procurando uma variável e qual
  110. // linha ir caso necessário
  111. if (fscanf(inputFile, "flez v%d %d", &varIndex, &targetLine) != 2)
  112. error(msg_invalidComm, line);
  113. printf("%d | iflez v%d %d\n", line, varIndex, targetLine);
  114. break;
  115. }
  116. default: error("Comando desconhecido", line);
  117. }
  118. line ++;
  119. commentFlag = '\0';
  120. fscanf(inputFile, " /%1[/]%*[^\n] ", &commentFlag);
  121. // printf("scanf %d\n", fscanf(inputFile, " /%1[/]%*[^\n] ", &commentFlag));
  122. if (commentFlag != '\0') {
  123. printf("<Comentário ignorado>\n");
  124. }
  125. }
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement