%{
/**
    This file is part of KMathTool, a KDE program for the math class...
    Copyright (C) 2002 Dominique Devriese <fritmebufstek@pandora.be>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "nodes.h"
#define YYSTYPE FormulaNode*
#include "parser.h"
int yylex( void );
%}
 
/* Declarations... */

/* the token for a numerical constant */
%token NUM
/* the token for a variable ( always 'x' ) */
%token VAR

%left '+' '-'
%left '*' '/'
%left NEG   /* negation -- unary minus */
%right '^'  /* exponentiation */

%%
/* Grammar rules... */

line: exp '\n' { Parser::instance()->setOutput( $1 ); };

exp: NUM { $$ = $1; }
| VAR { $$ = $1; }
| NUM VAR { $$ = new MultiplyNode( $1, $2 ); }
| exp '+' exp { $$ = new SumNode( $1, $3 ); }
| exp '-' exp { $$ = new SumNode( $1, new MultiplyNode( $3, new ConstantNode( -1 ) ) ); }
| exp '*' exp { $$ = new MultiplyNode( $1, $3 ); }
| exp '/' exp { $$ = new MultiplyNode( $1, new PowerNode( $3, new ConstantNode( -1 ) ) ); }
| '-' exp %prec NEG { $$ = new MultiplyNode( $2, new ConstantNode( -1 ) ); }
| exp '^' exp { $$ = new PowerNode( $1, $3 ); }
| '(' exp ')' { $$ = $2; }
;

%%

void yyerror( const char* s )
{
  Parser::instance()->setError( s );
};
