aboutsummaryrefslogtreecommitdiff
path: root/Source/Parser/wp_parser.y
diff options
context:
space:
mode:
authorGravatar Dave Grote <grote1@llnl.gov> 2019-04-18 11:26:51 -0700
committerGravatar Dave Grote <grote1@llnl.gov> 2019-04-18 11:26:51 -0700
commit5ed1a16ace5ed9d32e18e25b23ef87996679b4dc (patch)
tree110be73bd454bf2a673e2aa73b26b16cea29865c /Source/Parser/wp_parser.y
parentbe0cbe26c1914f14e059be84b546934a3933ab85 (diff)
parentae239587668bbadc742ce5992afc6d6f814c5a3c (diff)
downloadWarpX-5ed1a16ace5ed9d32e18e25b23ef87996679b4dc.tar.gz
WarpX-5ed1a16ace5ed9d32e18e25b23ef87996679b4dc.tar.zst
WarpX-5ed1a16ace5ed9d32e18e25b23ef87996679b4dc.zip
Merge branch 'dev' into RZgeometry
Diffstat (limited to 'Source/Parser/wp_parser.y')
-rw-r--r--Source/Parser/wp_parser.y84
1 files changed, 84 insertions, 0 deletions
diff --git a/Source/Parser/wp_parser.y b/Source/Parser/wp_parser.y
new file mode 100644
index 000000000..3081125cc
--- /dev/null
+++ b/Source/Parser/wp_parser.y
@@ -0,0 +1,84 @@
+
+%{
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <math.h>
+ #include "wp_parser_y.h"
+ int yylex (void);
+%}
+
+/* We do not need to make this reentrant safe, because we use flex and
+ bison for generating AST only and this part doesn't need to be
+ thread safe.
+*/
+/*%define api.pure full */
+
+/* This is the type returned by functions wp_new* declared in
+ wp_parser_y.h. See also bison rules at the end of this file.
+*/
+%union {
+ struct wp_node* n;
+ double d;
+ struct wp_symbol* s;
+ enum wp_f1_t f1;
+ enum wp_f2_t f2;
+}
+
+/* Define tokens. They are used by flex too. */
+%token <n> NODE
+%token <d> NUMBER
+%token <s> SYMBOL
+%token <f1> F1
+%token <f2> F2
+%token EOL
+%token POW "**" '^'
+
+%nonassoc F1 F2
+%right '='
+%left '+' '-'
+%left '*' '/'
+%left '<' '>'
+%nonassoc NEG UPLUS
+%right POW
+
+/* This specifies the type of `exp` (i.e., struct wp_node*). Rules
+ specified later pass `exp` to wp_new* functions declared in
+ wp_parser_y.h.
+*/
+%type <n> exp
+
+%start input
+
+%%
+
+/* Given `\n` terminated input, a tree is generated and passed to
+ * function wp_defexpr defined in wp_parser_y.c.
+ */
+input:
+ %empty
+| input exp EOL {
+ wp_defexpr($2);
+ }
+;
+
+/* Enum types WP_ADD, WP_SUB, etc. are defined in wp_parser_y.h.
+ * Functions wp_new* are also declared in that file.
+ */
+exp:
+ NUMBER { $$ = wp_newnumber($1); }
+| SYMBOL { $$ = wp_newsymbol($1); }
+| exp '+' exp { $$ = wp_newnode(WP_ADD, $1, $3); }
+| exp '-' exp { $$ = wp_newnode(WP_SUB, $1, $3); }
+| exp '*' exp { $$ = wp_newnode(WP_MUL, $1, $3); }
+| exp '/' exp { $$ = wp_newnode(WP_DIV, $1, $3); }
+| '(' exp ')' { $$ = $2; }
+| exp '<' exp { $$ = wp_newf2(WP_LT, $1, $3); }
+| exp '>' exp { $$ = wp_newf2(WP_GT, $1, $3); }
+| '-'exp %prec NEG { $$ = wp_newnode(WP_NEG, $2, NULL); }
+| '+'exp %prec UPLUS { $$ = $2; }
+| exp POW exp { $$ = wp_newf2(WP_POW, $1, $3); }
+| F1 '(' exp ')' { $$ = wp_newf1($1, $3); }
+| F2 '(' exp ',' exp ')' { $$ = wp_newf2($1, $3, $5); }
+;
+
+%%