1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | * or more contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. The ASF licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | |
20 | /*! |
21 | * \file op_table.h |
22 | * \brief A operator table for parsing. |
23 | * Provides symbolic token sequences to map to TVM operators, with a given associativity and arity. |
24 | */ |
25 | |
26 | #ifndef TVM_RELAY_PARSER_OP_TABLE_H_ |
27 | #define TVM_RELAY_PARSER_OP_TABLE_H_ |
28 | |
29 | #include <tvm/ir/op.h> |
30 | #include <tvm/runtime/object.h> |
31 | |
32 | #include <fstream> |
33 | #include <string> |
34 | #include <unordered_map> |
35 | #include <vector> |
36 | |
37 | #include "./tokenizer.h" |
38 | |
39 | namespace tvm { |
40 | namespace relay { |
41 | |
42 | struct Rule { |
43 | std::vector<TokenType> tokens; |
44 | int precedence; |
45 | int arity; |
46 | tvm::Op op; |
47 | bool left_assoc; |
48 | |
49 | Rule() : tokens(), precedence(0), arity(0), op(tvm::Op()), left_assoc(false) {} |
50 | |
51 | Rule(std::vector<TokenType> tokens, tvm::Op op, int precedence, int arity = 2, |
52 | bool left_assoc = false) |
53 | : tokens(tokens), precedence(precedence), arity(arity), op(op), left_assoc(left_assoc) {} |
54 | |
55 | Rule(const Rule& rule) { |
56 | this->tokens = rule.tokens; |
57 | this->op = rule.op; |
58 | this->precedence = rule.precedence; |
59 | this->arity = rule.arity; |
60 | this->left_assoc = rule.left_assoc; |
61 | } |
62 | }; |
63 | |
64 | struct OperatorTable { |
65 | std::vector<Rule> rules; |
66 | std::unordered_map<std::string, Rule> this_is_a_hack; |
67 | |
68 | explicit OperatorTable(std::vector<Rule> rules) : rules(rules), this_is_a_hack() { |
69 | for (auto rule : rules) { |
70 | std::stringstream key; |
71 | for (auto token : rule.tokens) { |
72 | key << ToString(token); |
73 | } |
74 | this->this_is_a_hack.insert({key.str(), rule}); |
75 | } |
76 | } |
77 | }; |
78 | |
79 | inline OperatorTable DefaultOpTable() { |
80 | return OperatorTable( |
81 | {Rule({TokenType::kStar}, Op::Get("multiply" ), 12, 2, true), |
82 | Rule({TokenType::kDivision}, Op::Get("divide" ), 12, 2, true), |
83 | Rule({TokenType::kPlus}, Op::Get("add" ), 10, 2, true), |
84 | Rule({TokenType::kMinus}, Op::Get("subtract" ), 10, 2, true), |
85 | Rule({TokenType::kLAngle}, Op::Get("less" ), 8, 2, true), |
86 | Rule({TokenType::kLAngle, TokenType::kEqual}, Op::Get("less_equal" ), 8, 2, true), |
87 | Rule({TokenType::kRAngle}, Op::Get("greater" ), 8, 2, true), |
88 | Rule({TokenType::kRAngle, TokenType::kEqual}, Op::Get("greater_equal" ), 8, 2, true), |
89 | Rule({TokenType::kEqual, TokenType::kEqual}, Op::Get("equal" ), 7, 2, true), |
90 | Rule({TokenType::kBang, TokenType::kEqual}, Op::Get("not_equal" ), 7, 2, true)}); |
91 | } |
92 | |
93 | } // namespace relay |
94 | } // namespace tvm |
95 | #endif // TVM_RELAY_PARSER_OP_TABLE_H_ |
96 | |