-
Notifications
You must be signed in to change notification settings - Fork 2
/
pascal语言的编译程序.java
2227 lines (2092 loc) · 61.7 KB
/
pascal语言的编译程序.java
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
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class fuhaobiao extends Frame{
static final int CHACHE_LENGTH=100;//final定义常量,它的值在运行时不能被改变
static int offset=0;//偏移量可能有用吧
JFrame mainFrame;
JTextArea input,output,code_run;
JList lineNumberColumn;
JScrollPane input_Pane, output_Pane, table_Pane;
//JButton getIt;
JTable table,out;
//JPanel getin;
JLabel wenben,shuchu;
DefaultTableModel tableModel,errorModel;
Cff jisuan = new Cff();
private JMenuBar bar;
private JMenuItem openItem,saveItem,runItem;//,closeItem;
private JMenu fileMenu,runMenu;
private FileDialog openDia,saveDia;
private File file;
JToolBar toolbar;
JSeparator sepv;
//表-2016-5-24
public fuhaobiao ()
{
mainFrame = new JFrame("编译器");
GridBagLayout layout = new GridBagLayout();
Container contentPane = mainFrame.getContentPane();
contentPane.setLayout(layout);
wenben =new JLabel("文本编辑区");
shuchu =new JLabel("生成的汇编代码");
//Integer []liine={1};
lineNumberColumn = new JList();
final DefaultListModel listModel = new DefaultListModel();
lineNumberColumn.setModel(listModel );
listModel.addElement("1"+" ");
//创建菜单条
bar = new JMenuBar();
bar.setForeground(Color.gray);
toolbar = new JToolBar();
//创建菜单和菜单条目
fileMenu = new JMenu("File");
fileMenu.setFont(new Font("微软雅黑",Font.BOLD,12));
runMenu = new JMenu("Run");
runMenu.setFont(new Font("微软雅黑",Font.BOLD,12));
runItem=new JMenuItem("Run");
runItem.setFont(new Font("微软雅黑",Font.BOLD,12));
//ImageIcon logoing=new ImageIcon("go-next-view.png"); //这里定义一个Icon图片
runItem.setIcon(new ImageIcon("go-next-view.png"));
runItem.setPreferredSize(new Dimension(200,18));//设置初始宽度
runItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));//运行快捷键为ctrl+r
openItem = new JMenuItem("Open");
openItem.setPreferredSize(new Dimension(200,18));
openItem.setFont(new Font("微软雅黑",Font.BOLD,12));
openItem.setIcon(new ImageIcon("document-open.png"));
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));//运行快捷键为ctrl+o
saveItem = new JMenuItem("Save");
saveItem.setFont(new Font("微软雅黑",Font.BOLD,12));
saveItem.setPreferredSize(new Dimension(200,18));
saveItem.setIcon(new ImageIcon("document-save-as.png"));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));//运行快捷键为ctrl+s
//closeItem = new MenuItem("退出");
//为菜单添加菜单条目
fileMenu.add(openItem);
//fileMenu.addSeparator();//设置分割符
fileMenu.add(saveItem);
//fileMenu.add(closeItem);
runMenu.add(runItem);
//将菜单添加到菜单条中
bar.add(fileMenu);
bar.add(runMenu);
//为窗体设置菜单条
mainFrame.setJMenuBar(bar);
//创建“打开”和“保存”对话框
openDia = new FileDialog(mainFrame,"open",FileDialog.LOAD);
saveDia = new FileDialog(mainFrame,"save",FileDialog.SAVE);
//错误分析
JTable table = new JTable(){public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
//table.setForeground(Color.RED);
public String getToolTipText(MouseEvent e) {
int row=this.rowAtPoint(e.getPoint());
int col=this.columnAtPoint(e.getPoint());
String tiptextString=null;
if(row>-1 && col>-1){
Object value=this.getValueAt(row, col);
if(null!=value && !"".equals(value))
tiptextString=value.toString();//悬浮显示单元格内容
}
return tiptextString;
}
};
table.setForeground(Color.RED);
table.setPreferredScrollableViewportSize(new Dimension(850, 100));
errorModel = (DefaultTableModel) table.getModel();
errorModel.addColumn("错误类型");
errorModel.addColumn("错误详情");
errorModel.addColumn("错误行数");
//errorModel.
//结果居中显示
DefaultTableCellRenderer rr = new DefaultTableCellRenderer(); //结果居中显示
rr.setHorizontalAlignment(JLabel.CENTER);
//r.setBackground(Color.GRAY);
table.setDefaultRenderer(Object.class, rr);
//设置宽度
TableColumn err = table.getColumnModel().getColumn(0);//取第一列
err.setPreferredWidth(200);
err = table.getColumnModel().getColumn(1);
err.setPreferredWidth(550);
err = table.getColumnModel().getColumn(2);
err.setPreferredWidth(100);
//输出结果
JTable output = new JTable(){public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
public String getToolTipText(MouseEvent e) {
int row=this.rowAtPoint(e.getPoint());
int col=this.columnAtPoint(e.getPoint());
String tiptextString=null;
if(row>-1 && col>-1){
Object value=this.getValueAt(row, col);
if(null!=value && !"".equals(value))
tiptextString=value.toString();//悬浮显示单元格内容
}
return tiptextString;
}
};
output.setPreferredScrollableViewportSize(new Dimension(350, 350));
tableModel = (DefaultTableModel) output.getModel();
tableModel.addColumn("单词");
tableModel.addColumn("行号");
DefaultTableCellRenderer r = new DefaultTableCellRenderer(); //结果居中显示
r.setHorizontalAlignment(JLabel.CENTER);
//r.setBackground(Color.GRAY);
output.setDefaultRenderer(Object.class, r);
//设置宽度
TableColumn column = output.getColumnModel().getColumn(0);//取第一列
column.setPreferredWidth(250);
column = output.getColumnModel().getColumn(1);
column.setPreferredWidth(100);
//////////////////////////////////////////////////////////////////
input =new JTextArea(20,44);
code_run=new JTextArea(20,30);
//input.setPreferredSize(new Dimension(350,370));
//output =new JTextArea(20,30);
//input.setLineWrap(true);// 激活自动换行功能
input.setWrapStyleWord(true);// 激活断行不断字功能
lineNumberColumn.setFocusable( false );
code_run.setWrapStyleWord(true);// 激活断行不断字功能
lineNumberColumn.setFocusable( false );
input.getDocument( ).addDocumentListener( new DocumentListener() {
public void insertUpdate( DocumentEvent e ) {
//lineNumberColumn.( "" );
listModel.removeAllElements();
for ( int i = 1; i <= input.getLineCount( ); i++ )
listModel.addElement(i+" ");
//lineNumberColumn.append( " " + i + " \n" );
}
public void removeUpdate( DocumentEvent e ) { insertUpdate( e ); }
public void changedUpdate( DocumentEvent e ) { insertUpdate( e ); }
} );
//listModel.getSize();
//getIt = new JButton("词法分析");
//getIt.addActionListener(jisuan);
runItem.addActionListener(jisuan);
//getin = new JPanel();
//getin.add(getIt);
//input.setFont(new Font("Courier New",Font.BOLD,18));
//lineNumberColumn.setFixedCellHeight();
lineNumberColumn.setFixedCellHeight(18);//设置List的每一行的高度
input_Pane = new JScrollPane(input);
output_Pane = new JScrollPane(code_run);
// output_Pane = new JScrollPane(output);
table_Pane = new JScrollPane(table);
//input.setRowHeaderView(new JList());
contentPane.add(wenben);
contentPane.add(shuchu);
contentPane.add(input_Pane);
//contentPane.add(getin);
contentPane.add(output_Pane);
contentPane.add(table_Pane);
input_Pane.setRowHeaderView( lineNumberColumn );
lineNumberColumn.setBackground(Color.getColor("input_Pane"));
//input_Pane.setBorder(null);
GridBagConstraints s= new GridBagConstraints();//定义一个GridBagConstraints,是用来控制添加进的组件的显示位置
s.gridx=0;
s.gridy=1;
s.anchor= GridBagConstraints.SOUTH;
s.insets = new Insets(5,10,5,10);
//输入输出框--start
layout.setConstraints(input_Pane, s);
/*
s.gridx=1;
s.gridy=1;
s.anchor= GridBagConstraints.CENTER;
layout.setConstraints(getin, s);
*/
s.gridx=2;
s.gridy=1;
s.anchor= GridBagConstraints.NORTHEAST;
layout.setConstraints(output_Pane, s);
s.gridx=0;
s.gridy=0;
s.anchor= GridBagConstraints.WEST;
layout.setConstraints(wenben, s);
s.gridx=2;
s.gridy=0;
s.anchor= GridBagConstraints.WEST;
layout.setConstraints(shuchu, s);
s.gridx=0;
s.gridy=2;
s.anchor= GridBagConstraints.NORTHEAST;
s.gridwidth=3;
//s.fill = GridBagConstraints.BOTH;
layout.setConstraints(table_Pane, s);
//输入输出框--end
mainFrame.setSize(960,630);//设置大小
mainFrame.setResizable(false);//设置大小不可变
mainFrame.setLocation(40, 40); //设置出现在屏幕的位置
mainFrame.setVisible(true);
myEvent();
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void getArea(){
String s=input.getText();
String m=s.trim();
output.setText(m);
}
//如果缓冲区s该要读取新的内容时,进行判断
//如果缓冲区内容
public void chache_read(int length, String []s, int ch_index){
if(ch_index%CHACHE_LENGTH==0)
{
int m=ch_index/CHACHE_LENGTH;
if((m+2)*CHACHE_LENGTH<length)
s[(m+1)%2]=input.getText().substring((m+1)*CHACHE_LENGTH,(m+2)*CHACHE_LENGTH).toLowerCase();
else if((m+2)*CHACHE_LENGTH>=length&&(m+1)*CHACHE_LENGTH<length)
{
s[(m+1)%2]=input.getText().substring((m+1)*CHACHE_LENGTH,length).toLowerCase();
}
}
}
private void myEvent(){
//为保存菜单条目监听以及事件处理
//当文件是新创建的,弹出对话框,否则直接保存
//而另存为则一定弹出对话框
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//判断文件是否已经存在
if(file==null){
saveDia.setVisible(true);
//获取文件的路径和名称
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
//取消时,做健壮性判断,防止空指针异常
if(dirPath==null || fileName==null)
return ;
file = new File(dirPath,fileName);
}
try{
//创建一个写入缓冲流
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
//获取文本区中的内容
String text = input.getText();
bufw.write(text);
//bufw.flush();
bufw.close();
}
catch (IOException ex){
throw new RuntimeException();
}
}
});
//为“打开”菜单条目添加监听器和处理事件
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//显示对话框
errorModel.setRowCount(0);
openDia.setVisible(true);
//获取文件路径和名称
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
// System.out.println(dirPath+"..."+fileName);
//点击“取消”时,做健壮性判断,防止空指针异常
if(dirPath==null || fileName==null)
return ;
//清空上次打开的内容
input.setText("");
//新建一个文件对象
file = new File(dirPath,fileName);
//将文本区中的内容写到创建的文件中
try{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
input.setForeground(Color.BLACK);
while((line=bufr.readLine())!=null){
input.append(line+"\n");
}
bufr.close();
}
catch (IOException ex){
//input.append("读取失败!!!!!!!");
//input.setForeground(Color.RED);
errorModel.setRowCount(0);
Object[] e1={"读取文件","读取文件失败",0};
errorModel.addRow(e1);
//throw new RuntimeException("读取失败");
}
}
});
/*
//为关闭条目添加监听器及处理
closeItem.addActionListener(new ActionListener(){
//用于关闭窗体
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
*/
//窗体监听及处理
mainFrame.addWindowListener(new WindowAdapter(){
//关闭窗体
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void chache_read_init(int length, String []s){
if(CHACHE_LENGTH>=length)
s[0]=input.getText().substring(0,length).toLowerCase();
else
{
s[0]=input.getText().substring(0,CHACHE_LENGTH).toLowerCase();
if(length<=2*CHACHE_LENGTH){
s[1] =input.getText().substring(CHACHE_LENGTH,length).toLowerCase();
}
else
s[1] =input.getText().substring(CHACHE_LENGTH,2*CHACHE_LENGTH).toLowerCase();
}
}
public class END{ //标记是否已经全部把文件读入到缓冲区
boolean value;
}
public class INT{ //一个缓冲区读到了哪里的标记
int value;
}
class EVA{
String str;
String value;//值或者变量名
String type;//0--int 1--real 2-string 3_number_T1-array
int width;
//String addr;==value
String offset;
ArrayList<Integer> truelist;
ArrayList<Integer> falselist;
}
class FOUR{
String op;
String arg1;
String arg2;
String result;
}
class str_value{
String str;
String value;
}
public char getch(char ch, String []s, int ch_index){
int m=ch_index/CHACHE_LENGTH;
ch=s[m%2].charAt(ch_index%CHACHE_LENGTH);
//System.out.println(ch);
return ch;
}
//ci为缓冲区读到哪里的标记;cf为读得哪个缓冲区的标记;end为是否已经全部把文件读入到缓冲区的标记
public ArrayList<str_value> token_scan(HashMap<String,Integer> hash,DefaultTableModel tableModel,DefaultTableModel err){
INIT(hash); //初始化哈希表
//将结果和错误栏清空
code_run.setText("");
tableModel.setRowCount(0);
err.setRowCount(0);
int length=input.getText().length();
String []s=new String[2];
//刚开始读取的时候
chache_read_init(length, s);
ArrayList<str_value> str_w=new ArrayList<str_value>();//作为语法分析器的输入字符串数组
int line=1;//标记行数
char ch=0;//读到的字符
int ch_index=0; //读到的位置
boolean flag=true;//标记回退现象
int xiaokuohao=0,zhongkuohao=0;//)和]的检查
while(ch_index<length||flag==false)
{
String token="";
if(flag){
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
}
flag=true;
while((ch==' ' || ch=='\n')&&ch_index<length) //遇到空字符
{
if(ch=='\n') {line=line+1;}
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);chache_read(length, s, ch_index);
}
//System.out.println("119行:"+line);
if(Character.isLetter(ch))
{
token+=ch;
if(ch_index<length){
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
while((Character.isLetter(ch) || Character.isDigit(ch))&&ch_index<length){
token+=ch;
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
}
if(ch_index==length&&(Character.isLetter(ch) || Character.isDigit(ch)))
{
token+=ch;
if(hash.containsKey(token))
{
//out+="("+token.toUpperCase()+",)"+"\n";
Object[] o={"("+token.toUpperCase()+",)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str=token.toUpperCase();
ao.value="";
str_w.add(ao);
}
else
{
//需要放入符号表!!!!!!
//out+="(ID,"+token+")"+"\n";
Object[] o={"(ID,"+token+")",line};
str_value ao = new str_value();
ao.str="ID";
ao.value=token;
str_w.add(ao);
tableModel.addRow(o);
}
break;
}
}
if(hash.containsKey(token))
{
//out+="("+token.toUpperCase()+",)"+"\n";
Object[] o={"("+token.toUpperCase()+",)",line};
str_value ao = new str_value();
ao.str=token.toUpperCase();
ao.value="";
str_w.add(ao);
tableModel.addRow(o);
}
else
{
//需要放入符号表!!!!!!
Object[] o={"(ID,"+token+")",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="ID";
ao.value=token;
str_w.add(ao);
//out+="(ID,"+token+")"+"\n";
}
}
if(Character.isDigit(ch)){
token+=ch;
if(ch_index<length){
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
while(Character.isDigit(ch)&&ch_index<length){
token+=ch;
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
}
if(ch_index==length&&Character.isDigit(ch)){
token+=ch;
//out+="(INT,"+token+")"+"\n";
Object[] o={"(INT,"+token+")",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="INT";
ao.value=token;
str_w.add(ao);
break;
}
if(!(Character.isDigit(ch)||ch=='.')) flag=false;
}
if(ch=='.')
{
token+=ch;
if(ch_index<length){
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
while(Character.isDigit(ch)&&ch_index<length){
token+=ch;
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
}}
if(ch_index==length&&Character.isDigit(ch)){
token+=ch;
//out+="(REAL,"+token+")"+"\n";
Object[] o={"(REALL,"+token+")",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="REALL";
ao.value=token;
str_w.add(ao);
break;
}
if(!Character.isDigit(ch)) flag=false;
//out+="(REAL,"+token+")"+"\n";
Object[] o={"(REALL,"+token+")",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="REALL";
ao.value=token;
str_w.add(ao);
}
else
{
//out+="(INT,"+token+")"+"\n";
Object[] o={"(INT,"+token+")",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="INT";
ao.value=token;
str_w.add(ao);
}
//flag=false;
}
else{
switch(ch){
case '*':
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
if(ch=='*'){
token+=ch;
//out+="(EXP,)"+"\n";
Object[] o={"(EXP,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="**";
ao.value="";
str_w.add(ao);
}
else{
flag=false;
//out+="(MULTI,)"+"\n";
Object[] o={"(MULTI,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="*";
ao.value="";
str_w.add(ao);
}
break;
case ':':
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
if(ch=='='){
token+=ch;
//out+="(ASSIGN,)"+"\n";
Object[] o={"(ASSIGN,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str=":=";
ao.value="";
str_w.add(ao);
}
else{
flag=false;
Object[] o={"(COLON,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str=":";
ao.value="";
str_w.add(ao);
//out+="(COLON,)"+"\n";
}
break;
case '<':
if(ch_index<length)
{
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
if(ch=='='){
token+=ch;
//out+="(LE,)"+"\n";
Object[] o={"(LE,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="<=";
ao.value="";
str_w.add(ao);
}
else if(ch=='>'){
token+=ch;
//out+="(NE,)"+"\n";
Object[] o={"(NE,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="<>";
ao.value="";
str_w.add(ao);
//w.add("<>");
}
else{
flag=false;
//out+="(LT,)"+"\n";
Object[] o={"(LT,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="<";
ao.value="";
str_w.add(ao);
//w.add("<");
}
}
else
{
Object[] o={"(LT,)",line};
tableModel.addRow(o);
//w.add("<");
str_value ao = new str_value();
ao.str="<";
ao.value="";
str_w.add(ao);
}
break;
case '=':
//out+="(EQ,)"+"\n";
Object[] o={"(EQ,)",line};
tableModel.addRow(o);
str_value ao = new str_value();
ao.str="=";
ao.value="";
str_w.add(ao);
//w.add("=");
break;
case '>':
if(ch_index<length)
{
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
if(ch=='='){
token+=ch;
Object[] o1={"(GE,)",line};
tableModel.addRow(o1);
str_value aoa = new str_value();
aoa.str=">=";
aoa.value="";
str_w.add(aoa);
//w.add(">=");
//out+="(GE,)"+"\n";
//out+="(RANGE,)"+"\n";
}
else{
flag=false;
Object[] o1={"(GT,)",line};
tableModel.addRow(o1);
str_value aob = new str_value();
aob.str=">";
aob.value="";
str_w.add(aob);
//w.add(">");
//out+="(GT,)"+"\n";
}
}
else
{
Object[] o1={"(GT,)",line};
tableModel.addRow(o1);
str_value aoc = new str_value();
aoc.str=">";
aoc.value="";
str_w.add(aoc);
//w.add(">");
}
break;
case '+':
//out+="(PLUS,)"+"\n";
Object[] o1={"(PLUS,)",line};
tableModel.addRow(o1);
str_value aoe = new str_value();
aoe.str="+";
aoe.value="";
str_w.add(aoe);
break;
case '-':
//out+="(MINUS,)"+"\n";
Object[] o2={"(MINUS,)",line};
tableModel.addRow(o2);
str_value aof = new str_value();
aof.str="-";
aof.value="";
str_w.add(aof);
//w.add("-");
break;
case '/':
//out+="(RDIV,)"+"\n";
Object[] o3={"(RDIV,)",line};
tableModel.addRow(o3);
str_value aog = new str_value();
aog.str="/";
aog.value="";
str_w.add(aog);
//w.add("/");
break;
case ',':
//out+="(COMMA,)"+"\n";
Object[] o4={"(COMMA,)",line};
tableModel.addRow(o4);
str_value aoh = new str_value();
aoh.str=",";
aoh.value="";
str_w.add(aoh);
//w.add(",");
break;
case ';':
//out+="(SEMIC,)"+"\n";
Object[] o5={"(SEMIC,)",line};
tableModel.addRow(o5);
str_value aoi = new str_value();
aoi.str=";";
aoi.value="";
str_w.add(aoi);
//w.add(";");
break;
case '(':
if(ch_index<length)
ch=getch(ch,s,ch_index);
//ch_index++;chache_read(length, s, ch_index);
if(ch=='*'){
if(ch_index+1<length&&ch_index+2<length)
{
char ch1=getch(ch,s,ch_index+1);
char ch2=getch(ch,s,ch_index+2);
while((ch_index<length-2)&&!(ch1=='*'&&ch2==')')){
ch1=getch(ch,s,ch_index+1);
ch2=getch(ch,s,ch_index+2);
ch_index++;chache_read(length, s, ch_index);
if(ch_index<length){
if(getch(ch,s,ch_index)=='\n') line++;}
}
ch_index++;chache_read(length, s, ch_index);
ch_index++;chache_read(length, s, ch_index);
ch_index++;chache_read(length, s, ch_index);
//System.out.println(ch_index);
}
}
else{
if(LR_BRAC(ch_index,s,length,line)==true)
{
//out+="(LR_BRAC,)"+"\n";
xiaokuohao++;
Object[] o6={"(LR_BRAC,)",line};
tableModel.addRow(o6);
//w.add("(");
str_value aoj = new str_value();
aoj.str="(";
aoj.value="";
str_w.add(aoj);
}
else{
//error+="封闭性错误--'(',出现在"+line+"行\n";
Object[] e={"词法分析","'('封闭性错误",line};
err.addRow(e);
}
}
break;
case ')':
if(xiaokuohao>0)
{
xiaokuohao--;
Object[] o6={"(RR_BRAC,)",line};
tableModel.addRow(o6);
str_value aoj = new str_value();
aoj.str=")";
aoj.value="";
str_w.add(aoj);
}
else
{
Object[] e={"词法分析","')'封闭性错误",line};
err.addRow(e);
}
break;
case '、':
Object[] o7={"(P_MARK,)",line};
tableModel.addRow(o7);
str_value aoj = new str_value();
aoj.str="、";
aoj.value="";
str_w.add(aoj);
//out+="(P_MARK,)"+"\n";
break;
case '.':
if(ch_index<length)
{
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
if(ch=='.'){
token+=ch;
Object[] o6={"(RANGE,)",line};
tableModel.addRow(o6);
str_value aok = new str_value();
aok.str="..";
aok.value="";
str_w.add(aok);
//out+="(RANGE,)"+"\n";
}
else{
flag=false;
Object[] o6={"(F_STOP,)",line};
tableModel.addRow(o6);
str_value aok = new str_value();
aok.str=".";
aok.value="";
str_w.add(aok);
//out+="(F_STOP,)"+"\n";
}
}
else
{
Object[] o6={"(F_STOP,)",line};
tableModel.addRow(o6);
str_value aok = new str_value();
aok.str=".";
aok.value="";
str_w.add(aok);
}
break;
case '^':
//out+="(CAP,)"+"\n";
Object[] o6={"(CAP,)",line};
tableModel.addRow(o6);
str_value aok = new str_value();
aok.str="^";
aok.value="";
str_w.add(aok);
break;
case '[':
if(LS_BRAC(ch_index,s,length,line)==true)
{
//out+="(LS_BRAC,)"+"\n";
zhongkuohao++;
Object[] o8={"(LS_BRAC,)",line};
str_value aok1 = new str_value();
aok1.str="[";
aok1.value="";
str_w.add(aok1);
tableModel.addRow(o8);
}
else{
Object[] e11={"词法分析","'['封闭性错误",line};
err.addRow(e11);
}
break;
case ']':
if(zhongkuohao>0)
{
zhongkuohao--;
Object[] o8={"(RS_BRAC,)",line};
str_value aok2 = new str_value();
aok2.str="]";
aok2.value="";
str_w.add(aok2);
tableModel.addRow(o8);
}
else
{
Object[] e1={"词法分析","']'封闭性错误",line};
err.addRow(e1);
}
break;
case '\'':
if(Q_MARK(ch_index,s,length,line)==true)
{
//out+="(RS_BRAC,)"+"\n";
Object[] o8={"(Q_MARK,)",line};
tableModel.addRow(o8);
str_value aokk = new str_value();
aokk.str="'";
aokk.value="";
str_w.add(aokk);
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
while(ch!='\'')
{
token+=ch;
ch=getch(ch,s,ch_index);
ch_index++;chache_read(length, s, ch_index);
}
//System.out.println(flag);
Object[] o11={"("+"STRING"+","+token+")",line};
tableModel.addRow(o11);
//tableModel.addRow(o8);
//w.add("STRING");
str_value aokl = new str_value();
aokl.str="STRING";
aokl.value=token;
str_w.add(aokl);
//out+="(RS_BRAC,)"+"\n";
//System.out.println(ch_index);
}
else{
Object[] e4={"词法分析","'引号'封闭性错误",line};
err.addRow(e4);
}
break;
case ' ':
break;
case '\n':