forked from alan2lin/hive_ql_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hive词法语法解析分析.htm
2317 lines (1880 loc) · 142 KB
/
hive词法语法解析分析.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=Generator content="Microsoft Word 12 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Cambria;
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
h1
{mso-style-link:"标题 1 Char";
margin-top:17.0pt;
margin-right:0cm;
margin-bottom:16.5pt;
margin-left:0cm;
text-align:justify;
text-justify:inter-ideograph;
line-height:240%;
page-break-after:avoid;
font-size:22.0pt;
font-family:"Calibri","sans-serif";}
h2
{mso-style-link:"标题 2 Char";
margin-top:13.0pt;
margin-right:0cm;
margin-bottom:13.0pt;
margin-left:21.25pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-21.25pt;
line-height:173%;
page-break-after:avoid;
font-size:16.0pt;
font-family:"Cambria","serif";}
h3
{mso-style-link:"标题 3 Char";
margin-top:13.0pt;
margin-right:0cm;
margin-bottom:13.0pt;
margin-left:49.6pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-1.0cm;
line-height:173%;
page-break-after:avoid;
font-size:16.0pt;
font-family:"Calibri","sans-serif";}
h4
{mso-style-link:"标题 4 Char";
margin-top:14.0pt;
margin-right:0cm;
margin-bottom:14.5pt;
margin-left:70.9pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-1.0cm;
line-height:156%;
page-break-after:avoid;
font-size:14.0pt;
font-family:"Cambria","serif";}
h5
{mso-style-link:"标题 5 Char";
margin-top:14.0pt;
margin-right:0cm;
margin-bottom:14.5pt;
margin-left:99.2pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-35.4pt;
line-height:156%;
page-break-after:avoid;
font-size:14.0pt;
font-family:"Calibri","sans-serif";}
p.MsoToc2, li.MsoToc2, div.MsoToc2
{margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:21.0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
p.MsoToc3, li.MsoToc3, div.MsoToc3
{margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:42.0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
p.MsoToc4, li.MsoToc4, div.MsoToc4
{margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:63.0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
p.MsoToc5, li.MsoToc5, div.MsoToc5
{margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:84.0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{mso-style-link:"页眉 Char";
margin:0cm;
margin-bottom:.0001pt;
text-align:center;
layout-grid-mode:char;
border:none;
padding:0cm;
font-size:9.0pt;
font-family:"Calibri","sans-serif";}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{mso-style-link:"页脚 Char";
margin:0cm;
margin-bottom:.0001pt;
layout-grid-mode:char;
font-size:9.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
code
{font-family:宋体;}
pre
{mso-style-link:"HTML 预设格式 Char";
margin-top:7.5pt;
margin-right:0cm;
margin-bottom:7.5pt;
margin-left:0cm;
font-size:12.0pt;
font-family:宋体;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-link:"批注框文本 Char";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:9.0pt;
font-family:"Calibri","sans-serif";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:21.0pt;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
p.MsoTocHeading, li.MsoTocHeading, div.MsoTocHeading
{margin-top:24.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
line-height:115%;
page-break-after:avoid;
font-size:14.0pt;
font-family:"Cambria","serif";
color:#365F91;
font-weight:bold;}
span.Char
{mso-style-name:"页眉 Char";
mso-style-link:页眉;}
span.Char0
{mso-style-name:"页脚 Char";
mso-style-link:页脚;}
span.HTMLChar
{mso-style-name:"HTML 预设格式 Char";
mso-style-link:"HTML 预设格式";
font-family:宋体;}
span.Char1
{mso-style-name:"批注框文本 Char";
mso-style-link:批注框文本;}
span.2Char
{mso-style-name:"标题 2 Char";
mso-style-link:"标题 2";
font-family:"Cambria","serif";
font-weight:bold;}
span.3Char
{mso-style-name:"标题 3 Char";
mso-style-link:"标题 3";
font-weight:bold;}
span.4Char
{mso-style-name:"标题 4 Char";
mso-style-link:"标题 4";
font-family:"Cambria","serif";
font-weight:bold;}
span.5Char
{mso-style-name:"标题 5 Char";
mso-style-link:"标题 5";
font-weight:bold;}
span.1Char
{mso-style-name:"标题 1 Char";
mso-style-link:"标题 1";
font-weight:bold;}
/* Page Definitions */
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
/* List Definitions */
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
</style>
</head>
<body lang=ZH-CN link=blue vlink=purple style='text-justify-trim:punctuation'>
<div class=Section1 style='layout-grid:15.6pt'>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>
</span><span style='font-size:16.0pt;font-family:宋体'>林氏物语</span><span
lang=EN-US style='font-size:16.0pt'>.</span><span style='font-size:16.0pt;
font-family:宋体'>技术乱弹之</span><span lang=EN-US style='font-size:16.0pt'>hive</span><span
style='font-size:16.0pt;font-family:宋体'>源码之词法语法解析</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoToc2><span
lang=EN-US><a href="#_Toc368923110"><span lang=EN-US style='font-family:宋体'><span
lang=EN-US>第一章</span></span><span style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>前言</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>1</span></a></span></p>
<p class=MsoToc2><span lang=EN-US><a href="#_Toc368923111"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第二章</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>准备工作</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>2</span></a></span></p>
<p class=MsoToc2><span lang=EN-US><a href="#_Toc368923112"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第三章</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>总体概览</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>4</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923113"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第一节</span></span><span
style='color:windowtext;text-decoration:none'> </span>hive<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>官方架构图</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>4</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923114"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第二节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>流程处理图</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>4</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923115"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第三节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>具体框架图</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>6</span></a></span></p>
<p class=MsoToc2><span lang=EN-US><a href="#_Toc368923116"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第四章</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>语法分析</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>6</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923117"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第一节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>快速定位词法语法分析的位置的方法</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>6</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923118"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第二节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>语法文件之间的关<span lang=EN-US>系</span></span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>8</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923119"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第三节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>静态分析语法文件的方法</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>9</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923120"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第四节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>动态分析语法文件的方法</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>14</span></a></span></p>
<p class=MsoToc2><span lang=EN-US><a href="#_Toc368923121"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第五章</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>语义分析阶段</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>16</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923122"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第一节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>语义处理模块的关系</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>17</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923123"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第二节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>高端大气的模板方法模式</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>18</span></a></span></p>
<p class=MsoToc3><span lang=EN-US><a href="#_Toc368923124"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第三节</span></span><span
style='color:windowtext;text-decoration:none'> </span>SemanticAnalyzer<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>的实现过程</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>20</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923125"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>1<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>处理位置别名</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>21</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923126"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>2<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>分析创建表命令</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>23</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923127"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>3<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>分析创建视图命令</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>26</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923128"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>4<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>第一阶段分析</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>26</span></a></span></p>
<p class=MsoToc5><span lang=EN-US><a href="#_Toc368923129"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>1<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小小节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>处理逻辑流程</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>26</span></a></span></p>
<p class=MsoToc5><span lang=EN-US><a href="#_Toc368923130"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>2<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小小节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>构造覆盖大多数子节点的例子</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>28</span></a></span></p>
<p class=MsoToc5><span lang=EN-US><a href="#_Toc368923131"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>3<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小小节</span></span><span
style='color:windowtext;text-decoration:none'> </span>select<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>从句的处理</span></span>(TOK_SELECTDI<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>和</span></span> TOK_SELECT<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>节点</span></span>)<span
style='color:windowtext;display:none;text-decoration:none'> </span><span
style='color:windowtext;display:none;text-decoration:none'>29</span></a></span></p>
<p class=MsoToc5><span lang=EN-US><a href="#_Toc368923132"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>4<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小小节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>关于自定义函数的插曲</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>33</span></a></span></p>
<p class=MsoToc5><span lang=EN-US><a href="#_Toc368923133"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>5<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小小节</span></span><span
style='color:windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>其他的</span></span>token<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>的处理</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>33</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923134"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>5<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>产生执行计划</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>34</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923135"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>6<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>根据选项设置的优化器进行优化</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>34</span></a></span></p>
<p class=MsoToc4><span lang=EN-US><a href="#_Toc368923136"><span lang=EN-US
style='font-family:宋体'><span lang=EN-US>第</span></span>7<span lang=EN-US
style='font-family:宋体'><span lang=EN-US>小节</span></span><span style='color:
windowtext;text-decoration:none'> </span><span
lang=EN-US style='font-family:宋体'><span lang=EN-US>产生</span></span>mr<span
lang=EN-US style='font-family:宋体'><span lang=EN-US>执行任务</span></span><span
style='color:windowtext;display:none;text-decoration:none'>... </span><span
style='color:windowtext;display:none;text-decoration:none'>34</span></a></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<h2><a name="_Toc368923110"><span lang=EN-US>第一章<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>前言</span></a></h2>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>我们知道</span><span lang=EN-US
style='font-size:14.0pt'> hive</span><span style='font-size:14.0pt;font-family:
宋体'>是一个基于</span><span lang=EN-US style='font-size:14.0pt'>hadoop</span><span
style='font-size:14.0pt;font-family:宋体'>上面提供了类</span><span lang=EN-US
style='font-size:14.0pt'>sql</span><span style='font-size:14.0pt;font-family:
宋体'>的接口的组件,以方便熟悉</span><span lang=EN-US style='font-size:14.0pt'>sql</span><span
style='font-size:14.0pt;font-family:宋体'>的人使用</span><span lang=EN-US
style='font-size:14.0pt'>hadoop</span><span style='font-size:14.0pt;font-family:
宋体'>,对大数据的处理。限于精力,本文只关注</span><span lang=EN-US style='font-size:14.0pt'>hive</span><span
style='font-size:14.0pt;font-family:宋体'>所提供的语法,它解析语法的过程,以及生成执行计划和优化这几个过程。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>本文可以作为</span><span lang=EN-US
style='font-size:14.0pt'>antlr</span><span style='font-size:14.0pt;font-family:
宋体'>实现的高级进阶教程,亦可以作为</span><span lang=EN-US style='font-size:14.0pt'>hive</span><span
style='font-size:14.0pt;font-family:宋体'>的源码分析教程,毕竟,语法词法解析,执行计划的生成优化以及执行也是</span><span
lang=EN-US style='font-size:14.0pt'>hive</span><span style='font-size:14.0pt;
font-family:宋体'>的核心。这不是定位不清晰,而是我有这样的需求,既想了解</span><span lang=EN-US
style='font-size:14.0pt'>antlr</span><span style='font-size:14.0pt;font-family:
宋体'>的具体使用,又想深入了解</span><span lang=EN-US style='font-size:14.0pt'>hive</span><span
style='font-size:14.0pt;font-family:宋体'>。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>本文所用到的自绘的图尽量采用</span><span lang=EN-US
style='font-size:14.0pt'>graphviz</span><span style='font-size:14.0pt;
font-family:宋体'>生成,考虑到他们放入</span><span lang=EN-US style='font-size:14.0pt'>word</span><span
style='font-size:14.0pt;font-family:宋体'>会失真,所以给提供了最原始的生成文件,你可以按照自己的需要生成各种格式,他们都存放在</span><span
lang=EN-US style='font-size:14.0pt'>dot_file</span><span style='font-size:14.0pt;
font-family:宋体'>的目录下。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>最后,本文随时会根据作者的喜好和发现而做调整,如果你觉得那些地方不妥,请来信指正或者指教</span><span
lang=EN-US style='font-size:14.0pt'>:workspace.public@gmail.com</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<h2><a name="_Toc368923111"><span lang=EN-US>第二章<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>准备工作</span></a></h2>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>分析对象</span><span
lang=EN-US style='font-size:14.0pt'>:hive</span><span style='font-size:14.0pt;
font-family:宋体'>源代码,版本</span><span lang=EN-US style='font-size:14.0pt'>Revision
1522497</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>需要用到的工具:</span><span
lang=EN-US style='font-size:14.0pt'> svn ant antlr-3.5-complete.jar</span><span
style='font-size:14.0pt;font-family:宋体'>,</span><span lang=EN-US
style='font-size:14.0pt'>antlrworks-1.4.2.jar</span><span style='font-size:
14.0pt;font-family:宋体'>,</span><span lang=EN-US style='font-size:14.0pt'>eclipse</span><span
style='font-size:14.0pt;font-family:宋体'>。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>注:在</span><span
lang=EN-US style='font-size:14.0pt'>http://www.antlr3.org/download.html </span><span
style='font-size:14.0pt;font-family:宋体'>可以下载</span><span lang=EN-US
style='font-size:14.0pt'>antlr-3.5-complete.jar</span><span style='font-size:
14.0pt;font-family:宋体'>,</span><span lang=EN-US style='font-size:14.0pt'>antlrworks-1.4.2.jar.</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>hive</span><span
style='font-size:14.0pt;font-family:宋体'>中使用到的</span><span lang=EN-US
style='font-size:14.0pt'>antlr</span><span style='font-size:14.0pt;font-family:
宋体'>是</span><span lang=EN-US style='font-size:14.0pt'> v3.4</span><span
style='font-size:14.0pt;font-family:宋体'>版本的,使用</span><span lang=EN-US
style='font-size:14.0pt'>v3.5</span><span style='font-size:14.0pt;font-family:
宋体'>没有问题,但请别使用</span><span lang=EN-US style='font-size:14.0pt'>v4.0</span><span
style='font-size:14.0pt;font-family:宋体'>的。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>下载源代码</span><span
lang=EN-US style='font-size:14.0pt'>,</span><span style='font-size:14.0pt;
font-family:宋体'>并编译</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>svn co
http://svn.apache.org/repos/asf/hive/trunk hive</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>cd hive</span></p>
<pre style='background:white'><span lang=EN-US style='font-size:14.0pt;
font-family:"Calibri","sans-serif"'>ant clean package eclipse-files</span></pre>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>最后一个是为了方便导入成</span><span
lang=EN-US style='font-size:14.0pt'>eclipse</span><span style='font-size:14.0pt;
font-family:宋体'>项目,有些人是喜欢用</span><span lang=EN-US style='font-size:14.0pt'>eclipse</span><span
style='font-size:14.0pt;font-family:宋体'>查看源代码。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>安装部署好分布式的</span><span
lang=EN-US style='font-size:14.0pt'>hadoop</span><span style='font-size:14.0pt;
font-family:宋体'>,并安装好</span><span lang=EN-US style='font-size:14.0pt'>hive</span><span
style='font-size:14.0pt;font-family:宋体'>。可以跑例子</span><span lang=EN-US
style='font-size:14.0pt'>...</span><span style='font-size:14.0pt;font-family:
宋体'>具体的可以参考网络上的教程</span><span lang=EN-US style='font-size:14.0pt'>,</span><span
style='font-size:14.0pt;font-family:宋体'>一搜一大把。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>文中准备的是官方网的测试数据。所以你需要执行以下语句</span><span
lang=EN-US style='font-size:14.0pt'>:</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>CREATE TABLE
invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>LOAD DATA LOCAL
INPATH '../examples/files/kv2.txt' OVERWRITE INTO TABLE invites PARTITION
(ds='2008-08-15');</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>LOAD DATA LOCAL
INPATH '../examples/files/kv3.txt' OVERWRITE INTO TABLE invites PARTITION
(ds='2008-08-08');</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>注意</span><span
lang=EN-US style='font-size:14.0pt'>:</span><span style='font-size:14.0pt;
font-family:宋体'>数据文件在本地的位置要根据你启动</span><span lang=EN-US style='font-size:14.0pt'>hive</span><span
style='font-size:14.0pt;font-family:宋体'>的不同而改变。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<h2><a name="_Toc368923112"><span lang=EN-US>第三章<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>总体概览</span></a></h2>
<h3><a name="_Toc368923113"><span lang=EN-US>第一节<span style='font:7.0pt "Times New Roman"'>
</span></span><span lang=EN-US>hive</span></a><span style='font-family:宋体'>官方架构图</span></h3>
<p class=MsoNormal><span lang=EN-US><img width=554 height=310
src="hive词法语法解析分析.files/image001.jpg"></span></p>
<p class=MsoNormal><span style='font-family:宋体'>摘自</span><span lang=EN-US>
hadoop </span><span style='font-family:宋体'>权威指南</span></p>
<p class=MsoNormal><span lang=EN-US> </span></p>
<h3><a name="_Toc368923114"><span lang=EN-US>第二节<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>流程处理图</span></a></h3>
<p class=MsoNormal><span lang=EN-US> <img width=617 height=930
src="hive词法语法解析分析.files/image002.png" alt="proccess_flow.dot.png"></span></p>
<p class=MsoNormal><span style='font-family:宋体'>这张图是从源代码中剥茧抽丝的出来的抽象,忽略掉了无关紧要的细节。</span></p>
<p class=MsoNormal><span style='font-family:宋体'>这张图描述了</span><span lang=EN-US>
hive</span><span style='font-family:宋体'>从解析语句到执行的过程。</span> <span
style='font-family:宋体'>后续的内容会以为大纲详细展开。</span></p>
<h3><a name="_Toc368923115"><span lang=EN-US>第三节<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>具体框架图</span></a></h3>
<p class=MsoNormal><span lang=EN-US>(</span><span style='font-family:宋体'>待完善</span><span
lang=EN-US>)</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<h2><a name="_Toc368923116"><span lang=EN-US>第四章<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>语法分析</span></a></h2>
<p class=MsoNormal><span lang=EN-US> </span><span
style='font-size:14.0pt;font-family:宋体'>对一个已有的项目进行语法分析,首先是要找到语法分析文件,分析语法文件之间的关系,然后根据语法分析文件提供的接口,确定语法解析在这个体系中所处的位置。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>
</span><span style='font-size:14.0pt;font-family:宋体'>在分析语法文件的时候,一般常用两种分析方法</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>静态分析和动态分析方法。静态分析方法常见是读语法文件,</span><span style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>或者使用可视化工具查看语法文件。动态分析方法是运行该解析程序,查看运行过程中语法识别的情况以及相关的具体语法树的构建过程以及抽象语法树构建过程</span><span
lang=EN-US style='font-size:14.0pt'>(</span><span style='font-size:14.0pt;
font-family:宋体'>如果可能有抽象语法树的话</span><span lang=EN-US style='font-size:14.0pt'>)</span><span
style='font-size:14.0pt;font-family:宋体'>。</span></p>
<p class=MsoNormal><span lang=EN-US> </span></p>
<h3><a name="_Toc368923117"><span lang=EN-US>第一节<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>快速定位词法语法分析的位置的方法</span></a></h3>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>搜索</span><span
lang=EN-US style='font-size:14.0pt'> .g</span><span style='font-size:14.0pt;
font-family:宋体'>的位置</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>find ./ -name
"*.g"</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>会得到</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./ql/src/java/org/apache/hadoop/hive/ql/parse/FromClauseParser.g</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./ql/src/java/org/apache/hadoop/hive/ql/parse/SelectClauseParser.g</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./ql/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./ql/src/java/org/apache/hadoop/hive/ql/parse/HiveLexer.g</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./metastore/src/java/org/apache/hadoop/hive/metastore/parser/Filter.g</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>基本可以确定了</span><span
lang=EN-US style='font-size:14.0pt'> ql</span><span style='font-size:14.0pt;
font-family:宋体'>下面就是需要找到东西。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>find . -name
"*.java" |xargs grep "HiveLexer "</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>找到两处</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./build/ql/gen/antlr/gen-java/org/apache/hadoop/hive/ql/parse/HiveLexer.java:public
class HiveLexer extends Lexer {</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>./ql/src/java/org/apache/hadoop/hive/ql/parse/ParseDriver.java:
public class HiveLexerX extends HiveLexer {</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>其中第一处是编译期构建的,</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>而另外一处是包装了该类。重点可以放到第二处。也即</span><span lang=EN-US style='font-size:14.0pt'>parseDriver</span><span
style='font-size:14.0pt;font-family:宋体'>类。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>对</span><span
lang=EN-US style='font-size:14.0pt'>parseDriver</span><span style='font-size:
14.0pt;font-family:宋体'>类做一个大致的分析,</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>包装了</span><span
lang=EN-US style='font-size:14.0pt'>antlr</span><span style='font-size:14.0pt;
font-family:宋体'>的</span><span lang=EN-US style='font-size:14.0pt'>antlrStringStream
</span><span style='font-size:14.0pt;font-family:宋体'>成为</span><span lang=EN-US
style='font-size:14.0pt'>antlrnocasestream</span><span style='font-size:14.0pt;
font-family:宋体'>。消除了大小写敏感。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>包装了词法解析器</span><span
style='font-size:14.0pt'> <span lang=EN-US>HiveLexer</span></span><span
style='font-size:14.0pt;font-family:宋体'>,主要是包装了出错信息。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>包装了抽象树节点,主要聚合了</span><span
lang=EN-US style='font-size:14.0pt'>ASTNodeOrigin </span><span
style='font-size:14.0pt;font-family:宋体'>以求能获得对象类型,名字,定义,别名,和定义。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>其中</span><span
lang=EN-US style='font-size:14.0pt'>parser</span><span style='font-size:14.0pt;
font-family:宋体'>方法是我们重点关注的,它调用词法解析和语法解析。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>察看其所有的调用者的关系形成的路径,找到一条通向</span><span
lang=EN-US style='font-size:14.0pt'>cli</span><span style='font-size:14.0pt;
font-family:宋体'>的包的</span><span lang=EN-US style='font-size:14.0pt'>clidriver</span><span
style='font-size:14.0pt;font-family:宋体'>类</span><span lang=EN-US
style='font-size:14.0pt'>main</span><span style='font-size:14.0pt;font-family:
宋体'>方法,(可以通过</span><span lang=EN-US style='font-size:14.0pt'>eclipse</span><span
style='font-size:14.0pt;font-family:宋体'>的北调用关系反向查看)</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>clidriver.run()
->clidriver.executeDriver()->clidriver.processLine(string,boolean)->clidriver.processCmd()->clidriver.processloaclcmd()->driver.run->driver.runInternal()->driver.compile()->parserdriver.parse</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>而我们知道这也是其中的入口之一。</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>基本上可以快速的确定其所用到的词法语法解析了。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<h3><a name="_Toc368923118"><span lang=EN-US>第二节<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>语法文件之间的关系</span></a></h3>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>翻阅几个</span><span
lang=EN-US style='font-size:14.0pt'>.g</span><span style='font-size:14.0pt;
font-family:宋体'>文件可以得知</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>HiveLexer.g </span><span
style='font-size:14.0pt;font-family:宋体'>是做词法分析的,定义了所有用到的</span><span
lang=EN-US style='font-size:14.0pt'>token</span><span style='font-size:14.0pt;
font-family:宋体'>。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>HiveParser.g </span><span
style='font-size:14.0pt;font-family:宋体'>是做语法解析的。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>FromClauseParser.g
from</span><span style='font-size:14.0pt;font-family:宋体'>从句语法解析</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>SelectClauseParser.g
select </span><span style='font-size:14.0pt;font-family:宋体'>从句语法解析。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>IdentifiersParser.g
</span><span style='font-size:14.0pt;font-family:宋体'>自定义函数的解析,</span><span
lang=EN-US style='font-size:14.0pt'>hive</span><span style='font-size:14.0pt;
font-family:宋体'>中的自定义函数范围很广,各种内建的库函数,包括操作符之类的都被归为自定义的函数,而在语义解析的时候给以甄别。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>关系图</span><span
lang=EN-US style='font-size:14.0pt'>:</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'><img width=554
height=273 id="图片 9" src="hive词法语法解析分析.files/image003.png"
alt="gfile_relation.dot.png"></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>熟悉</span><span
lang=EN-US style='font-size:14.0pt'>antlr v3</span><span style='font-size:14.0pt;
font-family:宋体'>的朋友一般会奇怪,为什么这里的词法语法怎么不是在一个文件,或者词法语法结对出现呢?</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>这里面用到了一种叫</span><span lang=EN-US style='font-size:14.0pt'>composite
grammars</span><span style='font-size:14.0pt;font-family:宋体'>的技术。</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>这种高级货是</span><span lang=EN-US style='font-size:14.0pt'>antlr v3.1</span><span
style='font-size:14.0pt;font-family:宋体'>开始引进的。</span><span style='font-size:
14.0pt'> </span><span style='font-size:14.0pt;font-family:宋体'>是为了解决把所有语法塞入到一个文件里导致编译出来的</span><span
lang=EN-US style='font-size:14.0pt'>java</span><span style='font-size:14.0pt;
font-family:宋体'>文件过大和逻辑多了之后不容易阅读的问题。它允许在逻辑上把一个大语法划分成几大块,独立实现,然后合并在一起。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>HiveParser.g </span><span
style='font-size:14.0pt;font-family:宋体'>有一行</span><span lang=EN-US
style='font-size:14.0pt'> import SelectClauseParser, FromClauseParser,
IdentifiersParser; </span><span style='font-size:14.0pt;font-family:宋体'>类似于</span><span
lang=EN-US style='font-size:14.0pt'> c</span><span style='font-size:14.0pt;
font-family:宋体'>中的</span><span lang=EN-US style='font-size:14.0pt'> include SelectClauseParser.g,
FromClauseParser.g, IdentifiersParser.g</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<h3><a name="_Toc368923119"><span lang=EN-US>第三节<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>静态分析语法文件的方法</span></a></h3>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>分析一个词法语法的实现,最好的方式是跟随着语法规则走读一边逐一标注。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>当然,更快捷有效的方式是在静态分析方面使用一些可视化的工具来察看语法结构如语法图,</span></p>
<h5><span lang=EN-US>第1小小节<span style='font:7.0pt "Times New Roman"'>
</span></span><span style='font-family:宋体'>使用</span><span lang=EN-US>antlrworks</span><span
style='font-family:宋体'>来查看语法结构</span> </h5>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>具体的使用初学者请参照</span><span
style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>http://www.github.com/alan2lin/hand_in_hand_with_antlr</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>中的安装与使用。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>hivelexer</span><span
style='font-size:14.0pt;font-family:宋体'>不需要关注太多,就是识别关键字和</span><span
lang=EN-US style='font-size:14.0pt'>token</span><span style='font-size:14.0pt;
font-family:宋体'>的,重点是</span><span lang=EN-US style='font-size:14.0pt'>
hiveparser</span><span style='font-size:14.0pt;font-family:宋体'>。</span></p>
<h5><span lang=EN-US>第2小小节<span style='font:7.0pt "Times New Roman"'>
</span></span><span lang=EN-US>antlr</span><span style='font-family:宋体'>语法的简单介绍</span></h5>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>关于</span><span
lang=EN-US style='font-size:14.0pt'>antlr</span><span style='font-size:14.0pt;
font-family:宋体'>语法规范,详细请参见</span><span lang=EN-US style='font-size:14.0pt'>
hand in hand with antlr</span><span style='font-size:14.0pt;font-family:宋体'>中的语法规范的翻译。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>这里只做简单的介绍,以便没有基础的人也可以读懂。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>parser grammar
HiveParser; </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>//</span><span
style='font-size:14.0pt;font-family:宋体'>标准格式</span><span style='font-size:14.0pt'>
<span lang=EN-US>parser grammar </span></span><span style='font-size:14.0pt;
font-family:宋体'>后面跟语法名。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>options //</span><span
style='font-size:14.0pt;font-family:宋体'>选项</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>{</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>tokenVocab=HiveLexer;
//</span><span style='font-size:14.0pt;font-family:宋体'>词汇表来源于</span><span
style='font-size:14.0pt'> <span lang=EN-US>HiveLexer</span></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>output=AST;
//</span><span style='font-size:14.0pt;font-family:宋体'>输出</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>抽象语法树</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>ASTLabelType=CommonTree;
//</span><span style='font-size:14.0pt;font-family:宋体'>抽象语法树类型为</span><span
lang=EN-US style='font-size:14.0pt'> commonTtree</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>backtrack=false;
//</span><span style='font-size:14.0pt;font-family:宋体'>不回溯</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>k=3;
//</span><span style='font-size:14.0pt;font-family:宋体'>前向窥看</span><span
lang=EN-US style='font-size:14.0pt'>3</span><span style='font-size:14.0pt;
font-family:宋体'>个</span><span lang=EN-US style='font-size:14.0pt'>token</span><span
style='font-size:14.0pt;font-family:宋体'>的长度。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>}</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>import
SelectClauseParser, FromClauseParser, IdentifiersParser;</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>//map</span><span
style='font-size:14.0pt;font-family:宋体'>建立的过程</span><span lang=EN-US
style='font-size:14.0pt'>....</span><span style='font-size:14.0pt;font-family:
宋体'>省略</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>//</span><span
style='font-size:14.0pt;font-family:宋体'>整个规则由</span><span lang=EN-US
style='font-size:14.0pt'>statement</span><span style='font-size:14.0pt;
font-family:宋体'>开始。</span><span lang=EN-US style='font-size:14.0pt'> statement </span><span
style='font-size:14.0pt;font-family:宋体'>由</span><span style='font-size:14.0pt'>
</span><span style='font-size:14.0pt;font-family:宋体'>解释语句</span><span
lang=EN-US style='font-size:14.0pt'>explainStatement</span><span
style='font-size:14.0pt;font-family:宋体'>或执行语句</span><span lang=EN-US
style='font-size:14.0pt'>execStatement</span><span style='font-size:14.0pt;
font-family:宋体'>组成。这种形式叫产生式</span><span style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>冒号左边的是左部,作为代表这个产生式规则的符号。</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>冒号右边是右部,</span><span style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>连接符号</span><span lang=EN-US
style='font-size:14.0pt'> | </span><span style='font-size:14.0pt;
font-family:宋体'>表示</span><span style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>右部的组成部分是或者的关系,还有另一种解读的意思是</span><span
lang=EN-US style='font-size:14.0pt'>:</span><span style='font-size:14.0pt;
font-family:宋体'>从</span><span lang=EN-US style='font-size:14.0pt'>statement</span><span
style='font-size:14.0pt;font-family:宋体'>开始,产生了</span><span lang=EN-US
style='font-size:14.0pt'> explainstatement </span><span style='font-size:14.0pt;
font-family:宋体'>或者</span><span lang=EN-US style='font-size:14.0pt'>
execstatement</span><span style='font-size:14.0pt;font-family:宋体'>。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>// starting rule</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>statement</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> :
explainStatement EOF</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> |
execStatement EOF</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> ;</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>//</span><span
style='font-size:14.0pt;font-family:宋体'>解释语句</span><span style='font-size:14.0pt'>
<span lang=EN-US>explainStatement </span></span><span style='font-size:14.0pt;
font-family:宋体'>由</span><span lang=EN-US style='font-size:14.0pt'>KW_EXPLAIN </span><span
style='font-size:14.0pt;font-family:宋体'>开始</span><span style='font-size:14.0pt'>
</span><span style='font-size:14.0pt;font-family:宋体'>中间有可选项</span><span
style='font-size:14.0pt'> <span lang=EN-US>KW_EXTENDED</span></span><span
style='font-size:14.0pt;font-family:宋体'>,</span><span lang=EN-US
style='font-size:14.0pt'>KW_FORMATTED KW_DEPENDENCY KW_LOGICAL </span><span
style='font-size:14.0pt;font-family:宋体'>,</span><span style='font-size:14.0pt'>
</span><span style='font-size:14.0pt;font-family:宋体'>后面紧跟着</span><span
style='font-size:14.0pt'> </span><span style='font-size:14.0pt;font-family:
宋体'>执行语句</span><span style='font-size:14.0pt'> </span><span style='font-size:
14.0pt;font-family:宋体'>。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>通过观测,</span><span
lang=EN-US style='font-size:14.0pt'>KW_ </span><span style='font-size:14.0pt;
font-family:宋体'>开始的</span><span lang=EN-US style='font-size:14.0pt'>token</span><span
style='font-size:14.0pt;font-family:宋体'>代表</span><span style='font-size:14.0pt'>
</span><span style='font-size:14.0pt;font-family:宋体'>关键字。</span></p>
<p class=MsoNormal><span style='font-size:14.0pt;font-family:宋体'>语法形式</span><span
lang=EN-US style='font-size:14.0pt'>: </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>@init </span><span
style='font-size:14.0pt;font-family:宋体'>表示进入规则时执行后面的</span><span lang=EN-US
style='font-size:14.0pt'>{}</span><span style='font-size:14.0pt;font-family:
宋体'>里的动作</span><span style='font-size:14.0pt'> </span><span style='font-size:
14.0pt;font-family:宋体'>,例中,压入</span><span lang=EN-US style='font-size:14.0pt'>trace</span><span
style='font-size:14.0pt;font-family:宋体'>的消息。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>@after{} </span><span
style='font-size:14.0pt;font-family:宋体'>表示规则完成后执行</span><span lang=EN-US
style='font-size:14.0pt'>{}</span><span style='font-size:14.0pt;font-family:
宋体'>里面的动作。</span><span lang=EN-US style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>例中,弹出</span><span lang=EN-US
style='font-size:14.0pt'>trace</span><span style='font-size:14.0pt;font-family:
宋体'>的消息。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>-></span><span
style='font-size:14.0pt;font-family:宋体'>构建语法抽象树</span><span lang=EN-US
style='font-size:14.0pt'> ^(rootnode leafnode1 leafnode2...) </span><span
style='font-size:14.0pt;font-family:宋体'>如例</span><span style='font-size:14.0pt'>
</span><span style='font-size:14.0pt;font-family:宋体'>表示构建一个以</span><span
style='font-size:14.0pt'> <span lang=EN-US>TOK_EXPLAIN </span></span><span
style='font-size:14.0pt;font-family:宋体'>为根节点</span><span lang=EN-US
style='font-size:14.0pt'> execStatement </span><span
style='font-size:14.0pt;font-family:宋体'>为第一个叶结点,</span><span style='font-size:
14.0pt'> </span><span style='font-size:14.0pt;font-family:宋体'>可选项为第二个叶结点,如果有可选项的话。</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>explainOptions=KW_EXTENDED
</span><span style='font-size:14.0pt;font-family:宋体'>定义了</span><span
style='font-size:14.0pt'> <span lang=EN-US>explainOptions</span></span><span
style='font-size:14.0pt;font-family:宋体'>作为别名引用</span><span lang=EN-US
style='font-size:14.0pt'>KW_EXTENDED</span><span style='font-size:14.0pt;
font-family:宋体'>,</span><span style='font-size:14.0pt'> </span><span
style='font-size:14.0pt;font-family:宋体'>引用形式为</span><span lang=EN-US
style='font-size:14.0pt'> $ explainOptions.</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>explainStatement</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>@init {
msgs.push("explain statement"); }</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>@after {
msgs.pop(); }</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> :
KW_EXPLAIN (explainOptions=KW_EXTENDED|explainOptions=KW_FORMATTED|explainOptions=KW_DEPENDENCY|explainOptions=KW_LOGICAL)?
execStatement</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>
-> ^(TOK_EXPLAIN execStatement $explainOptions?)</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> ;</span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'> </span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:14.0pt'>//</span><span
style='font-size:14.0pt;font-family:宋体'>执行语句</span><span lang=EN-US