NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
ScriptEngineLexyn.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_SCRIPTENGINELEXYN_H
27 #define NVNEURAL_SCRIPTENGINELEXYN_H
28 
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <vector>
33 
34 namespace nvneural {
35 namespace detail {
36 
37 struct Lexem //rule lexeme
38 {
39  enum TypeCode
40  {
41  ID,
42  INTEGER,
43  FLOAT,
44  DIMENSION,
45  STRING,
46  OPERATOR,
47  COMMENT,
48  FATAL_ERROR,
49 
50  //special case for syntax analyzer
51  GRAMMAR
52  };
53  TypeCode type = FATAL_ERROR;
54  std::string value;
55 };
56 
57 std::vector<Lexem> lexParser(const std::string& text, bool debugScript = false);
58 
59 class SynParser
60 {
61 public:
62  SynParser()
63  {
64  }
65  ~SynParser()
66  {
67  }
68 
69  SynParser& addLiteral(const std::string& name, const std::string& value, bool skip = false);
70  SynParser& addTypedLiteral(const std::string& name, Lexem::TypeCode type, bool skip = false);
71  SynParser& addGrammar(const std::string& name, const std::string& value, bool root = false, bool unskippable = false);
72 
73  struct Node
74  {
75  Lexem lex;
76  std::vector<Node> child;
77  std::string print(const std::string& tab = "");
78  };
79 
80  Node parse(std::vector<Lexem>& lexems, std::string root = "");
81 
82 private:
83 
84  std::string m_rootName;
85  std::set<std::string> m_skippableLiterals;
86  std::set<std::string> m_unskippableGrammars;
87  std::map<std::string, std::string> m_literals;
88  std::map<std::string, Lexem::TypeCode> m_literalsTyped;
89  std::map<std::string, std::string> m_grammars;
90  std::map<std::string, std::vector<Lexem>> m_grammarsParsed;
91 
92  Node parseRecursive(const std::string& g, std::vector<Lexem>& lexems, size_t& off);
93 };
94 
95 struct SyntaxTreeNode
96 {
97  enum TypeCode
98  {
99  CALL,
100  SHAPE,
101  ADDSUB,
102  MULDIV,
103  POWER,
104  CONSTANT,
105  PARAMETER,
106  OPTION,
107  BLOCK_SIZE,
108  FATAL_ERROR
109  };
110  TypeCode type = FATAL_ERROR;
111  Lexem literal;
112  Lexem flag;
113  std::vector<SyntaxTreeNode> params;
114 };
115 
116 SyntaxTreeNode syntaxParserPower(std::vector<Lexem>& lexems, size_t& off);
117 SyntaxTreeNode syntaxParserMulDiv(std::vector<Lexem>& lexems, size_t& off);
118 SyntaxTreeNode syntaxParserAddSub(std::vector<Lexem>& lexems, size_t& off);
119 SyntaxTreeNode syntaxParserOperand(std::vector<Lexem>& lexems, size_t& off);
120 SyntaxTreeNode syntaxParserBlock(std::vector<Lexem>& lexems, size_t& off);
121 SyntaxTreeNode syntaxParser(std::vector<Lexem>& lexems, bool call, SyntaxTreeNode* pBlockParams = nullptr, size_t* pCallOffset = nullptr);
122 
123 SyntaxTreeNode syntaxMixFormula(std::vector<Lexem>& lexems);
124 
125 } // namespace detail
126 } // namespace nvneural
127 
128 #endif // !NVNEURAL_SCRIPTENGINELEXYN_H