antlr4 接触
1. 配置IDE(可选)
2. 加减法示例
编写expr.g4
1 grammar AddMinus; 2 3 expr: Minus Number #minusNum 4 | expr op=('+'|'-') expr #regular 5 | Number #single 6 ; 7 Number:[0-9]+; 8 Minus:'-'; 9 WS : [ \t\n\r]+ ->skip ; 10 Add:'+';
生成文件
实现listener 完成遍历
1 public static class AddMinusListener extends AddMinusBaseListener{ 2 private Stack<Integer> stack = new Stack<>(); 3 4 /** 5 * {@inheritDoc} 6 * 7 * <p>The default implementation does nothing.</p> 8 */ 9 @Override public void enterMinusNum(AddMinusParser.MinusNumContext ctx) { 10 System.out.println("enter minus: " + ctx.getText()); 11 System.out.println(ctx); 12 13 } 14 /** 15 * {@inheritDoc} 16 * 17 * <p>The default implementation does nothing.</p> 18 */ 19 @Override public void exitMinusNum(AddMinusParser.MinusNumContext ctx) { 20 stack.push(Integer.valueOf(ctx.getText())); 21 } 22 /** 23 * {@inheritDoc} 24 * 25 * <p>The default implementation does nothing.</p> 26 */ 27 @Override public void enterRegular(AddMinusParser.RegularContext ctx) { 28 System.out.println("enter regular text: " + ctx.getText()); 29 } 30 /** 31 * {@inheritDoc} 32 * 33 * <p>The default implementation does nothing.</p> 34 */ 35 @Override public void exitRegular(AddMinusParser.RegularContext ctx) { 36 if( ctx.op.getText().equals("+")){ 37 stack.push(stack.pop() + stack.pop()); 38 }else if( ctx.op.getText().equals("-")){ 39 Integer peek = stack.pop(); 40 stack.push(stack.pop() - peek); 41 } 42 43 44 } 45 /** 46 * {@inheritDoc} 47 * 48 * <p>The default implementation does nothing.</p> 49 */ 50 @Override public void enterSingle(AddMinusParser.SingleContext ctx) { } 51 /** 52 * {@inheritDoc} 53 * 54 * <p>The default implementation does nothing.</p> 55 */ 56 @Override public void exitSingle(AddMinusParser.SingleContext ctx) { 57 58 stack.push(Integer.valueOf(ctx.getText())); 59 } 60 61 /** 62 * {@inheritDoc} 63 * 64 * <p>The default implementation does nothing.</p> 65 */ 66 @Override public void enterEveryRule(ParserRuleContext ctx) { 67 68 } 69 /** 70 * {@inheritDoc} 71 * 72 * <p>The default implementation does nothing.</p> 73 */ 74 @Override public void exitEveryRule(ParserRuleContext ctx) { 75 76 } 77 /** 78 * {@inheritDoc} 79 * 80 * <p>The default implementation does nothing.</p> 81 */ 82 @Override public void visitTerminal(TerminalNode node) { 83 84 85 } 86 /** 87 * {@inheritDoc} 88 * 89 * <p>The default implementation does nothing.</p> 90 */ 91 @Override public void visitErrorNode(ErrorNode node) { } 92 93 public Integer getResult(){ 94 if (stack.size() == 1){ 95 return stack.peek(); 96 } 97 return 0; 98 } 99 }
测试代码如下:
CharStream stream = CharStreams.fromString("2+3-4"); AddMinusLexer lexer = new AddMinusLexer(stream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); AddMinusParser parser = new AddMinusParser(tokenStream); //parser.setBuildParseTree(true); AddMinusParser.ExprContext ctx = parser.expr(); System.out.println(ctx.toStringTree()); System.out.println("------------------ up string tree-------"); ParseTreeWalker walker = new ParseTreeWalker(); AddMinusListener listener = new AddMinusListener(); walker.walk(listener,ctx); System.out.println(listener.getResult());