diff options
Diffstat (limited to 'runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3')
-rw-r--r-- | runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 new file mode 100644 index 0000000..0ed570b --- /dev/null +++ b/runtime/CSharp3/Sources/Antlr3.Runtime.Test/Composition/Reduce.g3 @@ -0,0 +1,26 @@ +tree grammar Reduce; + +options +{ + tokenVocab=VecMath; + ASTLabelType=CommonTree; + output=AST; + filter=true; + language=CSharp3; +} + + +@members +{ + //public override IAstRuleReturnScope Topdown() { return topdown(); } + public override IAstRuleReturnScope Bottomup() { return bottomup(); } +} + + +/** Rewrite: x+x to be 2*x, 2*x to be x<<1, x<<n<<m to be x<<(n+m) */ +bottomup + : ^(PLUS i=INT j=INT {$i.int==$j.int}?) -> ^(MULT["*"] INT["2"] $j) + | ^(MULT x=INT {$x.int==2}? y=.) -> ^(SHIFT["<<"] $y INT["1"]) + | ^(SHIFT ^(SHIFT e=. n=INT) m=INT) + -> ^(SHIFT["<<"] $e INT[($n.int+$m.int).ToString()]) + ; |