lexical.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "__virtualmath.h"
  2. /**
  3. * 从文件中读取一个字节,并处理is_back
  4. * 每次从文件中读取字符时,则会保存字符到back.p中,调用backChar回退一个字符的时候则不需要传入字符了
  5. * @param file
  6. * @return 返回一个字符,若为EOF则返回-1
  7. */
  8. int readChar(LexFile *file){
  9. if (file->back.is_back)
  10. file->back.is_back = false;
  11. else
  12. file->back.p = fgetc(file->file);
  13. if (file->back.p == '\n')
  14. file->line++;
  15. return file->back.p;
  16. }
  17. /**
  18. * 设置字符回退
  19. * @param file
  20. */
  21. void backChar(LexFile *file){
  22. file->back.is_back = true;
  23. if (file->back.p == '\n')
  24. file->line --;
  25. }
  26. void clearLexFile(LexFile *file) {
  27. int ch;
  28. backChar(file);
  29. while ((ch = readChar(file)) != '\n' && ch != EOF)
  30. PASS;
  31. }
  32. LexFile *makeLexFile(char *dir){
  33. LexFile *tmp = memCalloc(1, sizeof(LexFile));
  34. tmp->is_std = (bool)(dir == NULL);
  35. if (tmp->is_std)
  36. tmp->file = stdin;
  37. else
  38. tmp->file = fopen(dir, "r");
  39. tmp->back.is_back = false;
  40. tmp->back.p = EOF;
  41. tmp->line = 1;
  42. tmp->filter_data.enter = 0;
  43. return tmp;
  44. }
  45. void freeLexFile(LexFile *file) {
  46. FREE_BASE(file, return_);
  47. if (!file->is_std)
  48. fclose(file->file);
  49. memFree(file);
  50. return_:
  51. return;
  52. }
  53. /**
  54. * 初始化mather,代码被复用
  55. * @param mather
  56. */
  57. void setupMather(LexMather *mather){
  58. mather->len = 0;
  59. mather->ascii = 0;
  60. mather->str = NULL;
  61. mather->second_str = NULL;
  62. mather->string_type = '"';
  63. mather->status = LEXMATHER_START;
  64. }
  65. LexMather *makeMather(){
  66. LexMather *tmp = memCalloc(1, sizeof(LexMather));
  67. setupMather(tmp);
  68. return tmp;
  69. }
  70. void freeMather(LexMather *mather) {
  71. memFree(mather->str);
  72. memFree(mather->second_str);
  73. mather->len = 0;
  74. memFree(mather);
  75. }
  76. LexMathers *makeMathers(int size){
  77. LexMathers *tmp = memCalloc(1, sizeof(LexMathers));
  78. tmp->size = size;
  79. tmp->mathers = (struct LexMather**)memCalloc(size, sizeof(LexMather*));
  80. for(int i=0;i < size; i++)
  81. tmp->mathers[i] = makeMather();
  82. return tmp;
  83. }
  84. void freeMathers(LexMathers *mathers) {
  85. FREE_BASE(mathers, return_);
  86. for(int i=0;i < mathers->size; i++)
  87. freeMather(mathers->mathers[i]);
  88. memFree(mathers->mathers);
  89. mathers->size = 0;
  90. memFree(mathers);
  91. return_:
  92. return;
  93. }
  94. /**
  95. * 初始化mathers,本质是初始化mathers.mathers内所有的mather
  96. * @param mathers
  97. */
  98. void setupMathers(LexMathers *mathers){
  99. for (int i=0;i < mathers->size;i++){
  100. if(mathers->mathers[i]->str != NULL){
  101. memFree(mathers->mathers[i]->str);
  102. memFree(mathers->mathers[i]->second_str);
  103. }
  104. setupMather(mathers->mathers[i]);
  105. }
  106. }
  107. /**
  108. * 检查mathers中mather的匹配情况。
  109. * 情况1:只出现一个匹配器处于END状态,其他均处于MISTAKE或者END_SECOND状态,则视为匹配成功,返回END状态的匹配器
  110. * 情况2:只出现一个匹配器处于END_SECOND状态,其他均处于MISTAKE状态无END状态,则视为匹配成功,返回END_SECOND状态的匹配器
  111. * 情况3:全部都在MISTAKE,返回-2,匹配失败
  112. * 其他情况:匹配还未完成,返回-1
  113. * @param mathers
  114. * @param max
  115. * @return
  116. */
  117. int checkoutMather(LexMathers *mathers, int max) {
  118. bool return_1 = false;
  119. int mistake_count = 0;
  120. int end_count = 0, end_index = -1;
  121. int end_second_count = 0, end_second_index = -1;
  122. for (int i=0;i < mathers->size;i++){
  123. if(mathers->mathers[i]->status == LEXMATHER_END_1){
  124. end_count ++;
  125. end_index = i;
  126. }
  127. else if(mathers->mathers[i]->status == LEXMATHER_END_2){
  128. end_second_count ++;
  129. end_second_index = i;
  130. }
  131. else if(mathers->mathers[i]->status == LEXMATHER_MISTAKE){
  132. mistake_count ++;
  133. }
  134. else{
  135. return_1 = true;
  136. }
  137. }
  138. if (return_1)
  139. goto return_;
  140. if (mistake_count == max)
  141. return -2;
  142. else if(end_count == 1)
  143. return end_index;
  144. else if(end_second_count == 1)
  145. return end_second_index;
  146. return_:
  147. return -1;
  148. }