Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. /*
  2. Simple +/-/* expression language;
  3. parser evaluates constant expressions on the fly
  4. */
  5.  
  6. package cup.example;
  7.  
  8. import java_cup.runtime.*;
  9. import cup.example.Lexer;
  10. import java.io.IOException;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13.  
  14. parser code {:
  15. protected Lexer lexer;
  16. :}
  17.  
  18. /* define how to connect to the scanner! */
  19. init with {:
  20. ComplexSymbolFactory f = new ComplexSymbolFactory();
  21. symbolFactory = f;
  22. File file = new File("input.txt");
  23. FileInputStream fis = null;
  24. try {
  25. fis = new FileInputStream(file);
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. lexer = new Lexer(f,fis);
  30. :};
  31. scan with {: return lexer.next_token(); :};
  32.  
  33. /* Terminals (tokens returned by the scanner). */
  34. terminal LCB, RCB, LSQB, RSQB, COMMA, COLON, IDENTIFIER;
  35. terminal Integer NUMBER;
  36. terminal Boolean BOOLEAN;
  37. terminal String STRING;
  38.  
  39. /* Non terminals */
  40. // non terminal expr_list;
  41. non terminal Integer value; // used to store evaluated subexpressions
  42.  
  43. /* Precedences */
  44. precedence left LCB, RCB, LSQB, RSQB, COMMA, COLON;
  45. precedence left NUMBER, BOOLEAN, STRING;
  46.  
  47. obj ::= LCB keyValList:k RCB {: RESULT = new Map :}
  48. | LCB RCB {: RESULT = {} :}
  49. ;
  50.  
  51. keyValList ::= ListElem:l {: RESULT = l :}
  52. | ListElem COMMA {: :}
  53. ;
  54.  
  55. listElem ::= ListElem COMMA keyValue {: :}
  56. | keyValue {: :}
  57. ;
  58.  
  59. keyValue ::= IDENTIFIER COLON value {: :}
  60. | STRING COLON value {: :}
  61. ;
  62.  
  63. value ::= NUMBER:n {: System.out.println(n); RESULT = n; :}
  64. | obj:n {: System.out.println(n); RESULT = n; :}
  65. | BOOLEAN:n {: System.out.println(n); RESULT = n; :}
  66. | STRING:n {: System.out.println(n); RESULT = n; :}
  67. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement