Advertisement
kernel_memory_dump

vezba9

May 11th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include "SyntaxAnalysis.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <memory.h>
  6.  
  7. #include "SyntaxAnalysisLib.h"
  8. #include "Matrix.h"
  9. #include "Lex.h"
  10.  
  11.  
  12. char* program;
  13.  
  14. Token token;
  15. int brLinijePrveGreske = 0;
  16. int trenutniBrLinije=0;
  17. bool syntaxOK = true;
  18.  
  19.  
  20. void greska(){
  21.     if (syntaxOK){
  22.         brLinijePrveGreske = trenutniBrLinije;
  23.         syntaxOK = false;
  24.         printf("Prva greska na %d, liniji!", brLinijePrveGreske);
  25.         exit(1);
  26.     }
  27. }
  28.  
  29. char* init()
  30. {
  31.     FILE* f = fopen("program.txt", "rb");
  32.     char* buffer;
  33.  
  34.     if (f != NULL)
  35.     {
  36.         long len;
  37.         fseek(f, 0, SEEK_END);
  38.         len = ftell(f);
  39.         fseek(f, 0, SEEK_SET);
  40.  
  41.         buffer = (char*) malloc(len * sizeof(char));
  42.         memset(buffer, 0x0, len);
  43.  
  44.         fread(buffer, sizeof(char), len, f);
  45.         fclose(f);
  46.  
  47.         initFSM();
  48.  
  49.         return buffer;
  50.     }
  51.     else
  52.     {
  53.         return NULL;
  54.     }
  55. }
  56.  
  57.  
  58. void cleanUp(char* buffer)
  59. {
  60.     if (buffer != NULL)
  61.     {
  62.         free(buffer);
  63.     }
  64. }
  65.  
  66.  
  67. void eat(TokenType t)
  68. {
  69.     if (token.tokenType == t)
  70.     {
  71.         token = getNextTokenLex();
  72.     }
  73.     else
  74.     {
  75.         printSynError(token);
  76.     }
  77. }
  78.  
  79.  
  80. void Q();
  81. void S();
  82. void L();
  83. void E();
  84.  
  85.  
  86. void Q()
  87. {
  88.     if(token.tokenType == BEGIN)
  89.     {
  90.         trenutniBrLinije++;
  91.         puts(token.value);
  92.         eat(BEGIN);
  93.         S();
  94.         L();
  95.     }
  96.     else
  97.     {
  98.         printSynError(token);
  99.         greska();
  100.     }
  101. }
  102.  
  103.  
  104. void S()
  105. {
  106.     if (token.tokenType  == IF)
  107.     {
  108.         trenutniBrLinije++;
  109.         puts(token.value);
  110.         eat(IF);
  111.         E();
  112.         if (token.tokenType == THEN)
  113.         {
  114.             puts(token.value);
  115.             eat(THEN);
  116.             S();
  117.             if(token.tokenType == ELSE)
  118.             {
  119.                 puts(token.value);
  120.                 eat(ELSE);
  121.                 S();
  122.             }
  123.             else{
  124.                 printSynError(token);
  125.                 greska();
  126.             }
  127.         }
  128.         else{
  129.             printSynError(token);
  130.             greska();
  131.         }
  132.        
  133.     }
  134.     else if (token.tokenType == PRINT)
  135.     {
  136.         puts(token.value);
  137.         eat(PRINT);
  138.         E();
  139.     }
  140.     else
  141.     {
  142.         printSynError(token);
  143.         greska();
  144.     }
  145. }
  146.  
  147.  
  148. void L()
  149. {
  150.     if(token.tokenType == END)
  151.  
  152.     {
  153.         trenutniBrLinije++;
  154.         puts(token.value);
  155.         eat(END);
  156.     }
  157.     else{
  158.         S();
  159.         L();
  160.     }
  161.  
  162. }
  163.  
  164.  
  165. void E()
  166. {
  167.     if(token.tokenType == ID)
  168.     {
  169.         puts(token.value);
  170.         eat(ID);
  171.         if(token.tokenType == EQ){
  172.             puts(token.value);
  173.             eat(EQ);
  174.             if(token.tokenType == NUM){
  175.                 puts(token.value);
  176.                 eat(NUM);
  177.             }
  178.             else{
  179.                 printSynError(token);
  180.                 greska();
  181.             }
  182.         }
  183.         else{
  184.             printSynError(token);
  185.             greska();
  186.         }
  187.     }
  188.     else{
  189.         printSynError(token);
  190.         greska();
  191.     }
  192. }
  193.  
  194.  
  195. int main(int argc, char* argv[])
  196. {
  197.     //
  198.     program = init();
  199.  
  200.     token = getNextTokenLex();
  201.  
  202.     // Call function for the starting non-terminal symbol
  203.     Q();
  204.     if (syntaxOK){
  205.         puts("SYNTAX OK");
  206.     }
  207.  
  208.     // free resources
  209.     cleanUp(program);
  210.  
  211.     getchar();
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement