Add Apache License version 2.0.
[pbc.git] / pbc / parser.y
blobd51cebcce5c102a16c36a57bb64eba03147ed818
1 %{
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include <stdint.h> // for intptr_t
5 #include <gmp.h>
6 #include "pbc_utils.h"
7 #include "pbc_field.h"
9 #include "pbc_tree.h"
10 #define YYSTYPE tree_ptr
11 void yyerror(const char *s);
12 int yylex(void);
14 #define YY_NO_INPUT
15 #define YY_NO_UNPUT
17 extern int option_easy;
20 %error-verbose
21 %token DEFINE
22 %token TERMINATOR
23 %token NUM ID
24 %token LPAR RPAR LSQU RSQU LBRACE RBRACE COMMA
25 %right QUESTION COLON
26 %left EQ NE LT T_GT LE GE
27 %right ASSIGN
28 %left PLUS MINUS
29 %left DIVIDE TIMES
30 %right UMINUS
31 %right POW
32 %token UNKNOWN
33 %token END 0 "end of file"
35 input
36 : // Empty.
37 | input stmt { tree_eval_stmt($2); }
40 stmt
41 : expr TERMINATOR
42 | DEFINE ID LPAR parms RPAR LBRACE stmtlist RBRACE {
43 $$ = tree_new_define($2, $4, $7);
47 stmtlist
48 : { $$ = tree_new_empty_stmt_list(); } // Empty.
49 | stmtlist stmt { tree_append($1, $2); }
52 parms
53 : { $$ = tree_new_empty_parms(); } // Empty.
54 | parms1
57 parms1
58 : ID { $$ = tree_new_empty_parms(); tree_append($$, $1); }
59 | parms1 COMMA ID { tree_append($1, $3); }
62 expr
63 : multinomial
64 | ID ASSIGN expr { $$ = tree_new_assign($1, $3); }
65 | expr QUESTION expr COLON expr { $$ = tree_new_ternary($1, $3, $5); }
66 | molecule
67 | molecule LSQU expr RSQU { $$ = tree_new_item($1, $3); }
68 | expr EQ expr { $$ = tree_new_eq($1, $3); }
69 | expr NE expr { $$ = tree_new_ne($1, $3); }
70 | expr LE expr { $$ = tree_new_le($1, $3); }
71 | expr GE expr { $$ = tree_new_ge($1, $3); }
72 | expr LT expr { $$ = tree_new_lt($1, $3); }
73 | expr T_GT expr { $$ = tree_new_gt($1, $3); }
74 | expr PLUS expr { $$ = tree_new_add($1, $3); }
75 | expr MINUS expr { $$ = tree_new_sub($1, $3); }
76 | expr TIMES expr { $$ = tree_new_mul($1, $3); }
77 | expr DIVIDE expr { $$ = tree_new_div($1, $3); }
78 | expr POW expr { $$ = tree_new_pow($1, $3); }
79 | MINUS expr %prec UMINUS { $$ = tree_new_neg($2); }
82 // Not quite atoms.
83 molecule
84 : molecule LPAR exprlist RPAR { $$ = $3; tree_set_fun($$, $1); }
85 | LPAR expr RPAR { $$ = $2; }
86 | ID
89 exprlist
90 : { $$ = tree_new_funcall(); } // Empty.
91 | nonemptyexprlist
94 nonemptyexprlist
95 : expr { tree_append($$ = tree_new_funcall(), $1); }
96 | nonemptyexprlist COMMA expr { tree_append($1, $3); }
99 multinomial
100 : NUM
101 | numlist
104 numlist
105 : LSQU sequence RSQU { $$ = $2; }
108 sequence
109 : expr { $$ = tree_new_list($1); }
110 | sequence COMMA expr { tree_append($1, $3); }