随笔-341  评论-2670  文章-0  trackbacks-0

    今天照着C Minus的语法(略有修改)设计出了C Minus的语法树。语法树的设计尽量让生成语法树的代码易于编写。每一个结构的意义十分明确,而且结构与结构之间不需要有相互联系。举个例子,在生成语法树的时候,一个变量定义“Point p;”由两部分组成。其中第一部分是字符串"Point"而不是指向一个类型定义的指针TypeDeclaration*。当然至于Point代表什么,自有语义分析器代为理解。

    过几天等毕业论文初稿写完了,我将使用Vczh Combinator Parser为C Minus构造语法分析器。前天为Vczh Combinator Parser加上了错误处理的能力,至此这个工具已经可以为编译器服务了。

    语法树分为四大部分,其功能由四套类继承结构承担,基类分别是:
    VL_CMinusType:类型表达式
    VL_CMinusExpression:值表达式
    VL_CMinusStatement:语句表达式
    VL_CMinusDeclaration:程序全局对象声明

    下面是语法树的代码:

  1 /*******************************************************************************
  2 Vczh Library++ 2.0
  3 JIT::C简化版语法树
  4 开发者:陈梓瀚
  5 *******************************************************************************/
  6 #ifndef VL_JIT_CMINUS
  7 #define VL_JIT_CMINUS
  8 
  9 #include "..\..\..\Data\Data\VL_Data_Basic.h"
 10 #include "..\..\..\Data\Data\VL_Data_String.h"
 11 #include "..\..\..\Data\Data\VL_Data_List.h"
 12 #include "..\..\..\Data\Data\VL_Data_Map.h"
 13 
 14 namespace vl
 15 {
 16     namespace jit
 17     {
 18         namespace cminus
 19         {
 20             using namespace collection;
 21 
 22             class VL_CMinusExpressionInfo : public VL_Base
 23             {
 24             public:
 25                 VInt                            Line;
 26 
 27                 VL_CMinusExpressionInfo();
 28             };
 29 
 30 /*********************************************************************************************************
 31 类型表达式
 32 *********************************************************************************************************/
 33 
 34             class VL_CMinusType : public VL_Base
 35             {
 36             public:
 37                 typedef VL_AutoPtr<VL_CMinusType>                            Ptr;
 38                 typedef VL_List<Ptr , false , VL_CMinusType>                List;
 39                 typedef VL_List<VL_Fullpair<VUnicodeString , Ptr> , false>    NamedList;
 40 
 41                 VL_CMinusExpressionInfo            Info;
 42             };
 43 
 44             class VL_CMinusNamedType : public VL_CMinusType
 45             {
 46             public:
 47                 VUnicodeString                    Name;
 48 
 49                 VL_CMinusNamedType();
 50                 VL_CMinusNamedType(VUnicodeString aName);
 51             };
 52 
 53             class VL_CMinusPointerType : public VL_CMinusType
 54             {
 55             public:
 56                 VL_CMinusType::Ptr                Type;
 57 
 58                 VL_CMinusPointerType();
 59                 VL_CMinusPointerType(VL_CMinusType::Ptr aType);
 60             };
 61 
 62             class VL_CMinusArrayType : public VL_CMinusType
 63             {
 64             public:
 65                 VL_CMinusType::Ptr                Type;
 66                 VInt                            Count;
 67 
 68                 VL_CMinusArrayType();
 69                 VL_CMinusArrayType(VL_CMinusType::Ptr aType , VInt aCount);
 70             };
 71 
 72             class VL_CMinusFunctionType : public VL_CMinusType
 73             {
 74             public:
 75                 VL_CMinusType::Ptr                ReturnType;
 76                 VL_CMinusType::List                ParameterTypes;
 77 
 78                 VL_CMinusFunctionType();
 79                 VL_CMinusFunctionType(VL_CMinusType::Ptr aReturnType , VL_CMinusType::List& aParameterTypes);
 80             };
 81 
 82             class VL_CMinusStructType : public VL_CMinusType
 83             {
 84             public:
 85                 VL_CMinusType::NamedList        MemberTypes;
 86 
 87                 VL_CMinusStructType();
 88                 VL_CMinusStructType(VL_CMinusType::NamedList& aMemberTypes);
 89             };
 90 
 91 /*********************************************************************************************************
 92 值表达式
 93 *********************************************************************************************************/
 94 
 95             class VL_CMinusExpression : public VL_Base
 96             {
 97             public:
 98                 typedef VL_AutoPtr<VL_CMinusExpression>                        Ptr;
 99                 typedef VL_List<Ptr , false , VL_CMinusExpression>            List;
100                 typedef VL_List<VL_Fullpair<VUnicodeString , Ptr> , false>    NamedList;
101 
102                 VL_CMinusExpressionInfo            Info;
103             };
104 
105             enum VLE_CMinusPrimitiveKind
106             {
107                 vcpkInt,
108                 vcpkFloat,
109                 vcpkBool,
110                 vcpkChar,
111                 vcpkWChar,
112                 vcpkString,
113                 vcpkWString,
114                 vcpkName,
115                 vcpkNull
116             };
117             class VL_CMinusPrimitiveExpression : public VL_CMinusExpression
118             {
119             public:
120                 VLE_CMinusPrimitiveKind            Kind;
121                 VInt                            IntValue;
122                 VDouble                            FloatValue;
123                 VBool                            BoolValue;
124                 VWChar                            CharValue;
125                 VUnicodeString                    StringValue;
126 
127                 VL_CMinusPrimitiveExpression(VInt Value);
128                 VL_CMinusPrimitiveExpression(VDouble Value);
129                 VL_CMinusPrimitiveExpression(VBool Value);
130                 VL_CMinusPrimitiveExpression(VWChar Value , VBool Unicode);
131                 VL_CMinusPrimitiveExpression(VUnicodeString Value , VBool Unicode);
132                 VL_CMinusPrimitiveExpression(VUnicodeString Value);
133             };
134 
135             enum VLE_CMinusUnaryOperator
136             {
137                 vcuoPositive,
138                 vcuoNegative,
139                 vcuoBitNot,
140                 vcuoNot,
141                 vcboAddress,
142                 vcboValue
143             };
144             class VL_CMinusUnaryExpression : public VL_CMinusExpression
145             {
146             public:
147                 VLE_CMinusUnaryOperator            Operator;
148                 VL_CMinusExpression::Ptr        Expression;
149 
150                 VL_CMinusUnaryExpression();
151                 VL_CMinusUnaryExpression(VLE_CMinusUnaryOperator aOperator , VL_CMinusExpression::Ptr aExpression);
152             };
153 
154             enum VLE_CMinusBinaryOperator
155             {
156                 vcboAdd,
157                 vcboSub,
158                 vcboMul,
159                 vcboDiv,
160                 vcboMod,
161                 vcboBitAnd,
162                 vcboBitOr,
163                 vcboBitXor,
164                 vcboAnd,
165                 vcboOr,
166                 vcboXor,
167                 vcboLess,
168                 vcboLessAndEqual,
169                 vcboGreater,
170                 vcboGreaterAndEqual,
171                 vcboEqual,
172                 vcboNotEqual,
173                 vcboSubScript,
174             };
175             class VL_CMinusBinaryExpression : public VL_CMinusExpression
176             {
177             public:
178                 VLE_CMinusBinaryOperator        Operator;
179                 VL_CMinusExpression::Ptr        Left;
180                 VL_CMinusExpression::Ptr        Right;
181 
182                 VL_CMinusBinaryExpression();
183                 VL_CMinusBinaryExpression(VLE_CMinusBinaryOperator aOperator , VL_CMinusExpression::Ptr aLeft , VL_CMinusExpression::Ptr aRight);
184             };
185 
186             class VL_CMinusInvokeExpression : public VL_CMinusExpression
187             {
188             public:
189                 VL_CMinusExpression::Ptr        Function;
190                 VL_CMinusExpression::List        Parameters;
191 
192                 VL_CMinusInvokeExpression();
193                 VL_CMinusInvokeExpression(VL_CMinusExpression::Ptr aFunction , VL_CMinusExpression::List& aParameters);
194             };
195 
196             class VL_CMinusTypeCastExpression : public VL_CMinusExpression
197             {
198             public:
199                 VL_CMinusType::Ptr                Type;
200                 VL_CMinusExpression::Ptr        Expression;
201 
202                 VL_CMinusTypeCastExpression();
203                 VL_CMinusTypeCastExpression(VL_CMinusType::Ptr aType , VL_CMinusExpression::Ptr aExpression);
204             };
205 
206             class VL_CMinusArrayBuildingExpression : public VL_CMinusExpression
207             {
208             public:
209                 VL_CMinusType::Ptr                Type;
210                 VL_CMinusExpression::List        Expressions;
211 
212                 VL_CMinusArrayBuildingExpression();
213                 VL_CMinusArrayBuildingExpression(VL_CMinusType::Ptr aType , VL_CMinusExpression::List& aExpressions);
214             };
215 
216             class VL_CMinusStructBuildingExpression : public VL_CMinusExpression
217             {
218             public:
219                 VL_CMinusType::Ptr                Type;
220                 VL_CMinusExpression::NamedList    MemberValues;
221 
222                 VL_CMinusStructBuildingExpression();
223                 VL_CMinusStructBuildingExpression(VL_CMinusType::Ptr aType , VL_CMinusExpression::NamedList& aMemberValues);
224             };
225 
226 /*********************************************************************************************************
227 语句表达式
228 *********************************************************************************************************/
229 
230             class VL_CMinusStatement : public VL_Base
231             {
232             public:
233                 typedef VL_AutoPtr<VL_CMinusStatement>                Ptr;
234                 typedef VL_List<Ptr , false , VL_CMinusStatement*>    List;
235 
236                 VL_CMinusExpressionInfo            Info;
237             };
238 
239             class VL_CMinusVariableStatement : public VL_CMinusStatement
240             {
241             public:
242                 VBool                            Const;
243                 VUnicodeString                    Name;
244                 VL_CMinusType::Ptr                Type;
245                 VL_CMinusExpression::Ptr        Expression;
246 
247                 VL_CMinusVariableStatement();
248                 VL_CMinusVariableStatement(VBool aConst , VUnicodeString aName , VL_CMinusType::Ptr aType , VL_CMinusExpression::Ptr aExpression);
249             };
250 
251             class VL_CMinusExpressionStatement : public VL_CMinusStatement
252             {
253             public:
254                 VL_CMinusExpression::Ptr        Expression;
255 
256                 VL_CMinusExpressionStatement();
257                 VL_CMinusExpressionStatement(VL_CMinusExpression::Ptr aExpression);
258             };
259 
260             enum VLE_CMinusAssignmentOperator
261             {
262                 vcaoAssignment,
263                 vcaoAdd,
264                 vcaoSub,
265                 vcaoMul,
266                 vcaoDiv,
267                 vcaoMod,
268                 vcaoBitAnd,
269                 vcaoBitOr,
270                 vcaoBitXor,
271                 vcaoAnd,
272                 vcaoOr,
273                 vcaoXor
274             };
275             class VL_CMinusAssignmentStatement : public VL_CMinusStatement
276             {
277             public:
278                 VLE_CMinusAssignmentOperator    Operator;
279                 VL_CMinusExpression::Ptr        Left;
280                 VL_CMinusExpression::Ptr        Right;
281 
282                 VL_CMinusAssignmentStatement();
283                 VL_CMinusAssignmentStatement(VLE_CMinusAssignmentOperator aOperator , VL_CMinusExpression::Ptr aLeft , VL_CMinusExpression::Ptr aRight);
284             };
285 
286             class VL_CMinusIfStatement : public VL_CMinusStatement
287             {
288             public:
289                 VL_CMinusExpression::Ptr        Condition;
290                 VL_CMinusStatement::Ptr            TrueBody;
291                 VL_CMinusStatement::Ptr            FalseBody;
292 
293                 VL_CMinusIfStatement();
294                 VL_CMinusIfStatement(VL_CMinusExpression::Ptr aCondition , VL_CMinusStatement::Ptr aTrueBody , VL_CMinusStatement::Ptr aFalseBody);
295             };
296 
297             class VL_CMinusWhileStatement : public VL_CMinusStatement
298             {
299             public:
300                 VL_CMinusExpression::Ptr        Condition;
301                 VL_CMinusStatement::Ptr            Body;
302 
303                 VL_CMinusWhileStatement();
304                 VL_CMinusWhileStatement(VL_CMinusExpression::Ptr aCondition , VL_CMinusStatement::Ptr aBody);
305             };
306 
307             class VL_CMinusDoWhileStatement : public VL_CMinusStatement
308             {
309             public:
310                 VL_CMinusExpression::Ptr        Condition;
311                 VL_CMinusStatement::Ptr            Body;
312 
313                 VL_CMinusDoWhileStatement();
314                 VL_CMinusDoWhileStatement(VL_CMinusExpression::Ptr aCondition , VL_CMinusStatement::Ptr aBody);
315             };
316 
317             class VL_CMinusBreakStatement : public VL_CMinusStatement
318             {
319             public:
320             };
321 
322             class VL_CMinusContinueStatement : public VL_CMinusStatement
323             {
324             public:
325             };
326 
327             class VL_CMinusReturnStatement : public VL_CMinusStatement
328             {
329             public:
330                 VL_CMinusExpression::Ptr        Expression;
331 
332                 VL_CMinusReturnStatement();
333                 VL_CMinusReturnStatement(VL_CMinusExpression::Ptr aExpression);
334             };
335 
336             class VL_CMinusBlockStatement : public VL_CMinusStatement
337             {
338             public:
339                 VL_CMinusStatement::List        Statements;
340 
341                 VL_CMinusBlockStatement();
342                 VL_CMinusBlockStatement(VL_CMinusStatement::List& aStatements);
343             };
344 
345 /*********************************************************************************************************
346 程序结构
347 *********************************************************************************************************/
348 
349             class VL_CMinusDeclaration : public VL_Base
350             {
351             public:
352                 typedef VL_AutoPtr<VL_CMinusDeclaration>                Ptr;
353                 typedef VL_List<Ptr , false , VL_CMinusDeclaration*>    List;
354 
355                 VL_CMinusExpressionInfo            Info;
356                 VUnicodeString                    Name;
357             };
358 
359             class VL_CMinusTypeDeclaration : public VL_CMinusDeclaration
360             {
361             public:
362                 VL_CMinusType::Ptr                Type;
363 
364                 VL_CMinusTypeDeclaration();
365                 VL_CMinusTypeDeclaration(VUnicodeString aName , VL_CMinusType::Ptr aType);
366             };
367 
368             class VL_CMinusVariableDeclaration : public VL_CMinusDeclaration
369             {
370             public:
371                 VBool                            Const;
372                 VL_CMinusType::Ptr                Type;
373                 VL_CMinusExpression::Ptr        Expression;
374 
375                 VL_CMinusVariableDeclaration();
376                 VL_CMinusVariableDeclaration(VUnicodeString aName , VBool aConst , VL_CMinusType::Ptr aType , VL_CMinusExpression::Ptr aExpression);
377             };
378 
379             class VL_CMinusFunctionDeclaration : public VL_CMinusDeclaration
380             {
381             public:
382                 VL_CMinusType::Ptr                ReturnType;
383                 VL_CMinusType::NamedList        ParameterTypes;
384                 VL_CMinusStatement::Ptr            Body;
385 
386                 VL_CMinusFunctionDeclaration();
387                 VL_CMinusFunctionDeclaration(VUnicodeString aName , VL_CMinusType::Ptr aReturnType , VL_CMinusType::NamedList& aParameterTypes , VL_CMinusStatement::Ptr aBody);
388             };
389 
390             class VL_CMinusExternalFunctionDeclaration : public VL_CMinusDeclaration
391             {
392             public:
393                 VL_CMinusType::Ptr                ReturnType;
394                 VL_CMinusType::List                ParameterTypes;
395                 VUnicodeString                    Alias;
396 
397                 VL_CMinusExternalFunctionDeclaration();
398                 VL_CMinusExternalFunctionDeclaration(VUnicodeString aName , VL_CMinusType::Ptr aReturnType , VL_CMinusType::List& aParameterTypes , VUnicodeString aAlias);
399             };
400 
401             class VL_CMinusProgram : public VL_Base
402             {
403             public:
404                 VL_CMinusDeclaration::List        Declarations;
405             };
406         }
407     }
408 }
409 
410 #endif
posted on 2009-04-24 00:58 陈梓瀚(vczh) 阅读(3059) 评论(5)  编辑 收藏 引用 所属分类: JIT

评论:
# re: JIT脚本引擎:C Minus语言语法树定型 2009-04-26 22:42 | yindf
整个定义看起来很清晰,不知道后面的实现怎么样?  回复  更多评论
  
# re: JIT脚本引擎:C Minus语言语法树定型 2009-04-27 05:33 | 陈梓瀚(vczh)
语法分析不是接口的一部分,是用来给测试程序调用的,所以再烂都无所谓。不过我用了之前自己做的combinator parser,写得再乱也很清晰。  回复  更多评论
  
# re: JIT脚本引擎:C Minus语言语法树定型 2009-04-28 16:58 | 空明流转
@陈梓瀚(vczh)
NO,这个最清晰了:我在QQ上喊一句,vc啊,帮我做个编译器吧。你就帮我把方案搞定,嘎嘎。  回复  更多评论
  
# re: JIT脚本引擎:C Minus语言语法树定型 2009-04-29 09:20 | au
bu cuo  回复  更多评论
  
# re: JIT脚本引擎:C Minus语言语法树定型 2009-05-18 16:29 | 旱圣
T_T.........
没看懂。。。。。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理