-> becomes # for alt labels
This commit is contained in:
parent
30f61b17e3
commit
6314b7d31b
|
@ -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
|
||||
* unicode rule/token names
|
||||
|
||||
* -> skip notation
|
|
@ -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 ;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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+
|
||||
;
|
||||
|
|
|
@ -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" +
|
||||
|
|
|
@ -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" +
|
||||
|
|
Loading…
Reference in New Issue