Skip to content
Snippets Groups Projects
Select Git revision
  • 19773f886c6550b33f6d9bf7b6cc9f84499828a2
  • master default
  • cgnsUnstructured
  • partitioning
  • poppler
  • HighOrderBLCurving
  • gmsh_3_0_4
  • gmsh_3_0_3
  • gmsh_3_0_2
  • gmsh_3_0_1
  • gmsh_3_0_0
  • gmsh_2_16_0
  • gmsh_2_15_0
  • gmsh_2_14_1
  • gmsh_2_14_0
  • gmsh_2_13_2
  • gmsh_2_13_1
  • gmsh_2_12_0
  • gmsh_2_11_0
  • gmsh_2_10_1
  • gmsh_2_10_0
  • gmsh_2_9_3
  • gmsh_2_9_2
  • gmsh_2_9_1
  • gmsh_2_9_0
  • gmsh_2_8_6
26 results

parser.y

Blame
  • Forked from gmsh / gmsh
    Source project has a limited visibility.
    parser.y 3.03 KiB
    %{
    /*
     * 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.  */
    int force_buffer_flush(void);
    
    %}
    
    /* Parser semantic values type.  */
    %union {
      Node *node;
      Record *record;
    }
    
    /* Grammar terminal symbols.  */
    %token <node> NUMBER VARIABLE
    %token <record> FUNCTION
    
    /* Grammar non-terminal symbols.  */
    %type <node> 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;
    }