From 6314b7d31bf48ddb199c59958de90db136979049 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Thu, 26 Apr 2012 11:59:57 -0700 Subject: [PATCH] -> becomes # for alt labels --- tool/MIGRATION.txt | 16 +++++++--- tool/playground/T.g | 31 ++++++++++++++----- .../analysis/LeftRecursiveRuleAnalyzer.java | 2 +- tool/src/org/antlr/v4/parse/ANTLRParser.g | 4 +-- .../org/antlr/v4/test/TestLeftRecursion.java | 30 +++++++++--------- .../test/org/antlr/v4/test/TestListeners.java | 4 +-- 6 files changed, 55 insertions(+), 32 deletions(-) diff --git a/tool/MIGRATION.txt b/tool/MIGRATION.txt index c292b8ee7..c4aacd8fe 100644 --- a/tool/MIGRATION.txt +++ b/tool/MIGRATION.txt @@ -1,17 +1,23 @@ +Parsers + +* Full context LL(*) not SLL(*) +* Adaptive, takes all but indirect left-recursion + Actions/scopes * no global scopes. no scope[n]. Trees +* no ASTs +* no tree grammars +* parse trees created by default * moved methods to Trees -* Tree->AST -* added parse trees - - no -> and other tree construct in tree grammars Lexers * Added [Abc] notation -* unicode rule/token names \ No newline at end of file +* unicode rule/token names + +* -> skip notation \ No newline at end of file diff --git a/tool/playground/T.g b/tool/playground/T.g index 9949f8074..8d9dd33bb 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -1,8 +1,25 @@ grammar T; -s : e ';' ; -e : e '*' e - | ID - | INT - ; -INT : '0'..'9'+; -WS : (' '|'\n') {skip();} ; +@members { + public static class LeafListener extends TBaseListener { + public void exitCall(TParser.CallContext ctx) { + System.out.printf("%s %s",ctx.e().start.getText(), + ctx.eList()); + } + public void exitInt(TParser.IntContext ctx) { + System.out.println(ctx.INT().getSymbol().getText()); + } + } +} +s +@init {setBuildParseTree(true);} +@after { System.out.println($r.ctx.toStringTree(this)); ParseTreeWalker walker = new ParseTreeWalker(); + walker.walk(new LeafListener(), $r.ctx);} + : r=e ; +e : e '(' eList ')' # Call + | INT # Int + ; +eList : e (',' e)* ; +MULT: '*' ; +ADD : '+' ; +INT : [0-9]+ ; +WS : [ \t\n]+ -> skip ; diff --git a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java index bd1ad31aa..782d93886 100644 --- a/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java +++ b/tool/src/org/antlr/v4/analysis/LeftRecursiveRuleAnalyzer.java @@ -359,7 +359,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { TokenStream tokens = input.getTokenStream(); // find => for (int i=stop; i>=start; i--) { - if ( tokens.get(i).getType()==RARROW ) { + if ( tokens.get(i).getType()==POUND ) { altAST.setTokenStopIndex(i-1); return; } diff --git a/tool/src/org/antlr/v4/parse/ANTLRParser.g b/tool/src/org/antlr/v4/parse/ANTLRParser.g index 540bfbe47..12d5c0ef5 100644 --- a/tool/src/org/antlr/v4/parse/ANTLRParser.g +++ b/tool/src/org/antlr/v4/parse/ANTLRParser.g @@ -535,7 +535,7 @@ ruleAltList labeledAlt : alternative - ( RARROW! id! {((AltAST)$alternative.tree).altLabel=$id.tree;} + ( POUND! id! {((AltAST)$alternative.tree).altLabel=$id.tree;} )? ; @@ -655,7 +655,7 @@ lexerCommandName : id | MODE ->ID[$MODE] ; - + altList : alternative (OR alternative)* -> alternative+ ; diff --git a/tool/test/org/antlr/v4/test/TestLeftRecursion.java b/tool/test/org/antlr/v4/test/TestLeftRecursion.java index 2eb813f66..5fe3503fb 100644 --- a/tool/test/org/antlr/v4/test/TestLeftRecursion.java +++ b/tool/test/org/antlr/v4/test/TestLeftRecursion.java @@ -252,13 +252,13 @@ public class TestLeftRecursion extends BaseTest { "s : q=e {System.out.println($e.v);} ;\n" + "\n" + "e returns [int v]\n" + - " : a=e op='*' b=e {$v = $a.v * $b.v;} -> mult\n" + - " | a=e '+' b=e {$v = $a.v + $b.v;} -> add\n" + - " | INT {$v = $INT.int;} -> anInt\n" + - " | '(' x=e ')' {$v = $x.v;} -> parens\n" + - " | x=e '++' {$v = $x.v+1;} -> inc\n" + - " | e '--' -> dec\n" + - " | ID {$v = 3;} -> anID\n" + + " : a=e op='*' b=e {$v = $a.v * $b.v;} # mult\n" + + " | a=e '+' b=e {$v = $a.v + $b.v;} # add\n" + + " | INT {$v = $INT.int;} # anInt\n" + + " | '(' x=e ')' {$v = $x.v;} # parens\n" + + " | x=e '++' {$v = $x.v+1;} # inc\n" + + " | e '--' # dec\n" + + " | ID {$v = 3;} # anID\n" + " ; \n" + "\n" + "ID : 'a'..'z'+ ;\n" + @@ -304,18 +304,18 @@ public class TestLeftRecursion extends BaseTest { "// START:stat\n" + "prog: stat ;\n" + "\n" + - "stat: expr NEWLINE -> printExpr\n" + - " | ID '=' expr NEWLINE -> assign\n" + - " | NEWLINE -> blank\n" + + "stat: expr NEWLINE # printExpr\n" + + " | ID '=' expr NEWLINE # assign\n" + + " | NEWLINE # blank\n" + " ;\n" + "// END:stat\n" + "\n" + "// START:expr\n" + - "expr: expr ('*'|'/') expr -> MulDiv\n" + - " | expr ('+'|'-') expr -> AddSub\n" + - " | INT -> int\n" + - " | ID -> id\n" + - " | '(' expr ')' -> parens\n" + + "expr: expr ('*'|'/') expr # MulDiv\n" + + " | expr ('+'|'-') expr # AddSub\n" + + " | INT # int\n" + + " | ID # id\n" + + " | '(' expr ')' # parens\n" + " ;\n" + "// END:expr\n" + "\n" + diff --git a/tool/test/org/antlr/v4/test/TestListeners.java b/tool/test/org/antlr/v4/test/TestListeners.java index 0d05a00ca..a683d4aed 100644 --- a/tool/test/org/antlr/v4/test/TestListeners.java +++ b/tool/test/org/antlr/v4/test/TestListeners.java @@ -178,8 +178,8 @@ public class TestListeners extends BaseTest { " walker.walk(new LeafListener(), $r.ctx);" + "}\n" + " : r=e ;\n" + - "e : e '(' eList ')' -> Call\n" + - " | INT -> Int\n" + + "e : e '(' eList ')' # Call\n" + + " | INT # Int\n" + " ; \n" + "eList : e (',' e)* ;\n" + "MULT: '*' ;\n" +