%{ /* * Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc. * * This file is part of GNU libmatheval * * GNU libmatheval 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, or (at your option) * any later version. * * GNU libmatheval 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 program; see the file COPYING. If not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ /* This file was modified for inclusion in Gmsh */ #include "common.h" #include "node.h" /* Variables used to communicate with code using parser. */ extern Node* matheval_root; /* Root of tree representation of function. */ extern int matheval_ok; /* Flag representing success of parsing. */ /* Report parsing error. */ int yyerror (char *s); /* Function used to tokenize string representing function (this function is generated by scanner generator). */ int yylex (void); /* Function used to flush the internal flex buffer when we exit prematurely (i.e., in case of a parse error) so that the next time we call the scanner, all is nicely reset. Without this, the behaviour of the next call after an error is unpredictable. */ void force_buffer_flush(void); %} /* Parser semantic values type. */ %union { Node *node; Record *record; } /* Grammar terminal symbols. */ %token NUMBER VARIABLE %token FUNCTION /* Grammar non-terminal symbols. */ %type expression %left '-' '+' %left '*' '/' %left NEG %left '^' /* Grammar start non-terminal. */ %start input %% input : expression '\n' { matheval_root = $1; return 1; } ; expression : NUMBER { $$ = $1; } | VARIABLE { $$ = $1; } | expression '+' expression { /* Create addition binary operator node. */ $$ = node_create ('b', '+', $1, $3); } | expression '-' expression { /* Create subtraction binary operator node. */ $$ = node_create ('b', '-', $1, $3); } | expression '*' expression { /* Create multiplication binary operator node. */ $$ = node_create ('b', '*', $1, $3); } | expression '/' expression { /* Create division binary operator node. */ $$ = node_create ('b', '/', $1, $3); } | '-' expression %prec NEG { /* Create minus unary operator node. */ $$ = node_create ('u', '-', $2); } | expression '^' expression { /* Create exponentiation unary operator node. */ $$ = node_create ('b', '^', $1, $3); } | FUNCTION '(' expression ')' { /* Create function node. */ $$ = node_create ('f', $1, $3); } | '(' expression ')' { $$ = $2; } ; %% int yyerror(char* s) { /* Indicate parsing error through appropriate flag and stop parsing. */ matheval_ok = 0; force_buffer_flush(); return 0; }