From 73793898a98091f7a838a825d259f2734c817718 Mon Sep 17 00:00:00 2001 From: parrt Date: Wed, 11 Jan 2012 11:21:05 -0800 Subject: [PATCH] update grammars [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9856] --- tool/playground/A.g4 | 16 ++++++++ tool/playground/TestA.java | 82 ++++++++++++++++++++++++++++++++++++++ tool/playground/U.g | 21 +++++----- 3 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 tool/playground/A.g4 create mode 100644 tool/playground/TestA.java diff --git a/tool/playground/A.g4 b/tool/playground/A.g4 new file mode 100644 index 000000000..1662f6801 --- /dev/null +++ b/tool/playground/A.g4 @@ -0,0 +1,16 @@ +grammar A; + +s : q=e {System.out.println("result = "+$e.v);} ; + +e returns [int v] + : a=e '*' b=e {$v = $a.v * $b.v;} -> mult + | a=e '+' b=e {$v = $a.v + $b.v;} -> add + | INT {$v = $INT.int;} + | '(' x=e ')' {$v = $x.v;} + | e '++' -> inc + | e '--' + | ID -> anID + ; + +INT : '0'..'9'+ ; +WS : (' '|'\n')+ {skip();} ; diff --git a/tool/playground/TestA.java b/tool/playground/TestA.java new file mode 100644 index 000000000..5b5fc3cc1 --- /dev/null +++ b/tool/playground/TestA.java @@ -0,0 +1,82 @@ +/* + [The "BSD license"] + Copyright (c) 2011 Terence Parr + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.ParseTreeWalker; + +public class TestA { + public static class Do extends BlankAListener { + Parser p; + public Do(Parser p ) { this.p = p; } + @Override + public void exitEveryRule(ParserRuleContext ctx) { + System.out.println("exit "+ctx.toStringTree(p)); + } + + @Override + public void enterRule(AParser.eContext ctx) { + System.out.println("enter alt w/o -> label: "+ctx.toInfoString(p)); + } + + @Override + public void visitTerminal(ParserRuleContext ctx, Token symbol) { + if ( ctx instanceof AParser.eContext && symbol.getType()==AParser.INT ) { + AParser.eContext ectx = (AParser.eContext)ctx; + ectx.v = Integer.valueOf(symbol.getText()); + } + } + + @Override + public void exitRule(AParser.multContext ctx) { + System.out.println("mult "+ctx.a.v+" * "+ctx.b.v); + ctx.v = ctx.a.v * ctx.b.v; // repeat of what parser did--set return value + } + @Override + public void exitRule(AParser.addContext ctx) { + System.out.println("add "+ctx.a.v+" + "+ctx.b.v); + ctx.v = ctx.a.v + ctx.b.v; + } + } + public static void main(String[] args) throws Exception { + ALexer lexer = new ALexer(new ANTLRFileStream(args[0])); + CommonTokenStream tokens = new CommonTokenStream(lexer); + AParser p = new AParser(tokens); + p.setBuildParseTree(true); +// p.addParseListener(new Do(p)); + ParserRuleContext t = p.s(); + System.out.println("tree = "+t.toStringTree(p)); + + ParseTreeWalker walker = new ParseTreeWalker(); + Do doer = new Do(p); + walker.walk(doer, t); + AParser.eContext ectx = (AParser.eContext)t.getChild(0); + System.out.println("result from tree walk = "+ ectx.v); + } +} diff --git a/tool/playground/U.g b/tool/playground/U.g index 66c87ef11..ae193e846 100644 --- a/tool/playground/U.g +++ b/tool/playground/U.g @@ -1,12 +1,13 @@ grammar U; -s : '{' stat* '}' ; -// if x then break else if y then return else break -// still ambig: if x then if y then break else break -stat: 'if' ID 'then' stat ('else' stat)? - | 'break' - | 'return' - ; - -INT : '0'..'9'+ ; +s @after {System.out.println($ctx.toStringTree(this));} : e EOF ; +e : e '.' ID + | e '.' 'this' + | '-' e + | e '*' e + | e ('+'|'-') e + | INT + | ID + ; ID : 'a'..'z'+ ; -WS : (' '|'\n')+ {skip();} ; +INT : '0'..'9'+ ; +WS : (' '|'\n') {skip();} ;