diff options
Diffstat (limited to 'taccle/examples/infix_calc.tac')
-rw-r--r-- | taccle/examples/infix_calc.tac | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/taccle/examples/infix_calc.tac b/taccle/examples/infix_calc.tac new file mode 100644 index 0000000..95ef2ee --- /dev/null +++ b/taccle/examples/infix_calc.tac @@ -0,0 +1,35 @@ +# $Id: infix_calc.tac,v 1.1 2004/09/29 16:23:28 tang Exp $ + +# This example demonstrates how taccle handles operator precedence. +# The code is shamelessly borrowed from the GNU Bison info manual. + +# taccle Declarations +%token ID NEWLINE +%left '-' '+' +%left '*' '/' +%left NEG # negation--unary minus +%right '^' # exponentiation + +# Grammar follows +%% +input: # empty string + | input line +; + +line: NEWLINE + | exp NEWLINE { puts [format "\t%.10g" $1] } +; + +exp: ID { set _ $1 } + | exp '+' exp { set _ [expr {$1 + $3}] } + | exp '-' exp { set _ [expr {$1 - $3}] } + | exp '*' exp { set _ [expr {$1 * $3}] } + | exp '/' exp { set _ [expr {$1 / $3}] } + | '-' exp %prec NEG { set _ [expr {-1.0 * $2}] } + | exp '^' exp { set _ [expr {pow($1, $3)}] } + | '(' exp ')' { set _ $2 } +; +%% + +source simple_scanner.tcl +yyparse |