This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
/
parser-generator.js
executable file
·187 lines (183 loc) · 8.95 KB
/
parser-generator.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
var fs = require("fs");
var path = require("path");
var jison = require("jison");
var grammar = {
lex: {
rules: [
["\\s+", "/* skip whitespace */"],
["\\\\space", "/* skip \\space */"],
["\\\\ ", "/* skip '\\ ' */"],
["[0-9]+\\.?", "return \"INT\""],
["([0-9]+)?\\.[0-9]+", "return \"FLOAT\""],
["\\*\\*", "return \"^\""],
["\\*", "return \"*\""],
["\\\\cdot|\u00b7", "return \"*\""],
["\\\\times|\u00d7", "return \"*\""],
["\\\\ast", "return \"*\""],
["\\/", "return \"/\""],
["-", "return \"-\""],
["\u2212", "return \"-\""], // minus
["\\+", "return \"+\""],
["\\^", "return \"^\""],
["\\(", "return \"(\""],
["\\)", "return \")\""],
["\\\\left\\(", "return \"(\""],
["\\\\right\\)", "return \")\""],
["\\{", "return \"{\""],
["\\}", "return \"}\""],
["\\\\left\\{", "return \"{\""],
["\\\\right\\}", "return \"}\""],
["_", "return \"_\""],
["\\|", "return \"|\""],
["\\\\left\\|", "return \"LEFT|\""],
["\\\\right\\|", "return \"RIGHT|\""],
["\\!", "return \"!\""], // not yet interpreted
["<=|>=|<>|<|>|=", "return \"SIGN\""],
["\\\\le", "yytext = \"<=\"; return \"SIGN\""],
["\\\\ge", "yytext = \">=\"; return \"SIGN\""],
["\\\\leq", "yytext = \"<=\"; return \"SIGN\""],
["\\\\geq", "yytext = \">=\"; return \"SIGN\""],
["=\\/=", "yytext = \"<>\"; return \"SIGN\""],
["\\\\ne", "yytext = \"<>\"; return \"SIGN\""],
["\\\\neq", "yytext = \"<>\"; return \"SIGN\""],
["\u2260", "yytext = \"<>\"; return \"SIGN\""], // ne
["\u2264", "yytext = \"<=\"; return \"SIGN\""], // le
["\u2265", "yytext = \">=\"; return \"SIGN\""], // ge
["\\\\frac", "return \"FRAC\""],
["\\\\dfrac", "return \"FRAC\""],
["sqrt|\\\\sqrt", "return \"sqrt\""],
["abs|\\\\abs", "return \"abs\""],
["ln|\\\\ln", "return \"ln\""],
["log|\\\\log", "return \"log\""],
["sin|cos|tan", "return \"TRIG\""],
["csc|sec|cot", "return \"TRIG\""],
["sinh|cosh|tanh", "return \"TRIG\""],
["csch|sech|coth", "return \"TRIG\""],
["\\\\sin", "yytext = \"sin\"; return \"TRIG\""],
["\\\\cos", "yytext = \"cos\"; return \"TRIG\""],
["\\\\tan", "yytext = \"tan\"; return \"TRIG\""],
["\\\\csc", "yytext = \"csc\"; return \"TRIG\""],
["\\\\sec", "yytext = \"sec\"; return \"TRIG\""],
["\\\\cot", "yytext = \"cot\"; return \"TRIG\""],
["\\\\arcsin", "yytext = \"arcsin\"; return \"TRIG\""],
["\\\\arccos", "yytext = \"arccos\"; return \"TRIG\""],
["\\\\arctan", "yytext = \"arctan\"; return \"TRIG\""],
["\\\\arccsc", "yytext = \"arccsc\"; return \"TRIG\""],
["\\\\arcsec", "yytext = \"arcsec\"; return \"TRIG\""],
["\\\\arccot", "yytext = \"arccot\"; return \"TRIG\""],
["arcsin|arccos|arctan","return \"TRIGINV\""],
["arccsc|arcsec|arccot","return \"TRIGINV\""],
["\\\\sinh", "yytext = \"sinh\"; return \"TRIG\""],
["\\\\cosh", "yytext = \"cosh\"; return \"TRIG\""],
["\\\\tanh", "yytext = \"tanh\"; return \"TRIG\""],
["\\\\csch", "yytext = \"csch\"; return \"TRIG\""],
["\\\\sech", "yytext = \"sech\"; return \"TRIG\""],
["\\\\coth", "yytext = \"tanh\"; return \"TRIG\""],
["pi", "return \"CONST\""],
["\u03C0", "yytext = \"pi\"; return \"CONST\""], // pi
["\\\\pi", "yytext = \"pi\"; return \"CONST\""],
["theta", "return \"VAR\""],
["\u03B8", "yytext = \"theta\"; return \"VAR\""], // theta
["\\\\theta", "yytext = \"theta\"; return \"VAR\""],
["phi", "return \"VAR\""],
["\u03C6", "yytext = \"phi\"; return \"VAR\""], // phi
["\\\\phi", "yytext = \"phi\"; return \"VAR\""],
["[a-zA-Z]", "return yy.symbolLexer(yytext)"],
["$", "return \"EOF\""],
[".", "return \"INVALID\""]
],
options: {
flex: true // pick longest matching token
}
},
operators: [
["right", "|"],
["left", "+", "-"],
["left", "*", "/"],
["left", "UMINUS"],
["right", "^"]
],
start: "equation",
bnf: {
"equation": [
["expression SIGN expression EOF", "return new yy.Eq($1, $2, $3);"],
["expression EOF", "return $1;"],
["EOF", "return new yy.Add([]);"]
],
"expression": [
["additive", "$$ = $1;"]
],
"additive": [
["additive + multiplicative", "$$ = yy.Add.createOrAppend($1, $3);"],
["additive - multiplicative", "$$ = yy.Add.createOrAppend($1, yy.Mul.handleNegative($3, \"subtract\"));"],
["multiplicative", "$$ = $1;", {prec: "+"}]
],
"multiplicative": [
// the second term in an implicit multiplication cannot be negative
["multiplicative triglog", "$$ = yy.Mul.fold(yy.Mul.createOrAppend($1, $2));"],
["multiplicative * negative", "$$ = yy.Mul.fold(yy.Mul.createOrAppend($1, $3));"],
["multiplicative / negative", "$$ = yy.Mul.fold(yy.Mul.handleDivide($1, $3));"],
["negative", "$$ = $1;"]
],
"negative": [
["- negative", "$$ = yy.Mul.handleNegative($2);", {prec: "UMINUS"}],
["triglog", "$$ = $1;"]
],
"trig": [
["TRIG", "$$ = [yytext];"]
],
"trigfunc": [
["trig", "$$ = $1;"],
["trig ^ negative", "$$ = $1.concat($3);"],
["TRIGINV", "$$ = [yytext];"]
],
"logbase": [
["ln", "$$ = yy.Log.natural();"],
["log", "$$ = yy.Log.common();"],
["log _ subscriptable", "$$ = $3;"]
],
"triglog": [
["trigfunc negative", "$$ = yy.Trig.create($1, $2);"],
["logbase negative", "$$ = yy.Log.create($1, $2);"],
["power", "$$ = $1;"]
],
"power": [
["primitive ^ negative", "$$ = new yy.Pow($1, $3);"],
["primitive", "$$ = $1;"]
],
"variable": [
["VAR", "$$ = yytext;"]
],
"subscriptable": [
["variable _ subscriptable", "$$ = new yy.Var($1, $3);"],
["variable", "$$ = new yy.Var($1);"],
["CONST", "$$ = new yy.Const(yytext.toLowerCase());"],
["INT", "$$ = yy.Int.create(Number(yytext));"],
["FLOAT", "$$ = yy.Float.create(Number(yytext));"],
["{ additive }", "$$ = $2.completeParse();"],
["( additive )", "$$ = $2.completeParse().addHint('parens');"] // this probably shouldn't be a hint...
],
"function": [
["FUNC", "$$ = yytext;"]
],
"invocation": [
["sqrt ( additive )", "$$ = yy.Pow.sqrt($3);"],
["sqrt { additive }", "$$ = yy.Pow.sqrt($3);"],
["abs ( additive )", "$$ = new yy.Abs($3);"],
["| additive |", "$$ = new yy.Abs($2);"],
["LEFT| additive RIGHT|", "$$ = new yy.Abs($2);"],
["function ( additive )", "$$ = new yy.Func($1, $3);"]
],
"primitive": [
["subscriptable", "$$ = $1;"],
["invocation", "$$ = $1;"],
["FRAC { additive } { additive }", "$$ = yy.Mul.fold(yy.Mul.handleDivide($3, $6));"]
]
}
};
var prelude = "// This is a @gene" + "rated file\n" +
"window.KAS = {};\n(function(KAS) {\n\n";
var parser = (new jison.Parser(grammar)).generate({moduleType: "js"});
var postlude = "\n\nKAS.parser = parser;\n})(KAS);";
fs.writeFileSync(path.resolve(__dirname, "parser.js"), prelude + parser + postlude);