Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. package cup.example;
  2. import java_cup.runtime.ComplexSymbolFactory;
  3. import java_cup.runtime.ComplexSymbolFactory.Location;
  4. import java_cup.runtime.Symbol;
  5. import java.lang.*;
  6. import java.io.InputStreamReader;
  7.  
  8. %%
  9.  
  10. %class Lexer
  11. %implements sym
  12. %public
  13. %unicode
  14. %line
  15. %column
  16. %cup
  17. %char
  18. %{
  19.  
  20.  
  21. public Lexer(ComplexSymbolFactory sf, java.io.InputStream is){
  22. this(is);
  23. symbolFactory = sf;
  24. }
  25. public Lexer(ComplexSymbolFactory sf, java.io.Reader reader){
  26. this(reader);
  27. symbolFactory = sf;
  28. }
  29.  
  30. private StringBuffer sb;
  31. private ComplexSymbolFactory symbolFactory;
  32. private int csline,cscolumn;
  33.  
  34. public Symbol symbol(String name, int code){
  35. return symbolFactory.newSymbol(name, code,
  36. new Location(yyline+1,yycolumn+1, yychar), // -yylength()
  37. new Location(yyline+1,yycolumn+yylength(), yychar+yylength())
  38. );
  39. }
  40. public Symbol symbol(String name, int code, String lexem){
  41. return symbolFactory.newSymbol(name, code,
  42. new Location(yyline+1, yycolumn +1, yychar),
  43. new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
  44. }
  45.  
  46. protected void emit_warning(String message){
  47. System.out.println("scanner warning: " + message + " at : 2 "+
  48. (yyline+1) + " " + (yycolumn+1) + " " + yychar);
  49. }
  50.  
  51. protected void emit_error(String message){
  52. System.out.println("scanner error: " + message + " at : 2" +
  53. (yyline+1) + " " + (yycolumn+1) + " " + yychar);
  54. }
  55. %}
  56.  
  57. /* comments */
  58. Comment = {TraditionalComment} | {EndOfLineComment}
  59. TraditionalComment = "/*" {CommentContent} \*+ "/"
  60. EndOfLineComment = "//" [^\r\n]* {Newline}
  61. CommentContent = ( [^*] | \*+[^*/] )*
  62.  
  63. ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
  64.  
  65. %eofval{
  66. return symbolFactory.newSymbol("EOF",sym.EOF);
  67. %eofval}
  68.  
  69. %state CODESEG
  70.  
  71. Newline = \r | \n | \r\n
  72. Whitespace = [ \t\f] | {Newline}
  73. Number = [0-9]+
  74. Boolean = (true)|(false)
  75. String = \"[^\"\n]*\"|\'[^\'\n]*\'
  76. Identifier = [a-zA-Z]+
  77.  
  78. %%
  79.  
  80. <YYINITIAL> {
  81. {Number} { return symbolFactory.newSymbol("NUMBER", NUMBER, Integer.parseInt(yytext())); }
  82. {Boolean} { return symbolFactory.newSymbol("BOOLEAN", BOOLEAN, Boolean.parseBoolean(yytext())); }
  83. {String} { return symbolFactory.newSymbol("STRING", STRING, yytext()); }
  84. {Identifier} { return symbolFactory.newSymbol("IDENTIFIER", IDENTIFIER, yytext()); }
  85.  
  86. "{" { return symbolFactory.newSymbol("LCB", LCB); }
  87. "}" { return symbolFactory.newSymbol("RCB", RCB); }
  88. "[" { return symbolFactory.newSymbol("LSQB", LSQB); }
  89. "]" { return symbolFactory.newSymbol("RSQB", RSQB); }
  90. "," { return symbolFactory.newSymbol("COMMA", COMMA); }
  91. ":" { return symbolFactory.newSymbol("COLON", COLON); }
  92. }
  93.  
  94. // error fallback
  95. .|\n { emit_warning("Unrecognized character '" +yytext()+"' -- ignored"); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement