随笔-341  评论-2670  文章-0  trackbacks-0
    整整写了一天,终于基本写完了CMinus的语义分析的代码,只剩下一个检查struct是否可以计算出长度的问题(环引用造成的)。下面贴出语义分析的结果,从结果中可以看出符号表的结构以及构造方法:

    结果分成两部分,第一部分是格式化后的代码,第二部分是符号表。
  1 开始语法分析
  2 void BubbleSort (int* Input, int Count)
  3 {
  4   int i = 0;
  5   while ((i < (Count - 1)))
  6   {
  7     int j = (Count - 2);
  8     while ((j >= i))
  9     {
 10       if ((Input[j] > Input[(j + 1)]))
 11       {
 12         int Temp = Input[j];
 13         Input[j] = Input[(j + 1)];
 14         Input[(j + 1)] = Temp;
 15       }
 16       j -= 1;
 17     }
 18     i += 1;
 19   }
 20 }
 21 
 22 int Fab (int Index)
 23 {
 24   if ((Index < 2))
 25     return 1;
 26   else
 27     return (Fab((Index - 1)) + Fab((Index - 2)));
 28 }
 29 
 30 typedef struct
 31 {
 32   int Data;
 33   Node* Next;
 34 } Node;
 35 
 36 extern void* malloc (int) alias "malloc";
 37 
 38 extern void free (void*) alias "free";
 39 
 40 Node* CreateNodes ()
 41 {
 42   Node* Head = (Node*)malloc(sizeof(Node));
 43   *Head = Node{0null};
 44   Node* Tail = Head;
 45   int Current = 1;
 46   do
 47   {
 48     Node* NewNode = (Node*)malloc(sizeof(Node));
 49     *NewNode = Node{Current, null};
 50     Tail->Next = NewNode;
 51     Tail = NewNode;
 52   }
 53   while ((Current <= 4));
 54   return Head;
 55 }
 56 
 57 void FreeNodes (Node* Head)
 58 {
 59   while ((Head != null))
 60   {
 61     Node* Next = Head->Next;
 62     free(Head);
 63     Head = Next;
 64   }
 65 }
 66 
 67 void CreateAndFreeNodes ()
 68 {
 69   FreeNodes(CreateNodes());
 70 }
 71 
 72 int SubRev (int A, int B)
 73 {
 74   return (-+ B);
 75 }
 76 
 77 int Sum ()
 78 {
 79   int[5] Number = int[]{12345};
 80   int Result = 0;
 81   int Index = 0;
 82   while (true)
 83   {
 84     Result += Number[Index];
 85     Index += 1;
 86     if ((Index == 5))
 87       break;
 88     else
 89       continue;
 90   }
 91   return Result;
 92 }
 93 
 94 int GlobalA;
 95 
 96 int GlobalB = 0;
 97 
 98 const int GlobalC = 0;
 99 
100 const int GlobalD = 0;
101 
102 void Local ()
103 {
104   int LocalA;
105   int LocalB = 0;
106   const int LocalC = 0;
107   const int LocalD = 0;
108 }
109 
110 typedef int(intint) IntFunc;
111 
112 int Add (int a, int b)
113 {
114   return (a + b);
115 }
116 
117 int Sub (int a, int b)
118 {
119   return (a - b);
120 }
121 
122 void RunAll (int a, int b)
123 {
124   IntFunc[2] Funcs = IntFunc[]{Add, Sub};
125   int[2] Nums;
126   int Index = 0;
127   while ((Index < 2))
128   {
129     Nums[Index] = Funcs[Index](a, b);
130     Index += 1;
131   }
132 }
133 
134 void DifferentTypes ()
135 {
136   byte vbyte = 0;
137   unsigned byte vubyte = 0;
138   signed byte vsbyte = 0;
139   short vshort = 0;
140   unsigned short vushort = 0;
141   signed short vsshort = 0;
142   int vint = 0;
143   unsigned int vuint = 0;
144   signed int vsint = 0;
145   char vchar = L'\0';
146   wchar vwchar = L'\0';
147   float vfloat = 0.0;
148   double vdouble = 0.0;
149   bool vbool = true;
150 }
151 开始语义分析
152 语义分析完成。
153 【CMINUS TYPE TABLE】
154   TYPE<0> = signed __int8
155   TYPE<1> = unsigned __int8
156   TYPE<2> = signed __int16
157   TYPE<3> = unsigned __int16
158   TYPE<4> = signed __int32
159   TYPE<5> = unsigned __int32
160   TYPE<6> = float
161   TYPE<7> = double
162   TYPE<8> = void
163   TYPE<9> = bool
164   TYPE<10> = <WRONG-PRIMITIVE-TYPE>
165   TYPE<11> = struct (TYPE<4> Data, TYPE<16> Next)
166   TYPE<12> = function TYPE<4> (TYPE<4>, TYPE<4>)
167   TYPE<13> = pointer of TYPE<4>
168   TYPE<14> = function TYPE<8> (TYPE<13>, TYPE<4>)
169   TYPE<15> = function TYPE<4> (TYPE<4>)
170   TYPE<16> = pointer of TYPE<11>
171   TYPE<17> = pointer of TYPE<8>
172   TYPE<18> = function TYPE<17> (TYPE<4>)
173   TYPE<19> = function TYPE<8> (TYPE<17>)
174   TYPE<20> = function TYPE<16> ()
175   TYPE<21> = function TYPE<8> (TYPE<16>)
176   TYPE<22> = function TYPE<8> ()
177   TYPE<23> = function TYPE<4> ()
178   TYPE<24> = function TYPE<8> (TYPE<4>, TYPE<4>)
179   TYPE<25> = array of TYPE<4> * 5
180   TYPE<26> = array of TYPE<12> * 2
181   TYPE<27> = array of TYPE<4> * 2
182 
183 【CMINUS SYMBOL TABLE 0
184   root symbol
185   SYMBOL<Add> = local function of TYPE<12>
186   SYMBOL<BubbleSort> = local function of TYPE<14>
187   SYMBOL<CreateAndFreeNodes> = local function of TYPE<22>
188   SYMBOL<CreateNodes> = local function of TYPE<20>
189   SYMBOL<DifferentTypes> = local function of TYPE<22>
190   SYMBOL<Fab> = local function of TYPE<15>
191   SYMBOL<FreeNodes> = local function of TYPE<21>
192   SYMBOL<GlobalA> = global variable of TYPE<4>
193   SYMBOL<GlobalB> = global variable of TYPE<4>
194   SYMBOL<GlobalC> = global constant of TYPE<4>
195   SYMBOL<GlobalD> = global constant of TYPE<4>
196   SYMBOL<IntFunc> = type of TYPE<12>
197   SYMBOL<Local> = local function of TYPE<22>
198   SYMBOL<Node> = type of TYPE<11>
199   SYMBOL<RunAll> = local function of TYPE<24>
200   SYMBOL<Sub> = local function of TYPE<12>
201   SYMBOL<SubRev> = local function of TYPE<12>
202   SYMBOL<Sum> = local function of TYPE<23>
203   SYMBOL<bool> = type of TYPE<9>
204   SYMBOL<byte> = type of TYPE<0>
205   SYMBOL<char> = type of TYPE<0>
206   SYMBOL<double> = type of TYPE<7>
207   SYMBOL<float> = type of TYPE<6>
208   SYMBOL<free> = external function of TYPE<19>
209   SYMBOL<int> = type of TYPE<4>
210   SYMBOL<malloc> = external function of TYPE<18>
211   SYMBOL<null> = type of TYPE<10>
212   SYMBOL<short> = type of TYPE<2>
213   SYMBOL<signed byte> = type of TYPE<0>
214   SYMBOL<signed int> = type of TYPE<4>
215   SYMBOL<signed short> = type of TYPE<2>
216   SYMBOL<unsigned byte> = type of TYPE<1>
217   SYMBOL<unsigned int> = type of TYPE<5>
218   SYMBOL<unsigned short> = type of TYPE<3>
219   SYMBOL<void> = type of TYPE<8>
220   SYMBOL<wchar> = type of TYPE<3>
221 
222 【CMINUS SYMBOL TABLE 1
223   previous symbol is 0
224   associated function is BubbleSort
225   SYMBOL<Count> = local 1th parameter of TYPE<4>
226   SYMBOL<Input> = local 0th parameter of TYPE<13>
227 
228 【CMINUS SYMBOL TABLE 2
229   previous symbol is 1
230   associated function is BubbleSort
231   SYMBOL<i> = local variable of TYPE<4>
232 
233 【CMINUS SYMBOL TABLE 3
234   previous symbol is 2
235   associated function is BubbleSort
236 
237 【CMINUS SYMBOL TABLE 4
238   previous symbol is 3
239   associated function is BubbleSort
240   SYMBOL<j> = local variable of TYPE<4>
241 
242 【CMINUS SYMBOL TABLE 5
243   previous symbol is 4
244   associated function is BubbleSort
245 
246 【CMINUS SYMBOL TABLE 6
247   previous symbol is 5
248   associated function is BubbleSort
249 
250 【CMINUS SYMBOL TABLE 7
251   previous symbol is 6
252   associated function is BubbleSort
253   SYMBOL<Temp> = local variable of TYPE<4>
254 
255 【CMINUS SYMBOL TABLE 8
256   previous symbol is 0
257   associated function is Fab
258   SYMBOL<Index> = local 0th parameter of TYPE<4>
259 
260 【CMINUS SYMBOL TABLE 9
261   previous symbol is 8
262   associated function is Fab
263 
264 【CMINUS SYMBOL TABLE 10
265   previous symbol is 0
266   associated function is CreateNodes
267 
268 【CMINUS SYMBOL TABLE 11
269   previous symbol is 10
270   associated function is CreateNodes
271   SYMBOL<Current> = local variable of TYPE<4>
272   SYMBOL<Head> = local variable of TYPE<16>
273   SYMBOL<Tail> = local variable of TYPE<16>
274 
275 【CMINUS SYMBOL TABLE 12
276   previous symbol is 11
277   associated function is CreateNodes
278 
279 【CMINUS SYMBOL TABLE 13
280   previous symbol is 12
281   associated function is CreateNodes
282   SYMBOL<NewNode> = local variable of TYPE<16>
283 
284 【CMINUS SYMBOL TABLE 14
285   previous symbol is 0
286   associated function is FreeNodes
287   SYMBOL<Head> = local 0th parameter of TYPE<16>
288 
289 【CMINUS SYMBOL TABLE 15
290   previous symbol is 14
291   associated function is FreeNodes
292 
293 【CMINUS SYMBOL TABLE 16
294   previous symbol is 15
295   associated function is FreeNodes
296 
297 【CMINUS SYMBOL TABLE 17
298   previous symbol is 16
299   associated function is FreeNodes
300   SYMBOL<Next> = local variable of TYPE<16>
301 
302 【CMINUS SYMBOL TABLE 18
303   previous symbol is 0
304   associated function is CreateAndFreeNodes
305 
306 【CMINUS SYMBOL TABLE 19
307   previous symbol is 18
308   associated function is CreateAndFreeNodes
309 
310 【CMINUS SYMBOL TABLE 20
311   previous symbol is 0
312   associated function is SubRev
313   SYMBOL<A> = local 0th parameter of TYPE<4>
314   SYMBOL<B> = local 1th parameter of TYPE<4>
315 
316 【CMINUS SYMBOL TABLE 21
317   previous symbol is 20
318   associated function is SubRev
319 
320 【CMINUS SYMBOL TABLE 22
321   previous symbol is 0
322   associated function is Sum
323 
324 【CMINUS SYMBOL TABLE 23
325   previous symbol is 22
326   associated function is Sum
327   SYMBOL<Index> = local variable of TYPE<4>
328   SYMBOL<Number> = local variable of TYPE<25>
329   SYMBOL<Result> = local variable of TYPE<4>
330 
331 【CMINUS SYMBOL TABLE 24
332   previous symbol is 23
333   associated function is Sum
334 
335 【CMINUS SYMBOL TABLE 25
336   previous symbol is 24
337   associated function is Sum
338 
339 【CMINUS SYMBOL TABLE 26
340   previous symbol is 0
341   associated function is Local
342 
343 【CMINUS SYMBOL TABLE 27
344   previous symbol is 26
345   associated function is Local
346   SYMBOL<LocalA> = local variable of TYPE<4>
347   SYMBOL<LocalB> = local variable of TYPE<4>
348   SYMBOL<LocalC> = local constant of TYPE<4>
349   SYMBOL<LocalD> = local constant of TYPE<4>
350 
351 【CMINUS SYMBOL TABLE 28
352   previous symbol is 0
353   associated function is Add
354   SYMBOL<a> = local 0th parameter of TYPE<4>
355   SYMBOL<b> = local 1th parameter of TYPE<4>
356 
357 【CMINUS SYMBOL TABLE 29
358   previous symbol is 28
359   associated function is Add
360 
361 【CMINUS SYMBOL TABLE 30
362   previous symbol is 0
363   associated function is Sub
364   SYMBOL<a> = local 0th parameter of TYPE<4>
365   SYMBOL<b> = local 1th parameter of TYPE<4>
366 
367 【CMINUS SYMBOL TABLE 31
368   previous symbol is 30
369   associated function is Sub
370 
371 【CMINUS SYMBOL TABLE 32
372   previous symbol is 0
373   associated function is RunAll
374   SYMBOL<a> = local 0th parameter of TYPE<4>
375   SYMBOL<b> = local 1th parameter of TYPE<4>
376 
377 【CMINUS SYMBOL TABLE 33
378   previous symbol is 32
379   associated function is RunAll
380   SYMBOL<Funcs> = local variable of TYPE<26>
381   SYMBOL<Index> = local variable of TYPE<4>
382   SYMBOL<Nums> = local variable of TYPE<27>
383 
384 【CMINUS SYMBOL TABLE 34
385   previous symbol is 33
386   associated function is RunAll
387 
388 【CMINUS SYMBOL TABLE 35
389   previous symbol is 34
390   associated function is RunAll
391 
392 【CMINUS SYMBOL TABLE 36
393   previous symbol is 0
394   associated function is DifferentTypes
395 
396 【CMINUS SYMBOL TABLE 37
397   previous symbol is 36
398   associated function is DifferentTypes
399   SYMBOL<vbool> = local variable of TYPE<9>
400   SYMBOL<vbyte> = local variable of TYPE<0>
401   SYMBOL<vchar> = local variable of TYPE<0>
402   SYMBOL<vdouble> = local variable of TYPE<7>
403   SYMBOL<vfloat> = local variable of TYPE<6>
404   SYMBOL<vint> = local variable of TYPE<4>
405   SYMBOL<vsbyte> = local variable of TYPE<0>
406   SYMBOL<vshort> = local variable of TYPE<2>
407   SYMBOL<vsint> = local variable of TYPE<4>
408   SYMBOL<vsshort> = local variable of TYPE<2>
409   SYMBOL<vubyte> = local variable of TYPE<1>
410   SYMBOL<vuint> = local variable of TYPE<5>
411   SYMBOL<vushort> = local variable of TYPE<3>
412   SYMBOL<vwchar> = local variable of TYPE<3>
413 
posted on 2009-05-06 08:13 陈梓瀚(vczh) 阅读(2984) 评论(4)  编辑 收藏 引用 所属分类: JIT

评论:
# re: JIT脚本引擎:基本完成CMinus的语义分析 2009-05-06 19:05 | yindf
很赞呀。

不过输出的话,你可不可以把查表以后的结果输出来,比如TYPE<2> 写成signed int16容易看嘛。我想查表的函数你肯定有的,调用下就好。  回复  更多评论
  
# re: JIT脚本引擎:基本完成CMinus的语义分析 2009-05-06 19:24 | 陈梓瀚(vczh)
@yindf
我这么输出是因为我的确是这么保存数据的。如果都换掉的话,有时候发生了什么小错误可能就会被掩盖掉了。  回复  更多评论
  
# re: JIT脚本引擎:基本完成CMinus的语义分析 2009-05-07 21:03 | yindf
@陈梓瀚(vczh)

还是很不错的,全部变量的关系,作用域一目了然。  回复  更多评论
  
# re: JIT脚本引擎:基本完成CMinus的语义分析 2009-05-08 20:31 | 妞妞
一点看不懂,  回复  更多评论
  

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