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 token.h |
22 | * \brief A operator table for parsing. |
23 | * |
24 | * Provides symbolic token sequences to map to TVM operators, with a given associativity and arity. |
25 | */ |
26 | |
27 | #ifndef TVM_PARSER_OP_TABLE_H_ |
28 | #define TVM_PARSER_OP_TABLE_H_ |
29 | |
30 | #include <tvm/ir/op.h> |
31 | #include <tvm/runtime/object.h> |
32 | |
33 | #include <fstream> |
34 | #include <string> |
35 | #include <unordered_map> |
36 | #include <vector> |
37 | |
38 | #include "./tokenizer.h" |
39 | |
40 | namespace tvm { |
41 | namespace parser { |
42 | |
43 | struct Rule { |
44 | std::vector<TokenType> tokens; |
45 | int precedence; |
46 | int arity; |
47 | tvm::Op op; |
48 | bool left_assoc; |
49 | |
50 | Rule() : tokens(), precedence(0), arity(0), op(tvm::Op()), left_assoc(false) {} |
51 | |
52 | Rule(std::vector<TokenType> tokens, tvm::Op op, int precedence, int arity = 2, |
53 | bool left_assoc = false) |
54 | : tokens(tokens), precedence(precedence), arity(arity), op(op), left_assoc(left_assoc) {} |
55 | |
56 | Rule(const Rule& rule) { |
57 | this->tokens = rule.tokens; |
58 | this->op = rule.op; |
59 | this->precedence = rule.precedence; |
60 | this->arity = rule.arity; |
61 | this->left_assoc = rule.left_assoc; |
62 | } |
63 | }; |
64 | |
65 | struct OperatorTable { |
66 | std::vector<Rule> rules; |
67 | std::unordered_map<std::string, Rule> this_is_a_hack; |
68 | |
69 | explicit OperatorTable(std::vector<Rule> rules) : rules(rules), this_is_a_hack() { |
70 | for (auto rule : rules) { |
71 | std::stringstream key; |
72 | for (auto token : rule.tokens) { |
73 | key << ToString(token); |
74 | } |
75 | this->this_is_a_hack.insert({key.str(), rule}); |
76 | } |
77 | } |
78 | }; |
79 | |
80 | OperatorTable DefaultOpTable() { |
81 | return OperatorTable( |
82 | {Rule({TokenType::kStar}, Op::Get("multiply" ), 12, 2, true), |
83 | Rule({TokenType::kDivision}, Op::Get("divide" ), 12, 2, true), |
84 | Rule({TokenType::kPlus}, Op::Get("add" ), 10, 2, true), |
85 | Rule({TokenType::kMinus}, Op::Get("subtract" ), 10, 2, true), |
86 | Rule({TokenType::kLAngle}, Op::Get("less" ), 8, 2, true), |
87 | Rule({TokenType::kLAngle, TokenType::kEqual}, Op::Get("less_equal" ), 8, 2, true), |
88 | Rule({TokenType::kRAngle}, Op::Get("greater" ), 8, 2, true), |
89 | Rule({TokenType::kRAngle, TokenType::kEqual}, Op::Get("greater_equal" ), 8, 2, true), |
90 | Rule({TokenType::kEqual, TokenType::kEqual}, Op::Get("equal" ), 7, 2, true), |
91 | Rule({TokenType::kBang, TokenType::kEqual}, Op::Get("not_equal" ), 7, 2, true)}); |
92 | } |
93 | |
94 | } // namespace parser |
95 | } // namespace tvm |
96 | #endif // TVM_PARSER_OP_TABLE_H_ |
97 | |