• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /* 使用指令%skeleton "lalr1.cc"选择C++解析器的骨架 */
17 %skeleton "lalr1.cc"
18 
19 /* 指定bison的版本 */
20 %require "3.0.4"
21 
22 %define api.namespace {uscript} //声明命名空间与下面声明的类名结合使用 uscript::Parser::  在scanner.l中有体现
23 %define api.parser.class { Parser }
24 %define api.token.constructor
25 %define api.value.type variant //使得类型与token定义可以使用各种复杂的结构与类型
26 %defines  //生成各种头文件  location.hh position.hh  parser.hpp
27 
28 %code requires
29 {
30   /*requires中的内容会放在YYLTYPE与YYSTPYPE定义前*/
31   #include <iostream>
32   #include <string>
33   #include <vector>
34   #include <stdint.h>
35   #include <cmath>
36   #include "location.hh"
37   #include "script_statement.h"
38   #include "script_param.h"
39   #include "script_function.h"
40   #include "script_expression.h"
41 
42   using std::string;
43 
44   namespace uscript { /*避免包含头文件时冲突 */
45     class Scanner;
46     class ScriptInterpreter;
47   }
48 
49 }
50 
51 %code top
52 {
53   /* 尽可能放在parser.cpp靠近头部的地方,与requires相似 */
54   #include <iostream>
55   #include "scanner.h"
56   #include "parser.hpp"
57   #include "script_interpreter.h"
58   #include "log.h"
59 
60   /* 注意:这里的参数由%parse-param决定 */
yylex(uscript::Scanner * scanner,uscript::ScriptInterpreter * interpreter)61   static uscript::Parser::symbol_type yylex(uscript::Scanner* scanner, uscript::ScriptInterpreter* interpreter){
62     return scanner->nextToken();
63   }
64 
65   using namespace std;
66   using namespace uscript;
67   using namespace updater;
68 }
69 
70 %{
error(const location_type & loc,const std::string & msg)71 void Parser::error (const location_type& loc, const std::string& msg)
72 {
73     LOG(updater::ERROR) << "error " << msg << "  loc "  << loc << std::endl;
74 }
75 %}
76 
77 /*定义parser传给scanner的参数*/
78 %lex-param { uscript::Scanner* scanner }
79 %lex-param { uscript::ScriptInterpreter* interpreter }
80 
81 /*定义interpreter传给parser的参数*/
82 %parse-param { uscript::Scanner* scanner }
83 %parse-param { uscript::ScriptInterpreter* interpreter }
84 
85 %locations
86 
87 /*详细显示错误信息*/
88 %define parse.error verbose
89 
90 /*通过Marker::Parser::make_XXX(loc)给token添加前缀*/
91 %define api.token.prefix {TOKEN_}
92 
93 %token <int> NUMBER
94 %token <float> FLOAT
95 
96 %token EOL
97 %token END 0
98 
99 %token  <string>VAR FUNCTION GLOBAL FOR WHILE IF ELSE ADD SUB MUL DIV ASSIGN AND OR
100         EQ NE GT GE LT LE LP RP LC RC SEMICOLON IDENTIFIER
101         BREAK CONTINUE RETURN COMMA STRING
102 
103 %type <ScriptParams*>arglist
104 
105 %type <ScriptFunction*>function_definition
106 
107 %type <UScriptExpression*> definition_or_statement
108         expression value_expression compare_expression add_sub_expression mul_div_expression
109         primary_expression expression_option arg
110 
111 %type <UScriptStatementList*> block statement_list
112 
113 %type <UScriptStatement*> expression_statement return_statement continue_statement break_statement
114                 for_statement while_statement statement if_statement
115 
116 %%
117 translation_unit: definition_or_statement
118         | translation_unit definition_or_statement
119         ;
120 definition_or_statement:function_definition
121         {
122                 interpreter->AddFunction($1);
123         }
124         |statement
125         {
126                 interpreter->AddStatement($1);
127         }
128         ;
129 function_definition: FUNCTION IDENTIFIER LP arglist RP block
130         {
131                 $$ = ScriptFunction::CreateInstance($2, $4, $6);
132         }
133         |
134         FUNCTION IDENTIFIER LP RP block
135         {
136                 $$ = ScriptFunction::CreateInstance($2, nullptr, $5);
137         }
138         ;
139 statement:expression_statement
140         |for_statement
141         |while_statement
142         |if_statement
143         |break_statement
144         |continue_statement
145         |return_statement
146         ;
147 expression_statement:expression SEMICOLON
148         {
149                 $$ = UScriptStatement::CreateExpressionStatement($1);
150         }
151         ;
152 expression: value_expression
153         |IDENTIFIER ASSIGN expression
154         {
155                 $$ = AssignExpression::CreateExpression($1, $3);
156         }
157         |IDENTIFIER COMMA IDENTIFIER ASSIGN expression
158         {
159                 $$ = AssignExpression::CreateExpression($1, $5);
160                 AssignExpression::AddIdentifier($$, $3);
161         }
162         |IDENTIFIER COMMA IDENTIFIER COMMA IDENTIFIER ASSIGN expression
163         {
164                 $$ = AssignExpression::CreateExpression($1, $7);
165                 AssignExpression::AddIdentifier($$, $3);
166                 AssignExpression::AddIdentifier($$, $5);
167         }
168         |IDENTIFIER COMMA IDENTIFIER COMMA IDENTIFIER COMMA IDENTIFIER ASSIGN expression
169         {
170                 $$ = AssignExpression::CreateExpression($1, $9);
171                 AssignExpression::AddIdentifier($$, $3);
172                 AssignExpression::AddIdentifier($$, $5);
173                 AssignExpression::AddIdentifier($$, $7);
174         }
175         ;
176 value_expression: compare_expression
177         |value_expression EQ compare_expression
178         {
179                 $$ = BinaryExpression::CreateExpression(UScriptExpression::EQ_OPERATOR, $1, $3);
180         }
181         |value_expression NE compare_expression
182         {
183                 $$ = BinaryExpression::CreateExpression(UScriptExpression::NE_OPERATOR, $1, $3);
184         }
185         |value_expression AND compare_expression
186         {
187                 $$ = BinaryExpression::CreateExpression(UScriptExpression::AND_OPERATOR, $1, $3);
188         }
189         |value_expression OR compare_expression
190         {
191                 $$ = BinaryExpression::CreateExpression(UScriptExpression::OR_OPERATOR, $1, $3);
192         }
193         ;
194 compare_expression:add_sub_expression
195         |compare_expression GT add_sub_expression
196         {
197                 $$ = BinaryExpression::CreateExpression(UScriptExpression::GT_OPERATOR, $1, $3);
198         }
199         |compare_expression GE add_sub_expression
200         {
201                 $$ = BinaryExpression::CreateExpression(UScriptExpression::GE_OPERATOR, $1, $3);
202         }
203         |compare_expression LT add_sub_expression
204         {
205                 $$ = BinaryExpression::CreateExpression(UScriptExpression::LT_OPERATOR, $1, $3);
206         }
207         |compare_expression LE add_sub_expression
208         {
209                 $$ = BinaryExpression::CreateExpression(UScriptExpression::LE_OPERATOR, $1, $3);
210         }
211         ;
212 add_sub_expression:mul_div_expression
213         |add_sub_expression ADD mul_div_expression
214         {
215                 $$ = BinaryExpression::CreateExpression(UScriptExpression::ADD_OPERATOR, $1, $3);
216         }
217         |add_sub_expression SUB mul_div_expression
218         {
219                 $$ = BinaryExpression::CreateExpression(UScriptExpression::SUB_OPERATOR, $1, $3);
220         }
221         ;
222 mul_div_expression:primary_expression
223         |mul_div_expression DIV primary_expression
224         {
225                 $$ = BinaryExpression::CreateExpression(UScriptExpression::DIV_OPERATOR, $1, $3);
226         }
227         |mul_div_expression MUL primary_expression
228         {
229                 $$ = BinaryExpression::CreateExpression(UScriptExpression::MUL_OPERATOR, $1, $3);
230         }
231         ;
232 primary_expression:SUB primary_expression
233         {
234                 $$=$2;
235         }
236         |LP expression RP
237         {
238                 $$=$2;
239         }
240         |IDENTIFIER
241         {
242                 $$ = IdentifierExpression::CreateExpression($1);
243         }
244         |STRING
245         {
246                 $$ = StringExpression::CreateExpression($1);
247         }
248         |NUMBER
249         {
250                 $$ = IntegerExpression::CreateExpression($1);
251         }
252         |FLOAT
253         {
254                 $$ = FloatExpression::CreateExpression($1);
255         }
256         |IDENTIFIER LP RP
257         {
258                 $$ = FunctionCallExpression::CreateExpression($1, nullptr);
259         }
260         |IDENTIFIER LP arglist RP
261         {
262                 $$ = FunctionCallExpression::CreateExpression($1, $3);
263         }
264         ;
265 statement_list:statement_list statement
266         {
267                 $1->AddScriptStatement($2);
268                 $$ = $1;
269         }
270         |statement
271         {
272                 $$ = UScriptStatementList::CreateInstance($1);
273         }
274         ;
275 block:LC RC
276         {
277                 $$=nullptr;
278         }
279         |LC statement_list RC
280         {
281                 $$=$2;
282         }
283         ;
284 arglist:arglist COMMA arg
285         {
286                 $$ = ScriptParams::AddParams($1, $3);
287         }
288         |arg
289         {
290                 $$ = ScriptParams::CreateParams($1);
291         }
292         ;
293 arg:    value_expression
294         ;
295 expression_option:
296         {
297                 $$=nullptr;
298         }
299         |expression
300         ;
301 for_statement: FOR LP expression_option SEMICOLON expression_option SEMICOLON expression_option  RP block
302         {
303                 $$ = UScriptStatement::CreateForStatement($3,$5,$7,$9);
304         }
305         ;
306 while_statement:  WHILE LP expression_option RP block
307 	{
308 		$$ = UScriptStatement::CreateWhileStatement($3, (UScriptStatementList*)$5);
309 	}
310 	;
311 if_statement: IF LP expression RP block
312         {
313                $$ = UScriptStatement::CreateIfStatement($3,$5);
314         }
315         | IF LP expression RP block ELSE if_statement
316         {
317                 $$ = UScriptStatement::CreateIfStatement($3,$5, nullptr, $7);
318         }
319         | IF LP expression RP block ELSE block
320         {
321                 $$ = UScriptStatement::CreateIfStatement($3,$5, $7);
322         }
323         ;
324 break_statement:BREAK SEMICOLON
325         {
326                 $$ = UScriptStatement::CreateStatement(UScriptStatement::STATEMENT_TYPE_BREAK);
327         }
328         ;
329 continue_statement:CONTINUE SEMICOLON
330         {
331                 //$$=create_Statement(STATEMENT_TYPE_CONTINUE);
332                 $$ = UScriptStatement::CreateStatement(UScriptStatement::STATEMENT_TYPE_CONTINUE);
333         }
334         ;
335 return_statement:RETURN arglist SEMICOLON
336         {
337                 $$ = UScriptReturnStatement::CreateStatement($2);
338         }
339         | RETURN SEMICOLON
340         {
341                 $$ = UScriptReturnStatement::CreateStatement(nullptr);
342         }
343         ;