Skip to content

Instantly share code, notes, and snippets.

@aeris
Created January 27, 2014 21:40
Show Gist options
  • Save aeris/8657917 to your computer and use it in GitHub Desktop.
Save aeris/8657917 to your computer and use it in GitHub Desktop.
Bistromathématique : calculatrice en base quelconque et précision infinie
grammar bistromathematique;
@header {
import java.math.BigInteger;
}
DIGIT: ('0'..'9') | ('a'..'z') | ('A'..'Z') ;
WS: [ \t\r\n]+ -> skip;
expression: a=addition { System.out.println($a.r); } EOF!;
addition returns [ BigInteger r ]
: a=substraction { $r = $a.r; } ('+'^ b=substraction { $r = $r.add($b.r); })*;
substraction returns [ BigInteger r ]
: a=multiply { $r = $a.r; } ('-'^ b=multiply { $r = $r.subtract($b.r); })*;
multiply returns [ BigInteger r ]
: a=divide { $r = $a.r; } ('*'^ b=divide { $r = $r.multiply($b.r); } )*;
divide returns [ BigInteger r ]
: a=predicate { $r = $a.r; } ('/'^ b=predicate { $r = $r.divide($b.r); })*;
predicate returns [ BigInteger r ]
: a=number { $r = $a.r; }
| '('^ c=addition { $r = $c.r; } ')'^;
number returns [ BigInteger r ]
: '('^ a=base ')'^ b=digits { $r = new BigInteger($b.text, $a.r); }
| c=digits { $r = new BigInteger($c.text); };
base returns [ int r ]
: a=digits { $r = new Integer($a.text); };
digits : DIGIT+;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment