fork download
  1.  
  2.  
  3.  
  4. /************************/
  5. /* generated */
  6.  
  7.  
  8.  
  9. //default define
  10. #ifndef __STDIO_H__
  11. #include <stdio.h>
  12. #endif
  13. #ifndef __STDLIB_H__
  14. #include <stdlib.h>
  15. #endif
  16. #ifndef __STRING_H__
  17. #include <string.h>
  18. #endif
  19. //
  20.  
  21. // default setting
  22. #define BUFFER_SIZE 512
  23. #define TOKEN_SIZE 512
  24. #define PG_EXIT_SCAN 0
  25. //
  26. // pg_ variables //
  27. char pg_buffer[BUFFER_SIZE];
  28. char* pg_head=0;
  29. char* pg_prev;
  30. char pg_token[TOKEN_SIZE];
  31. char* pg_save;
  32. FILE* pg_input=0;
  33. int pg_line=0;
  34. // pg_ variables //
  35. //
  36.  
  37.  
  38.  
  39. // generate by regex 0: '\+|\*|-|/'
  40. int R0_match(){
  41.  
  42. R0_ST0:
  43. switch(*pg_head){
  44. case 0: goto R0_EXIT0;
  45. case 42: pg_head++; goto R0_ST1;
  46. case 43: pg_head++; goto R0_ST2;
  47. case 45: pg_head++; goto R0_ST3;
  48. case 47: pg_head++; goto R0_ST4;
  49. default:
  50. R0_EXIT0:
  51. return 0;
  52. }
  53. R0_ST1: //final
  54. switch(*pg_head){
  55. case 0: goto R0_EXIT1;
  56. default:
  57. R0_EXIT1:
  58. return 1;
  59. }
  60. R0_ST2: //final
  61. switch(*pg_head){
  62. case 0: goto R0_EXIT2;
  63. default:
  64. R0_EXIT2:
  65. return 1;
  66. }
  67. R0_ST3: //final
  68. switch(*pg_head){
  69. case 0: goto R0_EXIT3;
  70. default:
  71. R0_EXIT3:
  72. return 1;
  73. }
  74. R0_ST4: //final
  75. switch(*pg_head){
  76. case 0: goto R0_EXIT4;
  77. default:
  78. R0_EXIT4:
  79. return 1;
  80. }
  81.  
  82. }
  83. // ------------------------ //
  84.  
  85. // generate by regex 1: '[0-9]+'
  86. int R1_match(){
  87.  
  88. R1_ST0:
  89. switch(*pg_head){
  90. case 0: goto R1_EXIT0;
  91. case 48: pg_head++; goto R1_ST1;
  92. case 57: pg_head++; goto R1_ST1;
  93. default:
  94. if(49<=*pg_head && *pg_head<=56){
  95. pg_head++; goto R1_ST1;
  96. }
  97. R1_EXIT0:
  98. return 0;
  99. }
  100. R1_ST1: //final
  101. switch(*pg_head){
  102. case 0: goto R1_EXIT1;
  103. case 48: pg_head++; goto R1_ST1;
  104. case 57: pg_head++; goto R1_ST1;
  105. default:
  106. if(49<=*pg_head && *pg_head<=56){
  107. pg_head++; goto R1_ST1;
  108. }
  109. R1_EXIT1:
  110. return 1;
  111. }
  112.  
  113. }
  114. // ------------------------ //
  115.  
  116. // generate by regex 2: '(\n|\s|\t)+'
  117. int R2_match(){
  118.  
  119. R2_ST0:
  120. switch(*pg_head){
  121. case 0: goto R2_EXIT0;
  122. case 9: pg_head++; goto R2_ST1;
  123. case 10: pg_head++; goto R2_ST2;
  124. case 32: pg_head++; goto R2_ST3;
  125. default:
  126. R2_EXIT0:
  127. return 0;
  128. }
  129. R2_ST1: //final
  130. switch(*pg_head){
  131. case 0: goto R2_EXIT1;
  132. case 9: pg_head++; goto R2_ST1;
  133. case 10: pg_head++; goto R2_ST2;
  134. case 32: pg_head++; goto R2_ST3;
  135. default:
  136. R2_EXIT1:
  137. return 1;
  138. }
  139. R2_ST2: //final
  140. switch(*pg_head){
  141. case 0: goto R2_EXIT2;
  142. case 9: pg_head++; goto R2_ST1;
  143. case 10: pg_head++; goto R2_ST2;
  144. case 32: pg_head++; goto R2_ST3;
  145. default:
  146. R2_EXIT2:
  147. return 1;
  148. }
  149. R2_ST3: //final
  150. switch(*pg_head){
  151. case 0: goto R2_EXIT3;
  152. case 9: pg_head++; goto R2_ST1;
  153. case 10: pg_head++; goto R2_ST2;
  154. case 32: pg_head++; goto R2_ST3;
  155. default:
  156. R2_EXIT3:
  157. return 1;
  158. }
  159.  
  160. }
  161. // ------------------------ //
  162.  
  163. int update_token(int size){
  164. int i;
  165. for(i=0;i<size; i++){
  166. pg_token[i]=*pg_head++;
  167. }
  168. pg_token[i]=0;
  169. }
  170. int pg_lex(){
  171. int pg_sel,pg_rec,pg_i;
  172. int (*pg_rex_funcs[3])()={
  173. R0_match,
  174. R1_match,
  175. R2_match
  176. };
  177. if(!pg_input)pg_input = stdin;
  178. if(!pg_head)pg_head = pg_buffer;
  179. while(1){pg_sel=-1,pg_rec=-1;
  180. if(!*pg_head){
  181. if(fgets(pg_buffer,BUFFER_SIZE,pg_input)!=NULL){
  182. pg_head = pg_buffer; pg_line++;
  183. }else{
  184. return PG_EXIT_SCAN;
  185. }
  186. }
  187. //matching
  188. for(pg_i=0;pg_i<3;pg_i++){
  189. pg_prev=pg_head;//save
  190. if(pg_rex_funcs[pg_i]()){
  191. if(pg_rec<pg_head-pg_prev){
  192. pg_sel=pg_i;
  193. pg_rec=pg_head-pg_prev;
  194. }
  195. }
  196. pg_head=pg_prev;//repair
  197. }
  198. //no match
  199. if(pg_sel==-1){
  200. puts("[error] not matched");exit(1);
  201. }
  202. //token update
  203. for(pg_i=0;pg_i<pg_rec; pg_i++){
  204. pg_token[pg_i]=*pg_head++;
  205. }
  206. pg_token[pg_i]=0;
  207. //run usr code
  208. switch(pg_sel){
  209. case 0:
  210. /* usr code */
  211. printf("operator: %s\n", pg_token);
  212. /* usr code */
  213. break;
  214.  
  215. case 1:
  216. /* usr code */
  217. printf("digit token: %s\n", pg_token);
  218. /* usr code */
  219. break;
  220.  
  221. case 2:
  222. /* usr code */
  223. // skip
  224. /* usr code */
  225. break;
  226.  
  227. }
  228. pg_head=pg_prev+pg_rec;
  229. }
  230. }
  231. /* generated */
  232. /************************/
  233.  
  234.  
  235. int main(void)
  236. {
  237. pg_lex();
  238. return 0;
  239. }
  240.  
Success #stdin #stdout 0.01s 1724KB
stdin
18+18 - 18* 18/18
stdout
digit token: 18
operator: +
digit token: 18
operator: -
digit token: 18
operator: *
digit token: 18
operator: /
digit token: 18