-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.jison
67 lines (58 loc) · 1.4 KB
/
grammar.jison
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* description: Parses end executes mathematical expressions. */
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
"{"[^}]*"}" return 'CHEMICAL'
/* [0-9]+("."[0-9]+)?("e""-"?[0-9]+)?(" "[\w\/\^]+)? return 'NUMBER' */
[0-9]+("."[0-9]+)?("e""-"?[0-9]+)? return 'NUMBER'
\w+ return 'VARIABLE'
"*" return '*'
"/" return '/'
"-" return '-'
"+" return '+'
"^" return '^'
"(" return '('
")" return ')'
":" return ":"
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
/* operator associations and precedence */
%left '+' '-'
%left '*' '/'
%left '^'
%left UMINUS
%right ":"
%start expressions
%% /* language grammar */
expressions
: e EOF
{return $1;}
;
e
: e '+' e
{$$ = ["+",$1,$3];}
| e '-' e
{$$ = ["-",$1,$3];}
| e '*' e
{$$ = ["*",$1,$3];}
| e '/' e
{$$ = ["/",$1,$3];}
| e '^' e
{$$ = ["^",$1,$3];}
| '-' e %prec UMINUS
{$$ = ["UMINUS",$2];}
| e ":" e
{$$ = [":", $1, $3];}
| '(' e ')'
{$$ = ["PARENS", $2];}
| VARIABLE '(' e ')'
{$$ = ['call', $1, $3];}
| CHEMICAL
{$$ = yytext;}
| VARIABLE
{$$ = yytext;}
| NUMBER
{$$ = Number(yytext);}
;