Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. /*
  2. * The scanner definition for COOL.
  3. */
  4.  
  5. import java_cup.runtime.Symbol;
  6.  
  7. %%
  8.  
  9. %{
  10.  
  11. /* Stuff enclosed in %{ %} is copied verbatim to the lexer class
  12. * definition, all the extra variables/functions you want to use in the
  13. * lexer actions should go here. Don't remove or modify anything that
  14. * was there initially. */
  15.  
  16. // Max size of string constants
  17. static int MAX_STR_CONST = 1025;
  18.  
  19. // For assembling string constants
  20. StringBuffer string_buf = new StringBuffer();
  21.  
  22. private int curr_lineno = 1;
  23. private int comment_level = 0;
  24.  
  25. int get_curr_lineno() {
  26. return curr_lineno;
  27. }
  28.  
  29. private AbstractSymbol filename;
  30.  
  31. void set_filename(String fname) {
  32. filename = AbstractTable.stringtable.addString(fname);
  33. }
  34.  
  35. AbstractSymbol curr_filename() {
  36. return filename;
  37. }
  38. %}
  39.  
  40. %init{
  41.  
  42. /* Stuff enclosed in %init{ %init} is copied verbatim to the lexer
  43. * class constructor, all the extra initialization you want to do should
  44. * go here. Don't remove or modify anything that was there initially. */
  45.  
  46. // empty for now
  47. %init}
  48.  
  49. %eofval{
  50.  
  51. /* Stuff enclosed in %eofval{ %eofval} specifies java code that is
  52. * executed when end-of-file is reached. If you use multiple lexical
  53. * states and want to do something special if an EOF is encountered in
  54. * one of those states, place your code in the switch statement.
  55. * Ultimately, you should return the EOF symbol, or your lexer won't
  56. * work. */
  57.  
  58. switch(yy_lexical_state) {
  59. case YYINITIAL:
  60. /* nothing special to do in the initial state */
  61. break;
  62. /* If necessary, add code for other states here, e.g:
  63. case COMMENT:
  64. ...
  65. break;
  66. */
  67. }
  68. return new Symbol(TokenConstants.EOF);
  69. %eofval}
  70.  
  71. %class CoolLexer
  72. %cup
  73. %state BLOCK_COMMENT
  74. %state LINE_COMMENT
  75. %state STRING
  76. %state STRING_BACKSLASH
  77.  
  78. CHARACTERS = [^\0]
  79. WHITESPACE = [ \n\f\r\t\x0B]
  80. IDENTIFIER = [a-zA-Z_0-9]*
  81. TYPE_IDENTIFIER = [A-Z]{IDENTIFIER}
  82. OBJECT_IDENTIFIER = [a-z]{IDENTIFIER}
  83.  
  84. DARROW = "=>"
  85. LE = "<="
  86. ASSIGN = "<-"
  87.  
  88.  
  89. A = [aA]
  90. B = [bB]
  91. C = [cC]
  92. D = [dD]
  93. E = [eE]
  94. F = [fF]
  95. G = [gG]
  96. H = [hH]
  97. I = [iI]
  98. J = [jJ]
  99. K = [kK]
  100. L = [lL]
  101. M = [mM]
  102. N = [nN]
  103. O = [oO]
  104. P = [pP]
  105. Q = [qQ]
  106. R = [rR]
  107. S = [sS]
  108. T = [tT]
  109. U = [uU]
  110. V = [vV]
  111. W = [wW]
  112. X = [xX]
  113. Y = [yY]
  114. Z = [zZ]
  115. %%
  116. <YYINITIAL>"(*" { yybegin(BLOCK_COMMENT); comment_level++; }
  117. <YYINITIAL>-- { yybegin(LINE_COMMENT); }
  118. <BLOCK_COMMENT>"*)" {
  119. comment_level--;
  120. if (comment_level == 0) yybegin(YYINITIAL);
  121. }
  122. <BLOCK_COMMENT>. {}
  123. <BLOCK_COMMENT>\n { curr_lineno++; }
  124. <LINE_COMMENT>. {}
  125. <LINE_COMMENT>\n { yybegin(YYINITIAL); curr_lineno++; }
  126.  
  127.  
  128.  
  129. <YYINITIAL>\" { string_buf.setLength(0); yybegin(STRING); }
  130. <STRING>\" { yybegin(YYINITIAL); return new Symbol(TokenConstants.STR_CONST, AbstractTable.stringtable.addString(string_buf.toString())); }
  131. <STRING>\\ { yybegin(STRING_BACKSLASH); }
  132. <STRING>"\n" { string_buf.append('\n'); }
  133. <STRING>"\b" { string_buf.append('\b'); }
  134. <STRING>"\t" { string_buf.append('\t'); }
  135. <STRING>"\f" { string_buf.append('\f'); }
  136. <STRING>\n { yybegin(YYINITIAL); return new Symbol(TokenConstants.ERROR, "Unterminated string constant"); }
  137. <STRING>\0 { yybegin(YYINITIAL); return new Symbol(TokenConstants.ERROR, "String contain null character"); }
  138. <STRING>[^\0\n] { string_buf.append(yytext()); }
  139. <STRING_BACKSLASH>\n { yybegin(STRING); }
  140. <STRING_BACKSLASH>[^\0\n] { yybegin(STRING); string_buf.append(yytext()); }
  141. <STRING_BACKSLASH>\0 { yybegin(YYINITIAL); return new Symbol(TokenConstants.ERROR, "String contain null character"); }
  142.  
  143.  
  144.  
  145. <YYINITIAL>{C}{L}{A}{S}{S} { return new Symbol(TokenConstants.CLASS); }
  146. <YYINITIAL>{E}{L}{S}{E} { return new Symbol(TokenConstants.ELSE); }
  147. <YYINITIAL>f{A}{L}{S}{E} { return new Symbol(TokenConstants.BOOL_CONST, false); }
  148. <YYINITIAL>{F}{I} { return new Symbol(TokenConstants.FI); }
  149. <YYINITIAL>{I}{F} { return new Symbol(TokenConstants.IF); }
  150. <YYINITIAL>{I}{N} { return new Symbol(TokenConstants.IN); }
  151. <YYINITIAL>{I}{N}{H}{E}{R}{I}{T}{S} { return new Symbol(TokenConstants.INHERITS); }
  152. <YYINITIAL>{I}{S}{V}{O}{I}{D} { return new Symbol(TokenConstants.ISVOID); }
  153. <YYINITIAL>{L}{E}{T} { return new Symbol(TokenConstants.LET); }
  154. <YYINITIAL>{L}{O}{O}{P} { return new Symbol(TokenConstants.LOOP); }
  155. <YYINITIAL>{P}{O}{O}{L} { return new Symbol(TokenConstants.POOL); }
  156. <YYINITIAL>{T}{H}{E}{N} { return new Symbol(TokenConstants.THEN); }
  157. <YYINITIAL>{W}{H}{I}{L}{E} { return new Symbol(TokenConstants.WHILE); }
  158. <YYINITIAL>{C}{A}{S}{E} { return new Symbol(TokenConstants.CASE); }
  159. <YYINITIAL>{E}{S}{A}{C} { return new Symbol(TokenConstants.ESAC); }
  160. <YYINITIAL>{N}{E}{W} { return new Symbol(TokenConstants.NEW); }
  161. <YYINITIAL>{O}{F} { return new Symbol(TokenConstants.OF); }
  162. <YYINITIAL>{N}{O}{T} { return new Symbol(TokenConstants.NOT); }
  163. <YYINITIAL>t{R}{U}{E} { return new Symbol(TokenConstants.BOOL_CONST, true); }
  164.  
  165. <YYINITIAL> "." { return new Symbol(TokenConstants.DOT); }
  166. <YYINITIAL> "@" { return new Symbol(TokenConstants.AT); }
  167. <YYINITIAL> "~" { return new Symbol(TokenConstants.NEG); }
  168. <YYINITIAL> "*" { return new Symbol(TokenConstants.MULT); }
  169. <YYINITIAL> "/" { return new Symbol(TokenConstants.DIV); }
  170. <YYINITIAL> "+" { return new Symbol(TokenConstants.PLUS); }
  171. <YYINITIAL> "-" { return new Symbol(TokenConstants.MINUS); }
  172. <YYINITIAL> "<" { return new Symbol(TokenConstants.LT); }
  173. <YYINITIAL> "=" { return new Symbol(TokenConstants.EQ); }
  174. <YYINITIAL> {DARROW} { return new Symbol(TokenConstants.DARROW); }
  175. <YYINITIAL> {ASSIGN} { return new Symbol(TokenConstants.ASSIGN); }
  176. <YYINITIAL> {LE} { return new Symbol(TokenConstants.LE); }
  177. <YYINITIAL> ";" { return new Symbol(TokenConstants.SEMI); }
  178. <YYINITIAL> "," { return new Symbol(TokenConstants.COMMA); }
  179. <YYINITIAL> ":" { return new Symbol(TokenConstants.COLON); }
  180. <YYINITIAL> "{" { return new Symbol(TokenConstants.LBRACE); }
  181. <YYINITIAL> "}" { return new Symbol(TokenConstants.RBRACE); }
  182. <YYINITIAL> "(" { return new Symbol(TokenConstants.LPAREN); }
  183. <YYINITIAL> ")" { return new Symbol(TokenConstants.RPAREN); }
  184.  
  185.  
  186. <YYINITIAL>[0-9]+ {
  187. return new Symbol(TokenConstants.INT_CONST, AbstractTable.inttable.addString(yytext()));
  188. }
  189.  
  190. <YYINITIAL>{TYPE_IDENTIFIER} {
  191. return new Symbol(TokenConstants.TYPEID, AbstractTable.idtable.addString(yytext()));
  192. }
  193.  
  194. <YYINITIAL>{OBJECT_IDENTIFIER} {
  195. return new Symbol(TokenConstants.OBJECTID, AbstractTable.idtable.addString(yytext()));
  196. }
  197.  
  198. " "|\r|\n|\t {
  199. }
  200.  
  201. \n { ++curr_lineno; }
  202. <YYINITIAL> {WHITESPACE} { }
  203.  
  204.  
  205. . { /* This rule should be the very last
  206. in your lexical specification and
  207. will match match everything not
  208. matched by other lexical rules. */
  209. System.err.println("LEXER BUG - UNMATCHED: " + yytext() + " Char at 0" + (int) yytext().charAt(0)); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement