Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. %option noyywrap yylineno
  2.  
  3. %{
  4. /* -*- mode: c -*- */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "macro.h"
  8. #include "AST.h"
  9.  
  10. #include "y.tab.h"
  11.  
  12. extern YYSTYPE yylval;
  13. extern YYLTYPE yylloc;
  14. extern int debug;
  15.  
  16. int charnum = 0;
  17. int linenum = 0;
  18.  
  19. #define YY_USER_ACTION do { yylloc.first_line = yylloc.last_line = yylineno; yylloc.first_column = charnum; } while (0);
  20. %}
  21.  
  22.  
  23. ID [_a-z]+
  24. NUM [0-9]+
  25. INVALID {ID}{NUM}|{NUM}{ID}
  26. COMMENTL \[
  27. COMMENTR \]
  28.  
  29. OPERATOR "+"|"-"|"*"|"/"|"%"
  30. COND "="|"!="|"<"|">"|"<="|">="
  31. CONDEXP IF|THEN|ELSE|ENDIF
  32. LOOP WLOOP|FLOOP
  33. WLOOP WHILE|DO|ENDWHILE
  34. FLOOP FOR|DOWN|FROM|TO|ENDFOR
  35. IO GET|PUT
  36. ASSIGN ":="
  37. DECLARE DECLARE
  38. IN IN
  39. END END
  40. SEMICOLON ";"
  41. PAR "("|")"
  42. KEYWORD {OPERATOR}|{COND}|{CONDEXP}|{LOOP}|{IO}|{ASSIGN}|{DECLARE}|{IN}|{END}|{SEMICOLON}|{PAR}|{INVALID}
  43.  
  44. %x COMMENT
  45. %x ERROR
  46.  
  47. %%
  48.  
  49. {KEYWORD} {
  50. charnum += (strlen(yytext));
  51. debug("KEYWORD %s", yytext);
  52. //yylval.string = (char *)strdup(yytext);
  53. //return yytext;
  54. REJECT;
  55. }
  56.  
  57. "+" { return PLUS; }
  58. "-" { return MINUS; }
  59. "*" { return TIMES; }
  60. "/" { return DIV; }
  61. "%" { return MOD; }
  62.  
  63. "=" { return EQ; }
  64. "!=" { return DIFF; }
  65. "<" { return LE; }
  66. ">" { return GE; }
  67. "<=" { return LEQ; }
  68. ">=" { return GEQ; }
  69.  
  70. IF { return IF; }
  71. THEN { return THEN; }
  72. ELSE { return ELSE; }
  73. ENDIF { return ENDIF; }
  74.  
  75. WHILE { return WHILE; }
  76. DO { return DO; }
  77. ENDWHILE { return ENDWHILE; }
  78.  
  79. FOR { return FOR; }
  80. DOWN { return DOWN; }
  81. FROM { return FROM; }
  82. TO { return TO; }
  83. DO { return DO; }
  84. ENDFOR { return ENDFOR; }
  85.  
  86. GET { return GET; }
  87. PUT { return PUT; }
  88.  
  89. := { return ASSIGN; }
  90.  
  91. DECLARE { return DECLARE; }
  92. IN { return IN; }
  93. END { return END; }
  94.  
  95. \( { return PARL; }
  96. \) { return PARR; }
  97.  
  98. {SEMICOLON} { return SEMICOLON; }
  99.  
  100. {INVALID} {
  101. error("%d:%d: unrecognized token \"%s\"", linenum, charnum, yytext);
  102. exit(1);
  103. }
  104.  
  105. {ID} {
  106. debug("ID %s", yytext);
  107. charnum += (strlen(yytext));
  108.  
  109. yylval.ast_node = new_stringnode('I', strdup(yytext)); // IDENTIFIER
  110. return PIDENTIFIER;
  111. }
  112.  
  113. {NUM} {
  114. debug("INTEGER %s", yytext);
  115. charnum += (strlen(yytext));
  116. //yylval = atoi(yytext);
  117.  
  118. yylval.ast_node = new_stringnode('N', strdup(yytext)); // NUM
  119. return NUM;
  120. }
  121.  
  122.  
  123. {COMMENTL} {
  124. BEGIN(COMMENT);
  125. charnum += (strlen(yytext));
  126. }
  127.  
  128. <COMMENT>\n {
  129. /* eat multiline comments */
  130. charnum = 0;
  131. ++linenum;
  132. }
  133.  
  134. <COMMENT>{COMMENTR} {
  135. BEGIN(INITIAL);
  136. charnum += (strlen(yytext));
  137. }
  138.  
  139. <COMMENT>. {
  140. /* eat multiline comments */
  141. charnum += (strlen(yytext));
  142. }
  143.  
  144. <COMMENT>{COMMENTL} {
  145. error("%d:%d: illegal nested comment",
  146. linenum, charnum);
  147. charnum += (strlen(yytext));
  148. exit(1);
  149. }
  150.  
  151. <COMMENT><<EOF>> {
  152. error("%d:%d: unexpected EOF (in comment)",
  153. linenum, charnum);
  154. BEGIN(INITIAL);
  155. exit(1);
  156. }
  157.  
  158.  
  159. [ \t] {
  160. /* eat whitespaces */
  161. charnum += (strlen(yytext));
  162. }
  163.  
  164. \n {
  165. /* eat whitespaces */
  166. charnum = 0;
  167. ++linenum;
  168. }
  169.  
  170.  
  171. . {
  172. BEGIN(ERROR);
  173. yymore();
  174. }
  175.  
  176. <ERROR>[ \t] {
  177. error("%d:%d: unrecognized token \"%s\"",
  178. linenum, charnum, yytext);
  179. BEGIN(INITIAL);
  180. charnum += (strlen(yytext));
  181. exit(1);
  182. }
  183.  
  184. <ERROR>\n {
  185. error("%d:%d: unrecognized token \"%s\"",
  186. linenum, charnum, yytext);
  187. BEGIN(INITIAL);
  188. charnum = 0;
  189. ++linenum;
  190. exit(1);
  191. }
  192.  
  193. <ERROR><<EOF>> {
  194. error("%d:%d: unrecognized token \"%s\"",
  195. linenum, charnum);
  196. exit(1);
  197. }
  198.  
  199. <ERROR>. {
  200. yymore();
  201. }
  202. %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement