0

I Just Asked this question for objective-c here, but need the answer quick and am able to do the parsing in either objective-c or javascript, so I will ask for js here.

In my app the user will be inputting a mathematical expression as a string, such as n*3/7+9-5, and I need to be able to get the polynomials from that string, like 4, 3/7, 0, 0, 0, 0, is there a way to do this or a library that will do this in javascript? I am using node, so preferably something that is node compatible.

I have done some research on google, but there appears to be nothing, and I have no idea how to go about writing it myself.

Community
  • 1
  • 1
Ari Porad
  • 2,834
  • 3
  • 33
  • 51
  • 1
    Please show your code. What have you tried, and how did you attempt to solve the problem? – elclanrs Aug 30 '13 at 22:15
  • @elclanrs, I have done some googling on the subject, there does not seem to be anything. Also, if I were to write it myself I have no clue where to start or how to go about it – Ari Porad Aug 30 '13 at 22:16
  • Then maybe you should start with something simpler... Try to get the result of a simple sum inside a string without `eval`. When you're comfortable with that, look into the string methods or even regular expressions to parse some more complicated strings. – elclanrs Aug 30 '13 at 22:18
  • @elclanrs, I need them like this so I can put them in a db and easily avoid duplicates. – Ari Porad Aug 30 '13 at 22:21
  • I think you're missing my point; seems you're chewing more than you can bite. Start with a more basic example to learn to parse strings, then increase in complexity. – elclanrs Aug 30 '13 at 22:24
  • @elclanrs, that will not solve my problem, I already can do that. I need to do this for a reason. – Ari Porad Aug 30 '13 at 22:25
  • Still missing it, but well, there's an answer below, try that. – elclanrs Aug 30 '13 at 22:26

1 Answers1

2

you cantry writing jison grammer with online also http://zaach.github.io/jison/try/

calculator grammer is like this I am also learning this it is little bit difficult to understand

From http://zaach.github.io/jison/try/ :

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex
%%

\s+                   /* skip whitespace */
[0-9]+("."[0-9]+)?\b  return 'NUMBER'
"*"                   return '*'
"/"                   return '/'
"-"                   return '-'
"+"                   return '+'
"^"                   return '^'
"!"                   return '!'
"%"                   return '%'
"("                   return '('
")"                   return ')'
"PI"                  return 'PI'
"E"                   return 'E'
<<EOF>>               return 'EOF'
.                     return 'INVALID'

/lex

/* operator associations and precedence */

%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS

%start expressions

%% /* language grammar */

expressions
    : e EOF
        { typeof console !== 'undefined' ? console.log($1) : print($1);
          return $1; }
    ;

e
    : e '+' e
        {$$ = $1+$3;}
    | e '-' e
        {$$ = $1-$3;}
    | e '*' e
        {$$ = $1*$3;}
    | e '/' e
        {$$ = $1/$3;}
    | e '^' e
        {$$ = Math.pow($1, $3);}
    | e '!'
        {{
          $$ = (function fact (n) { return n==0 ? 1 : fact(n-1) * n })($1);
        }}
    | e '%'
        {$$ = $1/100;}
    | '-' e %prec UMINUS
        {$$ = -$2;}
    | '(' e ')'
        {$$ = $2;}
    | NUMBER
        {$$ = Number(yytext);}
    | E
        {$$ = Math.E;}
    | PI
        {$$ = Math.PI;}
    ;
Jimmy Hoffa
  • 5,909
  • 30
  • 53