You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.7 KiB
72 lines
2.7 KiB
%prefix "calc"
|
|
%value "char*"
|
|
|
|
%source {
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "libstr.h"
|
|
}
|
|
|
|
statement <- _ e:eq _ EOL { puts(e); }
|
|
/ ( !EOL . )* EOL { fprintf(stderr, "error\n"); }
|
|
|
|
eq <- l:expression _ '=' _ r:expression { $$ = str_catall(l, " = ", r, NULL); free(l); free(r); }
|
|
|
|
expression <- e:term { $$ = e; }
|
|
|
|
term <- l:term _ '+' _ r:factor { $$ = str_catall(l, " + ", r, NULL); free(l); free(r); }
|
|
/ l:term _ '-' _ r:factor { $$ = str_catall(l, " - ", r, NULL); free(l); free(r); }
|
|
/ e:factor { $$ = e; }
|
|
|
|
factor <- l:factor _ '*' _ r:power { $$ = str_catall(l, " \\times ", r, NULL); free(l); free(r); }
|
|
/ l:factor _ '/' _ r:power { $$ = str_catall("\\frac{", l, "}{", r, "}", NULL); free(l); free(r); }
|
|
/ e:power { $$ = e; }
|
|
|
|
power <- l:power _ '^' _ r:unary { $$ = str_catall(l, "^{", r, "}", NULL); free(l); free(r); }
|
|
/ e:unary { $$ = e; }
|
|
|
|
unary <- '+' _ e:unary { $$ = str_cat("+", e); free(e); }
|
|
/ '-' _ e:unary { $$ = str_cat("-", e); free(e); }
|
|
/ e:function { $$ = e; }
|
|
|
|
function <- l:fun '(' _ r:function _ ')' { $$ = str_catall("\\", l, "{", r, "}", NULL); free(l); free(r); }
|
|
/ l:fun '(' _ r:expression _ ')' { $$ = str_catall("\\", l, "{", r, "}", NULL); free(l); free(r); }
|
|
/ e:atom { $$ = e; }
|
|
|
|
greek <- 'alpha' { $$ = strdup("\\alpha"); }
|
|
/ 'Alpha' { $$ = strdup("\\Alpha"); }
|
|
/ 'beta' { $$ = strdup("\\beta"); }
|
|
/ 'Beta' { $$ = strdup("\\Beta"); }
|
|
/ 'gamma' { $$ = strdup("\\gamma"); }
|
|
/ 'Gamma' { $$ = strdup("\\Gamma"); }
|
|
/ 'delta' { $$ = strdup("\\delta"); }
|
|
/ 'Delta' { $$ = strdup("\\Delta"); }
|
|
/ 'epsilon' { $$ = strdup("\\epsilon"); }
|
|
/ 'Epsilon' { $$ = strdup("\\Epsilon"); }
|
|
/ 'theta' { $$ = strdup("\\theta"); }
|
|
/ 'Theta' { $$ = strdup("\\Theta"); }
|
|
/ 'phi' { $$ = strdup("\\phi"); }
|
|
/ 'Phi' { $$ = strdup("\\Phi"); }
|
|
/ 'pi' { $$ = strdup("\\pi"); }
|
|
/ 'Pi' { $$ = strdup("\\Pi"); }
|
|
/ 'omicron' { $$ = strdup("\\omicron"); }
|
|
/ 'Omicron' { $$ = strdup("\\Omicron"); }
|
|
/ 'sigma' { $$ = strdup("\\sigma"); }
|
|
/ 'Sigma' { $$ = strdup("\\Sigma"); }
|
|
|
|
atom <- e:greek { $$ = e; }
|
|
/ < [a-zA-Z0-9.]+ > { $$ = strdup($1); }
|
|
/ '(' _ e:expression _ ')' { $$ = str_catall("(", e, ")", NULL); free(e); }
|
|
|
|
fun <- < [a-z]+ > { $$ = strdup($1); }
|
|
|
|
_ <- [ \t]*
|
|
EOL <- '\n' / '\r\n' / '\r' / ';'
|
|
|
|
%%
|
|
int main() {
|
|
calc_context_t *ctx = calc_create(NULL);
|
|
while (calc_parse(ctx, NULL));
|
|
calc_destroy(ctx);
|
|
return 0;
|
|
}
|
|
|