update playground

This commit is contained in:
Terence Parr 2012-02-14 13:59:53 -08:00
parent a6786743d4
commit b8f50d46e7
2 changed files with 57 additions and 44 deletions

View File

@ -8,19 +8,32 @@ public Token x(int i) { }
public eContext e() { }
or: public List<eContext> e() { }
*/
z : ID x+=INT+ e {List x = $INT; Token t = $ID;} ;
//z : ID INT+ e z+ X X -> Foo;
/*
e : f* | f* ;
f : F ;
statement
: 'if' E statement ('else' statement)? -> Foo
| 'for' '(' E ')' statement
;
*/
/*
s : Q q=e {Object o=$q.v;} -> one
| z=e {Object o=$z.v;}
;
*/
e returns [int v]
: a=e op='*' b=e {$v = $a.v * $b.v;} -> mult
| b=e '+' e
| '(' x=e ')'
;
*/
/*
INT : '9';
a : u=A A -> x

View File

@ -31,46 +31,46 @@ import org.antlr.v4.runtime.*;
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<Token> ctx) {
System.out.println("exit "+ctx.toStringTree(p));
}
@Override
public void enter(AParser.eContext ctx) {
System.out.println("enter alt w/o -> label: "+ctx.toInfoString(p));
}
@Override
public void visitTerminal(ParserRuleContext<Token> 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 exit(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
}
}
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<Token> 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);
}
// public static class Do extends BlankAListener {
// Parser p;
// public Do(Parser p ) { this.p = p; }
// @Override
// public void exitEveryRule(ParserRuleContext<Token> ctx) {
// System.out.println("exit "+ctx.toStringTree(p));
// }
//
// @Override
// public void enter(AParser.eContext ctx) {
// System.out.println("enter alt w/o -> label: "+ctx.toInfoString(p));
// }
//
// @Override
// public void visitTerminal(ParserRuleContext<Token> 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 exit(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
// }
// }
// 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<Token> 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);
// }
}