-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ypp
66 lines (49 loc) · 1.63 KB
/
parser.ypp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%skeleton "lalr1.cc"
%require "3.2"
%defines
%define api.token.constructor
%define api.value.type variant
%define parse.assert
%parse-param { Model& model }
%code requires {
#include <stdio.h>
#include "ast.h"
#include "model.h"
}
%code {
yy::parser::symbol_type yylex();
}
%token END 0
%token <double> NUM
%token <std::string> VAR
%token LPAREN RPAREN
%token ADD SUB MUL DIV POW
%token SIN COS EXP LN
%token SEMICOLON EQUALS COMMA
%type <std::unique_ptr<Node>> exp
%left ADD SUB
%left MUL DIV
%left POW
%%
input: exp { model.ast = std::move($1); }
| exp SEMICOLON assignments { model.ast = std::move($1); }
exp: NUM { $$ = std::make_unique<Scalar>($1); }
| VAR { $$ = std::make_unique<Variable>($1); model.arguments.insert($1); }
| LPAREN exp RPAREN { $$ = std::move($2); }
| exp ADD exp { $$ = std::make_unique<Add>(std::move($1), std::move($3)); }
| exp SUB exp { $$ = std::make_unique<Sub>(std::move($1), std::move($3)); }
| exp MUL exp { $$ = std::make_unique<Mul>(std::move($1), std::move($3)); }
| exp DIV exp { $$ = std::make_unique<Div>(std::move($1), std::move($3)); }
| exp POW NUM { $$ = std::make_unique<Pow>(std::move($1), $3); }
| SIN LPAREN exp RPAREN { $$ = std::make_unique<Sin>(std::move($3)); }
| COS LPAREN exp RPAREN { $$ = std::make_unique<Cos>(std::move($3)); }
| EXP LPAREN exp RPAREN { $$ = std::make_unique<Exp>(std::move($3)); }
| LN LPAREN exp RPAREN { $$ = std::make_unique<NatLog>(std::move($3)); }
assignments: assignment
| assignments COMMA assignment
assignment: VAR EQUALS NUM { model.vars.insert(std::make_pair($1, $3)); }
%%
void yy::parser::error (const std::string& s)
{
std::cout << s << std::endl;
}