Exemple #1
0
 private int lbp(Token tk) {
   if (tk.type == Token.Type.NAME) {
     return 90;
   } else if (tk.type == Token.Type.NUM) {
     return 90;
   }
   Integer res = LBP.get(tk.text);
   if (res == null) {
     System.err.println(tab(depth) + "warn " + tk);
     return 0;
   } else {
     return res;
   }
 }
Exemple #2
0
  public static void main(String[] args) throws Exception {
    LBP.put("=", 7);
    LBP.put("and", 5);
    LBP.put("or", 5);
    LBP.put("<", 7);
    LBP.put(">", 7);
    LBP.put("+", 10);
    LBP.put("-", 10);
    LBP.put("*", 20);
    LBP.put("/", 20);
    LBP.put("(", 100);
    LBP.put(")", 1);
    PrattParser p = new PrattParser();
    p.debug = true;

    Reader reader = (new StringReader("-a * -b"));

    p.lexer = new LookaheadLexerWrapper(new StructuredLexer(new BaseLexer(reader), "  "));
    // p.advance();
    Expr ast = p.expr(0);
    System.out.println(ast);
  }