lex.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include"token.h"
  4. #include <ctype.h>
  5. #define debug 0
  6. #define is_NUM 1
  7. #define no_NUM 0
  8. #define is_STOP 2
  9. FILE *fp = NULL;
  10. typedef enum NUM_TYPE{
  11. START=0,
  12. INT=1,
  13. DOT=2,
  14. DOUBLE=3,
  15. } NUM_TYPE;
  16. Token token;
  17. char str=' ';
  18. int get_char_back = 0;
  19. void get_char(){
  20. if (get_char_back){ // 回退
  21. get_char_back = 0;
  22. }
  23. else{
  24. str = fgetc(fp);
  25. }
  26. }
  27. void unget_char(){
  28. get_char_back = 1;
  29. }
  30. void exit(int);
  31. // int isdigit(char);
  32. double atof(char *);
  33. void get_token(Token *token){
  34. static int is_number = no_NUM;
  35. token->type = NULL_TOKEN;
  36. memset(token->str,0,sizeof(MAX_TOKEN_WIDTH));
  37. token->NUMBER = 0;
  38. NUM_TYPE num_type = START; // 数字输入默认情况
  39. for (int num=0;;num += 1)
  40. {
  41. get_char(); // 取得一个输入的字符
  42. token->str[num] = str;
  43. if ((num_type == INT)&&(str == '.')){ // 小数点
  44. if (num_type == INT){
  45. num_type++;
  46. continue;
  47. }
  48. }
  49. else if ((num_type == DOT)&&(isdigit(str))){ // 小数点到小数
  50. num_type++;
  51. continue;
  52. }
  53. if (num_type != START){
  54. if (isdigit(str)){ // 整数或者小数
  55. continue;
  56. }
  57. else{
  58. token->str[num] = '\0';
  59. is_number = is_NUM;
  60. unget_char();
  61. token->NUMBER = atof(token->str);
  62. return ;
  63. }
  64. }
  65. if (str == ' '){
  66. token->str[num] = '\0';
  67. num -= 1; //num要减少一位,否则就会空出来
  68. continue;
  69. }
  70. else if ((num_type == START)&&(isdigit(str))){ // 开始写入数字
  71. token->type=NUM; // 数字模式
  72. num_type++;
  73. continue;
  74. }
  75. else if (str == '+'){
  76. token->type=ADD;
  77. is_number = no_NUM;
  78. return;
  79. }
  80. else if (str == '-'){
  81. if (is_number != is_NUM){ // 上一个不是数字,这个是负数
  82. token->type=NUM; // 数字模式
  83. num_type++;
  84. continue;
  85. }
  86. else{
  87. token->type=SUB;
  88. is_number = no_NUM;
  89. return;
  90. }
  91. }
  92. else if (str == '*'){
  93. token->type=MUL;
  94. is_number = no_NUM;
  95. return;
  96. }
  97. else if (str == '/'){
  98. token->type=DIV;
  99. is_number = no_NUM;
  100. return;
  101. }
  102. else if (str == '('){
  103. token->type=LB;
  104. is_number = no_NUM;
  105. return;
  106. }
  107. else if (str == ')'){
  108. token->type=RB;
  109. is_number = no_NUM;
  110. return;
  111. }
  112. else if ((str == '\n')|| (str == ';')){ // 停止
  113. if (is_number == is_STOP){
  114. token->type=NULL_TOKEN;
  115. return;
  116. }
  117. token->type=STOP;
  118. token->str[0] = ' ';
  119. is_number = is_STOP;
  120. return;
  121. }
  122. else if (str = '\\'){ // 多行
  123. token->type=NULL_TOKEN;
  124. token->str[0] = ' ';
  125. is_number = is_STOP;
  126. return;
  127. }
  128. else{ // 停止
  129. token->type=EXIT;
  130. token->str[0] = ' ';
  131. is_number = no_NUM;
  132. return;
  133. }
  134. }
  135. }
  136. // #ifdef 0
  137. // void debug_get_token(){
  138. // get_token(&token);
  139. // printf("type = %d, str = %s, NUMBER = %f\n", token.type, token.str, token.NUMBER);
  140. // }
  141. // int main(){
  142. // fp = stdin;
  143. // while(1){
  144. // debug_get_token();
  145. // }
  146. // return 0;
  147. // }
  148. // #endif