├── .classpath
├── .gitattributes
├── .gitignore
├── .project
├── README.md
├── bin
├── analyize_error
│ └── Error.class
└── parse
│ ├── Exp.class
│ ├── Exp_B.class
│ ├── Exp_Operate.class
│ ├── Exp_calcul.class
│ ├── Exp_comp.class
│ ├── Exp_cond.class
│ ├── Exp_func.class
│ ├── Exp_funcname.class
│ ├── Exp_ife.class
│ ├── Exp_mark.class
│ ├── Exp_type.class
│ ├── Exp_whi.class
│ ├── HaveToken.class
│ ├── Main.class
│ ├── Parse_Semantics.class
│ ├── Parse_Token.class
│ ├── Symbol_Table.class
│ ├── Table.class
│ ├── Token_List.class
│ ├── Variable_Table.class
│ ├── type_cal.class
│ ├── type_com.class
│ ├── type_op.class
│ └── type_ret.class
├── src
├── analyize_error
│ └── Error.java
└── parse
│ ├── Exps.java
│ ├── HaveToken.java
│ ├── Main.java
│ ├── Parse_Semantics.java
│ ├── Parse_Token.java
│ ├── Symbol_Table.java
│ ├── Token_List.java
│ └── Variable_Table.java
├── test.t
└── token.py
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # Windows shortcuts
18 | *.lnk
19 |
20 | # =========================
21 | # Operating System Files
22 | # =========================
23 |
24 | # OSX
25 | # =========================
26 |
27 | .DS_Store
28 | .AppleDouble
29 | .LSOverride
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear on external disk
35 | .Spotlight-V100
36 | .Trashes
37 |
38 | # Directories potentially created on remote AFP share
39 | .AppleDB
40 | .AppleDesktop
41 | Network Trash Folder
42 | Temporary Items
43 | .apdisk
44 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | compile
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # compiler_java_python
2 | 简易编译器实现,最终翻译成伪汇编代码
3 |
4 | 项目太老,原先在eclipse上实现的,现在已经迁移到AndroidStudio,把分支切换到dev_android打开即可
5 |
6 | ## 编译器主要编译t语言(暂且这么称呼)
7 |
8 |
9 | ## t语言关键字:
10 | - func 用于定义函数
11 | - main 主函数名,文件中必须包含主函数
12 | - while 循环关键字
13 | - if 条件分支 关键字
14 | - else 必须与if 一起才能识别
15 | - num 数字类型
16 | - string 字符串类型
17 | - bool 布尔值类型
18 |
19 | ## t语言的比较符号:
20 | - >
21 | - <
22 | - >=
23 | - <=
24 | - ==
25 |
26 | 示例:
27 |
28 |
29 | func main(){
30 |
31 | num a,b;
32 |
33 | a=1;
34 |
35 | b=0;
36 |
37 | while(b<5){
38 |
39 | num c,k;
40 |
41 | b=b+1;
42 |
43 | if(b<3){
44 |
45 | c=b+b*a;
46 |
47 | }else{
48 |
49 | c=b;
50 |
51 | }
52 | }
53 |
54 | num d,c;
55 |
56 | c=1;
57 |
58 | a=b\*a\*c+b*4;
59 |
60 | }
61 |
62 | 输出伪汇编代码(三地址码的四元式)格式为:<操作符,操作数1,操作数2,结果存放>
63 | 以上示例的输出结果:
64 | - DEFINE , num , null , a
65 | - DEFINE , num , null , b
66 | - SEND , 1 , null , a
67 | - SEND , 0 , null , b
68 | - LABEL , null , null , Label1
69 | - CJMP_L , b , 5 , Label2
70 | - DEFINE , num , null , c
71 | - DEFINE , num , null , k
72 | - ADD , b , 1 , $
73 | - SEND , $ , null , b
74 | - CJMP_L , b , 3 , Label3
75 | - MUL , b , a , $
76 | - ADD , b , $ , $
77 | - SEND , $ , null , c
78 | - LABEL , null , null , Label3
79 | - SEND , b , null , c
80 | - JMP , null , null , Label1
81 | - LABEL , null , null , Label2
82 | - DEFINE , num , null , d
83 | - DEFINE , num , null , c
84 | - SEND , 1 , null , c
85 | - MUL , b , a , $
86 | - MUL , $ , c , $
87 | - MUL , b , 4 , $
88 | - ADD , $ , $ , $
89 | - SEND , $ , null , a
90 |
91 |
92 |
93 |
94 | ##个别操作符的含义:
95 | - DEFINE: 定义变量
96 | - SEND : 赋值
97 | - JMP: 无条件跳转
98 | - CJMP_E: 等于时执行下面的语句,否则跳转到某个label
99 | - CJMP_B : 大于时........
100 | - CJMP_B_E : 大于等于的时候......
101 | - LABEL: 定义一个标签(用于跳转)
102 | - $ : 用于存放左边操作数运算的结果,或者代表上面的运算结果,主要作用是把被分解的表达式连接起来
103 | - null 表示该操作数不进行操作,仅仅起占位的作用
104 |
105 |
106 |
107 | ## 补充说明:
108 | - 为了简化编译工作
109 | - t语言只支持一个mian(),函数,所以不分析函数之间的跳转
110 | - t语言只支持变量之间的加减乘除,自动识别运算优先级,不允许使用()
111 | - t语言以只支持while这一种循环,而且不支持break跳出循环。
112 |
113 | 由于第一次编写编译器,所以很多工作可能不是很好,但是对于理解编译原理,还是有很大帮助的,有意向的同学欢迎交流....
114 |
--------------------------------------------------------------------------------
/bin/analyize_error/Error.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/analyize_error/Error.class
--------------------------------------------------------------------------------
/bin/parse/Exp.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp.class
--------------------------------------------------------------------------------
/bin/parse/Exp_B.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_B.class
--------------------------------------------------------------------------------
/bin/parse/Exp_Operate.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_Operate.class
--------------------------------------------------------------------------------
/bin/parse/Exp_calcul.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_calcul.class
--------------------------------------------------------------------------------
/bin/parse/Exp_comp.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_comp.class
--------------------------------------------------------------------------------
/bin/parse/Exp_cond.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_cond.class
--------------------------------------------------------------------------------
/bin/parse/Exp_func.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_func.class
--------------------------------------------------------------------------------
/bin/parse/Exp_funcname.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_funcname.class
--------------------------------------------------------------------------------
/bin/parse/Exp_ife.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_ife.class
--------------------------------------------------------------------------------
/bin/parse/Exp_mark.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_mark.class
--------------------------------------------------------------------------------
/bin/parse/Exp_type.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_type.class
--------------------------------------------------------------------------------
/bin/parse/Exp_whi.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Exp_whi.class
--------------------------------------------------------------------------------
/bin/parse/HaveToken.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/HaveToken.class
--------------------------------------------------------------------------------
/bin/parse/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Main.class
--------------------------------------------------------------------------------
/bin/parse/Parse_Semantics.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Parse_Semantics.class
--------------------------------------------------------------------------------
/bin/parse/Parse_Token.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Parse_Token.class
--------------------------------------------------------------------------------
/bin/parse/Symbol_Table.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Symbol_Table.class
--------------------------------------------------------------------------------
/bin/parse/Table.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Table.class
--------------------------------------------------------------------------------
/bin/parse/Token_List.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Token_List.class
--------------------------------------------------------------------------------
/bin/parse/Variable_Table.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/Variable_Table.class
--------------------------------------------------------------------------------
/bin/parse/type_cal.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/type_cal.class
--------------------------------------------------------------------------------
/bin/parse/type_com.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/type_com.class
--------------------------------------------------------------------------------
/bin/parse/type_op.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/type_op.class
--------------------------------------------------------------------------------
/bin/parse/type_ret.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/bin/parse/type_ret.class
--------------------------------------------------------------------------------
/src/analyize_error/Error.java:
--------------------------------------------------------------------------------
1 | package analyize_error;
2 |
3 | public class Error {
4 | public Error(){
5 |
6 | }
7 | public void error_print(int id,String content,String line,String row){
8 | String error_content=null;
9 | switch(id){
10 | case 0:error_content="expression was in wrong place. ";break;
11 | case 1:error_content="expression was not wrote rightly. ";break;
12 | case 2:error_content="misss "+content+" .";break;
13 | case 3:error_content="variable "+content+" was not defined. ";break;
14 | case 4:error_content="the type was wrong (type only num,string,bool).";break;
15 | case 5: error_content="the variable"+content+" can't be used. ";break;
16 | case 6:error_content="the mark "+content+" was missing. ";break;
17 | case 7 :error_content="the key word "+content+" was wrong.";break;
18 | case 8: error_content="the type "+content+" can not match.";break;
19 | case 9 :error_content="the type "+content+" can not be calculatered. ";break;
20 | default:error_content="something wrong--";break;
21 | }
22 | print(error_content,line,row);
23 |
24 | }
25 | public void print(String content,String line,String row){
26 | System.out.println(content+"in line:"+line+" row:"+row);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/parse/Exps.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/Exps.java
--------------------------------------------------------------------------------
/src/parse/HaveToken.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/HaveToken.java
--------------------------------------------------------------------------------
/src/parse/Main.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/Main.java
--------------------------------------------------------------------------------
/src/parse/Parse_Semantics.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/Parse_Semantics.java
--------------------------------------------------------------------------------
/src/parse/Parse_Token.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/Parse_Token.java
--------------------------------------------------------------------------------
/src/parse/Symbol_Table.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/Symbol_Table.java
--------------------------------------------------------------------------------
/src/parse/Token_List.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ladingwu/compiler_java_python/268442050d7080cf7af74107982a67fb7ab8b7fd/src/parse/Token_List.java
--------------------------------------------------------------------------------
/src/parse/Variable_Table.java:
--------------------------------------------------------------------------------
1 | package parse;
2 |
3 | public class Variable_Table {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/test.t:
--------------------------------------------------------------------------------
1 | func main(){
2 | num a,b;
3 | a=1;
4 | b=0;
5 | while(b<5){
6 | num c,k;
7 | b=b+1;
8 | if(b<3){
9 | c=b+b*a;
10 | }else{
11 | c=b;
12 | }
13 | }
14 | num d,c;
15 | c=1;
16 | a=b*a*c+b*4;
17 |
18 | }
--------------------------------------------------------------------------------
/token.py:
--------------------------------------------------------------------------------
1 | import os,sys
2 | msg_table=['func','main','if','while',
3 | 'else','num','string','bool','(',')',
4 | ';',',','{','}','[',']','=','+','-','*'
5 | ,'/','%','&','|','>','<','==','>=','<=','"','"','id']
6 | def read_token(file):
7 | with open(file,'r')as f:
8 | return f.readlines()
9 |
10 | def token_recogn(lines):
11 | msg_line=0
12 | #k='qwertyuioplkjhgfdsazxvcbnm_'
13 | #s=k.upper()
14 | q1='>='
15 | q2='<='
16 | q3='=='
17 | m=',;+=*/-(){}""><'
18 | wrong_mark=';,。;“”【】'
19 | for line in lines:
20 | words=''
21 | msg_line+=1
22 | msg_row=0
23 | str_i=0
24 | msg=''
25 | for word in line:
26 | if word ==' ' or word =='\t' or word=='\n':
27 | if(word=='\t'):
28 | msg_row+=4
29 | if words != '':
30 | deal_words(words, msg_line, msg_row)
31 | words=''
32 | elif word in m :
33 | if(msg_row+10:
37 |
38 | deal_words(words,msg_line,msg_row-1)
39 | deal_words(msg+word, msg_line,msg_row)
40 | msg=''
41 | else:
42 | deal_words(msg+word,msg_line,msg_row)
43 | msg=''
44 | words=''
45 | elif word in wrong_mark:
46 | if len(words)>0:
47 |
48 | deal_words(words,msg_line,msg_row-1)
49 | deal_words(word, msg_line,msg_row)
50 |
51 | else:
52 | deal_words(word,msg_line,msg_row)
53 | else:
54 | words=words+word
55 | msg_row+=1
56 | def check_id(s):
57 | m='qwertyuiopasdfghjklzxcvbnm_'
58 | n=m.upper()
59 | k='0987654321'
60 | flag=True
61 | #print(n)
62 | j=0
63 | for i in s:
64 | if j==0:
65 | j+=1
66 | if i in k:
67 | flag=False
68 | break
69 |
70 | if i not in m and i not in n and i not in k:
71 |
72 | flag=False
73 | break
74 | return flag
75 | def check_num(s):
76 | k="0987654321"
77 | flag=True
78 | for i in s:
79 | if i not in k:
80 | flag=False
81 | break
82 | return flag
83 | last_word='' #为了读取字符串
84 | count=0
85 | def deal_words(words,msg_line,msg_row):
86 | i=0
87 | # print(words)
88 | global msg_table,last_word,count
89 | length=len(msg_table)
90 | #print('-----------------------------------',words)
91 | if words=='"':
92 | count+=1
93 | if (count%2)!=0 and last_word=='"':
94 | last_word=''
95 | # 表示字符常量
96 | print('id_string'+':'+words+':'+str(msg_line)+':'+str(msg_row))
97 | return
98 |
99 | for t in msg_table:
100 | if i==length-1:
101 |
102 | if check_id(words):
103 | print('id'+':'+words+':'+str(msg_line)+':'+str(msg_row))
104 | break
105 | elif check_num(words):
106 | # 表示常数变量
107 | print('id_num'+':'+words+':'+str(msg_line)+':'+str(msg_row))
108 | break
109 | else:
110 | error(words,msg_line,msg_row)
111 | break
112 | if words==t:
113 | if i<8:
114 |
115 | print(words+':'+words+':'+str(msg_line)+':'+str(msg_row))
116 | elif i