summaryrefslogtreecommitdiffstats
path: root/taccle/examples/infix_calc.tac
blob: 95ef2ee37c64db351b64f7137f7c80326b2e48ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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