Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define LETTER 256
  5.  
  6. int symbol;
  7. char lexVal;
  8. int yylex();
  9.  
  10. typedef struct _node
  11. {
  12. char key;
  13. struct _node* left;
  14. struct _node* right;
  15. }node;
  16.  
  17. node* E();
  18. node* T();
  19. node* F();
  20. node* P();
  21. void error(){
  22. printf("eroare de sintaxa");
  23. exit(0);
  24. }
  25.  
  26. void next_symbol()
  27. {
  28. symbol = yylex();
  29. }
  30.  
  31. node* E()
  32. {
  33. node* resultT=T();
  34. if(symbol == '|')
  35. {
  36. next_symbol();
  37. node* result = (node*)malloc(sizeof(node));
  38. result->key = '|';
  39. result->left=resultT;
  40. result->right =E();
  41. return result;
  42. }
  43. if(symbol == '\n'||symbol==')')
  44. return resultT;
  45. error();
  46.  
  47. }
  48.  
  49.  
  50. int followF(){ return firstT()||followT();}
  51. node* T(){
  52. node* resultF;
  53. resultF = F();
  54. if(symbol=='(' || symbol==LETTER)
  55. {
  56. node* result;
  57. result = (node*)malloc(sizeof(node));
  58.  
  59. return resultT;
  60.  
  61.  
  62. result->left=resultF;
  63. result->right =T();
  64. return result;
  65. }
  66.  
  67. return resultF;
  68.  
  69. }
  70.  
  71. node* F(){
  72. node* resultP=P();
  73. if (symbol == '*'){
  74. next_symbol();
  75. node* result = (node*)malloc(sizeof(node));
  76. result->key = '*';
  77. result->left=resultP;
  78. result->right =NULL;
  79. return result;
  80.  
  81. }
  82.  
  83. return resultP;
  84.  
  85.  
  86. }
  87.  
  88.  
  89. node* P(){
  90. node* val;
  91. if (symbol==LETTER) {
  92. val=(node*)malloc(sizeof(node));
  93. val->key = lexval;
  94. val->left=NULL;
  95. val->right=NULL;
  96. next_symbol();
  97. return val;
  98. }
  99. else if (symbol=='('){
  100. next_symbol();
  101. val = E();
  102. if (symbol==')') next_symbol();
  103. else error();
  104. return val;
  105. } else error();
  106. }
  107.  
  108. parser(){
  109. printf("%d\n", E()->key);
  110. if (symbol!='\n') error();
  111. }
  112.  
  113. main(){
  114. next_symbol();
  115. while (symbol!=0){
  116. parser();
  117. next_symbol();
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement