diff --git a/.classpath b/.classpath deleted file mode 100644 index fb50116..0000000 --- a/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index bdb0cab..0000000 --- a/.gitattributes +++ /dev/null @@ -1,17 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto - -# Custom for Visual Studio -*.cs diff=csharp - -# Standard to msysgit -*.doc diff=astextplain -*.DOC diff=astextplain -*.docx diff=astextplain -*.DOCX diff=astextplain -*.dot diff=astextplain -*.DOT diff=astextplain -*.pdf diff=astextplain -*.PDF diff=astextplain -*.rtf diff=astextplain -*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore index 96374c4..aa724b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,43 +1,15 @@ -# Windows image file caches -Thumbs.db -ehthumbs.db - -# Folder config file -Desktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Windows Installer files -*.cab -*.msi -*.msm -*.msp - -# Windows shortcuts -*.lnk - -# ========================= -# Operating System Files -# ========================= - -# OSX -# ========================= - +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml .DS_Store -.AppleDouble -.LSOverride - -# Thumbnails -._* - -# Files that might appear on external disk -.Spotlight-V100 -.Trashes - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..23b2655 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +CompileApplication \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..8d9bd21 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..01b9090 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..a1d4c72 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..0380d8d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..892046b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.project b/.project deleted file mode 100644 index 382ddfc..0000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - compile - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/README.md b/README.md index 2ab5d6d..28ec6a8 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ # compiler_java_python 简易编译器实现,最终翻译成伪汇编代码 -##编译器主要编译t语言(暂且这么称呼) -##t语言关键字: +项目太老,原先在eclipse上实现的,现在已经前移到AndroidStudio,把分支切换到dev_android打开即可 + +## 编译器主要编译t语言(暂且这么称呼) + + +## t语言关键字: - func 用于定义函数 - main 主函数名,文件中必须包含主函数 - while 循环关键字 @@ -87,7 +91,7 @@ -##个别操作符的含义: +## 个别操作符的含义: - DEFINE: 定义变量 - SEND : 赋值 - JMP: 无条件跳转 @@ -96,11 +100,14 @@ - CJMP_B_E : 大于等于的时候...... - LABEL: 定义一个标签(用于跳转) - $ : 用于存放左边操作数运算的结果,或者代表上面的运算结果,主要作用是把被分解的表达式连接起来 +- null 表示该操作数不进行操作,仅仅起占位的作用 ## 补充说明: -1. 为了简化编译工作,t语言只支持一个mian(),函数,所以不分析函数之间的跳转 -2. 为了简化编译工作,t语言只支持变量之间的加减乘除,自动识别运算优先级,不允许使用() -3. 为了简化编译工作,t语言以只支持while这一种循环,而且不支持break跳出循环。 -4. 由于第一次编写编译器,所以很多工作可能不是很好,但是对于理解编译原理,还是有很大帮助的,有意向的同学欢迎交流.... +- 为了简化编译工作 + - t语言只支持一个mian(),函数,所以不分析函数之间的跳转 + - t语言只支持变量之间的加减乘除,自动识别运算优先级,不允许使用() + - t语言以只支持while这一种循环,而且不支持break跳出循环。 + +由于第一次编写编译器,所以很多工作可能不是很好,但是对于理解编译原理,还是有很大帮助的,有意向的同学欢迎交流.... diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..27437b1 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,38 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdkVersion 30 + + defaultConfig { + applicationId "com.example.compileapplication" + minSdkVersion 21 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.2.1' + implementation 'androidx.constraintlayout:constraintlayout:2.0.1' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/compileapplication/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/compileapplication/ExampleInstrumentedTest.java new file mode 100644 index 0000000..c487adb --- /dev/null +++ b/app/src/androidTest/java/com/example/compileapplication/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.compileapplication; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.example.compileapplication", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..6fdb21b --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/compileapplication/MainActivity.java b/app/src/main/java/com/example/compileapplication/MainActivity.java new file mode 100644 index 0000000..0931fc1 --- /dev/null +++ b/app/src/main/java/com/example/compileapplication/MainActivity.java @@ -0,0 +1,14 @@ +package com.example.compileapplication; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..4fc2444 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..a571e60 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..61da551 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c41dd28 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..db5080a Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..6dba46d Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..da31a87 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..15ac681 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..b216f2d Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..f25a419 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..e96783c Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml new file mode 100644 index 0000000..1972d40 --- /dev/null +++ b/app/src/main/res/values-night/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..39ac144 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + CompileApplication + \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..36743db --- /dev/null +++ b/app/src/main/res/values/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/app/src/test/java/com/example/compileapplication/ExampleUnitTest.java b/app/src/test/java/com/example/compileapplication/ExampleUnitTest.java new file mode 100644 index 0000000..688a3d7 --- /dev/null +++ b/app/src/test/java/com/example/compileapplication/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.example.compileapplication; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/bin/analyize_error/Error.class b/bin/analyize_error/Error.class deleted file mode 100644 index 91bd2ce..0000000 Binary files a/bin/analyize_error/Error.class and /dev/null differ diff --git a/bin/parse/Exp.class b/bin/parse/Exp.class deleted file mode 100644 index d2f6e3e..0000000 Binary files a/bin/parse/Exp.class and /dev/null differ diff --git a/bin/parse/Exp_B.class b/bin/parse/Exp_B.class deleted file mode 100644 index a8aa958..0000000 Binary files a/bin/parse/Exp_B.class and /dev/null differ diff --git a/bin/parse/Exp_Operate.class b/bin/parse/Exp_Operate.class deleted file mode 100644 index b052a46..0000000 Binary files a/bin/parse/Exp_Operate.class and /dev/null differ diff --git a/bin/parse/Exp_calcul.class b/bin/parse/Exp_calcul.class deleted file mode 100644 index c58d3ce..0000000 Binary files a/bin/parse/Exp_calcul.class and /dev/null differ diff --git a/bin/parse/Exp_comp.class b/bin/parse/Exp_comp.class deleted file mode 100644 index 8909cf5..0000000 Binary files a/bin/parse/Exp_comp.class and /dev/null differ diff --git a/bin/parse/Exp_cond.class b/bin/parse/Exp_cond.class deleted file mode 100644 index 62c1e46..0000000 Binary files a/bin/parse/Exp_cond.class and /dev/null differ diff --git a/bin/parse/Exp_func.class b/bin/parse/Exp_func.class deleted file mode 100644 index 4256fbb..0000000 Binary files a/bin/parse/Exp_func.class and /dev/null differ diff --git a/bin/parse/Exp_funcname.class b/bin/parse/Exp_funcname.class deleted file mode 100644 index 79836df..0000000 Binary files a/bin/parse/Exp_funcname.class and /dev/null differ diff --git a/bin/parse/Exp_ife.class b/bin/parse/Exp_ife.class deleted file mode 100644 index 9c743ac..0000000 Binary files a/bin/parse/Exp_ife.class and /dev/null differ diff --git a/bin/parse/Exp_mark.class b/bin/parse/Exp_mark.class deleted file mode 100644 index d7c5f54..0000000 Binary files a/bin/parse/Exp_mark.class and /dev/null differ diff --git a/bin/parse/Exp_type.class b/bin/parse/Exp_type.class deleted file mode 100644 index 9267019..0000000 Binary files a/bin/parse/Exp_type.class and /dev/null differ diff --git a/bin/parse/Exp_whi.class b/bin/parse/Exp_whi.class deleted file mode 100644 index a78431a..0000000 Binary files a/bin/parse/Exp_whi.class and /dev/null differ diff --git a/bin/parse/HaveToken.class b/bin/parse/HaveToken.class deleted file mode 100644 index 7d90481..0000000 Binary files a/bin/parse/HaveToken.class and /dev/null differ diff --git a/bin/parse/Main.class b/bin/parse/Main.class deleted file mode 100644 index 13eb9a2..0000000 Binary files a/bin/parse/Main.class and /dev/null differ diff --git a/bin/parse/Parse_Semantics.class b/bin/parse/Parse_Semantics.class deleted file mode 100644 index ce27c0c..0000000 Binary files a/bin/parse/Parse_Semantics.class and /dev/null differ diff --git a/bin/parse/Parse_Token.class b/bin/parse/Parse_Token.class deleted file mode 100644 index ea7ad0a..0000000 Binary files a/bin/parse/Parse_Token.class and /dev/null differ diff --git a/bin/parse/Symbol_Table.class b/bin/parse/Symbol_Table.class deleted file mode 100644 index aafb9fa..0000000 Binary files a/bin/parse/Symbol_Table.class and /dev/null differ diff --git a/bin/parse/Table.class b/bin/parse/Table.class deleted file mode 100644 index ce960b1..0000000 Binary files a/bin/parse/Table.class and /dev/null differ diff --git a/bin/parse/Token_List.class b/bin/parse/Token_List.class deleted file mode 100644 index 27b107c..0000000 Binary files a/bin/parse/Token_List.class and /dev/null differ diff --git a/bin/parse/Variable_Table.class b/bin/parse/Variable_Table.class deleted file mode 100644 index 6304930..0000000 Binary files a/bin/parse/Variable_Table.class and /dev/null differ diff --git a/bin/parse/type_cal.class b/bin/parse/type_cal.class deleted file mode 100644 index 1963604..0000000 Binary files a/bin/parse/type_cal.class and /dev/null differ diff --git a/bin/parse/type_com.class b/bin/parse/type_com.class deleted file mode 100644 index 7244d3e..0000000 Binary files a/bin/parse/type_com.class and /dev/null differ diff --git a/bin/parse/type_op.class b/bin/parse/type_op.class deleted file mode 100644 index 83436fe..0000000 Binary files a/bin/parse/type_op.class and /dev/null differ diff --git a/bin/parse/type_ret.class b/bin/parse/type_ret.class deleted file mode 100644 index cbb1c67..0000000 Binary files a/bin/parse/type_ret.class and /dev/null differ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..dc935b3 --- /dev/null +++ b/build.gradle @@ -0,0 +1,25 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +buildscript { + repositories { + google() + mavenCentral() + } + dependencies { + classpath "com.android.tools.build:gradle:4.2.1" + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + mavenCentral() + + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} \ No newline at end of file diff --git a/compilelib/.gitignore b/compilelib/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/compilelib/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/compilelib/build.gradle b/compilelib/build.gradle new file mode 100644 index 0000000..e493c42 --- /dev/null +++ b/compilelib/build.gradle @@ -0,0 +1,8 @@ +plugins { + id 'java-library' +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_7 + targetCompatibility = JavaVersion.VERSION_1_7 +} \ No newline at end of file diff --git a/src/analyize_error/Error.java b/compilelib/src/main/java/com/example/compilelib/Error.java similarity index 97% rename from src/analyize_error/Error.java rename to compilelib/src/main/java/com/example/compilelib/Error.java index 57180ea..4a2f405 100644 --- a/src/analyize_error/Error.java +++ b/compilelib/src/main/java/com/example/compilelib/Error.java @@ -1,4 +1,4 @@ -package analyize_error; +package com.example.compilelib; public class Error { public Error(){ diff --git a/compilelib/src/main/java/com/example/compilelib/Main.java b/compilelib/src/main/java/com/example/compilelib/Main.java new file mode 100644 index 0000000..8ed0eba --- /dev/null +++ b/compilelib/src/main/java/com/example/compilelib/Main.java @@ -0,0 +1,22 @@ +package com.example.compilelib; + +import com.example.compilelib.parse.TokenReader; +import com.example.compilelib.parse.ParseSemantics; +import com.example.compilelib.parse.ParseToken; +import com.example.compilelib.parse.TokenList; + +public class Main { + + public static void main(String[] args) { + System.out.println("开始分析"); + TokenList tokenList = new TokenReader().getTokenList(); // 通过读取代码文件,获取token列表 + ParseToken pt =new ParseToken(tokenList); + pt.startParse(); // 把token解析成语法树 + if(pt.getExpFun() != null){ + System.out.println("语法树构建完成"); + ParseSemantics ps=new ParseSemantics(pt.getExpFun()); + ps.parseSemantics(); // 解析语法树 + } + } + +} diff --git a/compilelib/src/main/java/com/example/compilelib/parse/Exps.java b/compilelib/src/main/java/com/example/compilelib/parse/Exps.java new file mode 100644 index 0000000..5b19243 --- /dev/null +++ b/compilelib/src/main/java/com/example/compilelib/parse/Exps.java @@ -0,0 +1,107 @@ +package com.example.compilelib.parse; + +import java.awt.SystemColor; +import java.util.ArrayList; +import java.util.List; + +//enum type_id {NUM,STRING,BOOL,NORMAL_NUM,NORMAL_STRING}; +enum type_com {BIG, LITTLE, EQUAL, BIG_EQUAL, LITTLE_EQUAL}; + +enum type_cal { + BIG, LITTLE, EQUAL, BIG_EQUAL, LITTLE_EQUAL, + GOT, ADD, SUB, MUL, DIV, ID, NUM, STRING, BOOL, NORMAL_NUM, + NORMAL_STRING, TRANS_NUM, TRANS_STRING, TRANS_BOOL, FUNC, WHI, IFE +}; + +enum type_ret {NUM, STRING, BOOL}; + +class Exp { + type_cal type; + String line; + String row; +} + +//数值或者变量 +class Exp_mark extends Exp { + String value; + List list_value = null; + + public void setListValue(Exp s) { + if (list_value == null) { + list_value = new ArrayList(); + } + list_value.add(s); + } + + Exp got = null; //判断变量后面是否是一个等号 +} + +class Exp_type extends Exp { + Exp Id = null; //定义的变量 +} + +class Exp_calcul extends Exp { + Exp left; + Exp right; +} + +//条件 +class Exp_cond extends Exp { + Exp left; + Exp_comp exp_comp; + Exp right; +} + +//比较符号 +class Exp_comp extends Exp { + +} + +//函数定义 +class Exp_func extends Exp { + Exp_funcname name; + + List list_exp = null; + + + public void setExp(Exp exp) { + if (list_exp == null) { + list_exp = new ArrayList(); + } + list_exp.add(exp); + } + +} + +class Exp_funcname extends Exp { + String name = null; +} + + +class Exp_whi extends Exp { + + Exp_cond condition; + + Exp next = null; + +} + +class Exp_ife extends Exp { + Exp_cond condition; + + Exp left = null; + Exp right = null; + +} + +class Exp_B extends Exp { + + List list_exp = null; + + public void setList_exp(Exp exp) { + if (list_exp == null) { + list_exp = new ArrayList(); + } + list_exp.add(exp); + } +} \ No newline at end of file diff --git a/src/parse/Parse_Semantics.java b/compilelib/src/main/java/com/example/compilelib/parse/ParseSemantics.java similarity index 75% rename from src/parse/Parse_Semantics.java rename to compilelib/src/main/java/com/example/compilelib/parse/ParseSemantics.java index 8a7e896..6fdbf07 100644 --- a/src/parse/Parse_Semantics.java +++ b/compilelib/src/main/java/com/example/compilelib/parse/ParseSemantics.java @@ -1,34 +1,35 @@ -package parse; +package com.example.compilelib.parse; import java.util.ArrayList; import java.util.List; +import com.example.compilelib.Error; -import analyize_error.Error; - -public class Parse_Semantics { +/** + * + */ +public class ParseSemantics { Exp_func exp_func; - Symbol_Table st=null; + SymbolTable st=null; Error error=null; - int count=0,Lab=0 ,s=1; //count, s ά - List list_op=null; - public Parse_Semantics(Exp_func func){ + int count=0,Lab=0 ,s=1; + List list_op=null; //count错误计数, s 维数 + public ParseSemantics(Exp_func func){ this.exp_func=func; error=new Error(); - list_op=new ArrayList(); - st=new Symbol_Table(); //ű + list_op=new ArrayList(); + st=new SymbolTable(); //构建符号表 + } + public void parseSemantics(){ parse_se(); if(count==0){ - int i=0; - while(ilist=st.check_Dimension(s); //ЩĿΪ0ı -// Ż + Listlist=st.check_Dimension(s); + if(list!=null){ int i=0; while(i list_tab=new ArrayList(); Table tab=new Table(); @@ -164,7 +165,7 @@ public void parse_sema_typedef(Exp_type typedef,int Dimension){ st.put(m.value,t); - Exp_Operate operate=clone_Operate(exp_op); + ExpOperate operate=clone_Operate(exp_op); operate.result=m.value; list_op.add(operate); // System.out.println("define var.."+m.value); @@ -172,15 +173,15 @@ public void parse_sema_typedef(Exp_type typedef,int Dimension){ } } } - public void parse_sema_mark(Exp_mark exp_mark){ - //a=a+b ȺߵıDZdzҪƥͣұߵĿʹƥ + private void parse_sema_mark(Exp_mark exp_mark){ + if(exp_mark.type==type_cal.ID){ if(check_define(exp_mark.value)==type_cal.NUM ){ - Exp_Operate operate=null; - operate=new Exp_Operate(); + ExpOperate operate=null; + operate=new ExpOperate(); if(exp_mark.got!=null){ - Exp_Operate op_send=new Exp_Operate(); + ExpOperate op_send=new ExpOperate(); Exp_calcul c=(Exp_calcul)exp_mark.got; operate.s= @@ -225,18 +226,18 @@ public void parse_sema_mark(Exp_mark exp_mark){ error.error_print(8, exp_mark.type.toString(), exp_mark.line, exp_mark.row); } } - - - public String parse_sema_mark_(Exp_calcul exp_cal){ + + + private String parse_sema_mark_(Exp_calcul exp_cal){ // Exp_calcul exp_c1=(Exp_calcul) exp_cal.left; - Exp_Operate operate=null; + ExpOperate operate=null; Exp_mark mark=(Exp_mark)exp_cal.left; - operate=new Exp_Operate(); + operate=new ExpOperate(); // if(check_define(mark.value)!=type_cal.NUM && mark.type!=type_cal.NORMAL_NUM){ // error.error_print(9, null, mark.line, mark.row); // count++; // } - //ͼ + backString(mark, type_cal.NUM); // System.out.println(mark.value); @@ -256,7 +257,7 @@ public String parse_sema_mark_(Exp_calcul exp_cal){ // count++; // error.error_print(9, m.type.toString(), m.line, m.row); // } -// mĶ塣 + backString(m, type_cal.NUM); if(cal.type==type_cal.MUL){ @@ -277,14 +278,14 @@ public String parse_sema_mark_(Exp_calcul exp_cal){ if(operate.result==null){ s=operate.s; } - return s; //ճ˳ + return s; } - public void exec_exp_B(Exp_B exp_b,String label){ - Exp_Operate operate=null; + private void exec_exp_B(Exp_B exp_b,String label){ + ExpOperate operate=null; if(exp_b.list_exp!=null){ if(label!=null){ - operate=new Exp_Operate(); + operate=new ExpOperate(); operate.op=type_op.LABEL; operate.result=label; list_op.add(operate); @@ -307,17 +308,17 @@ public void exec_exp_B(Exp_B exp_b,String label){ } } } - - //ͳһ - public String backString(Exp_mark mark,type_cal match_type){ + + + private String backString(Exp_mark mark,type_cal match_type){ // String s=null; -// ȼǷDZֳ + if(mark.type==type_cal.ID || mark.type==type_cal.NORMAL_NUM){ type_cal t=check_define(mark.value); if(t==match_type ||mark.type==type_cal.NORMAL_NUM){ // s=mark.value; }else{ - if(t==null){//δ,Ҳdz + if(t==null){ error.error_print(3, mark.value, mark.line, mark.row); }else{ count++; @@ -327,7 +328,7 @@ public String backString(Exp_mark mark,type_cal match_type){ }else{ - //ַ + count++; error.error_print(9, mark.type.toString(), mark.line, mark.row); @@ -335,13 +336,13 @@ public String backString(Exp_mark mark,type_cal match_type){ return mark.value; // return s; } - //תıǩ - public String getLabel(){ + + private String getLabel(){ Lab++; return "Label"+Lab; } - //רʶȽϷ - public type_op get_compare_mark(Exp exp){ + + private type_op get_compare_mark(Exp exp){ type_op comp_op=null; switch (exp.type) { case BIG: @@ -360,32 +361,32 @@ public type_op get_compare_mark(Exp exp){ } return comp_op; } - public Table clone_tab(Table tab){ + private Table clone_tab(Table tab){ Table t=new Table(); t.Dimension=tab.Dimension; t.type=tab.type; t.code_index=tab.code_index; return t; } - public Exp_Operate clone_Operate(Exp_Operate oper){ - Exp_Operate ope=new Exp_Operate(); + private ExpOperate clone_Operate(ExpOperate oper){ + ExpOperate ope=new ExpOperate(); ope.op=oper.op; ope.s=oper.s; ope.d=oper.d; ope.result=oper.result; return ope; } - - //ļĶ - public type_cal check_define(String key){ + + + private type_cal check_define(String key){ return st.check_def(key); } } -//ijαΪ + enum type_op{ADD,SUB,MUL,DIV,JMP,LABEL,CJMP_E,CJMP_B,CJMP_L,CJMP_B_E,CJMP_L_E,SEND,DEFINE} -class Exp_Operate{ - type_op op; // - String s; //Դ1 - String d; //Դ2 - String result; // +class ExpOperate { + type_op op; + String s; + String d; + String result; } diff --git a/compilelib/src/main/java/com/example/compilelib/parse/ParseToken.java b/compilelib/src/main/java/com/example/compilelib/parse/ParseToken.java new file mode 100644 index 0000000..b766d1c --- /dev/null +++ b/compilelib/src/main/java/com/example/compilelib/parse/ParseToken.java @@ -0,0 +1,591 @@ +package com.example.compilelib.parse; + +import com.example.compilelib.Error; + +public class ParseToken { + boolean flag_main = false; //寻找main() + boolean flag_token = false, //防止扫描token时错开 + flag_print = false; + TokenList tk_list = null; + static int index = 0; + int count = 0; //count 记录一共有多少个错误,index全局变量是用于取token + Exp_func exp_fun = null; + Error error = null; + + public ParseToken(TokenList tokenList) { + this.tk_list = tokenList; + } + + public void startParse(){ + if (this.tk_list != null) { + error = new Error(); + parse_start(); + } + } + public Exp_func getExpFun(){ + return exp_fun; + } + private String[] getToken(int index) { + return this.tk_list.getToken(index).toString().split(":"); + } + private void printSelf() { + int i = 0; + while (i < this.tk_list.getsize()) { + String[] s = getToken(i); + System.out.println(s[0]+" "+s[1]+" "+s[2]+" "+s[3]); + i++; + } + } + private void parse_start() { + printSelf(); + while (index < this.tk_list.getsize()) { + String[] s = getToken(index); + + if (s[0].equals("func")) { + index++; + parse_P(); + } else { + count++; + error.error_print(0, getToken(index)[1], getToken(index)[2], getToken(index)[3]); + index++; + break; + } + } + if (count > 0) { + exp_fun = null; //如果语法分析有错误,则将抽象语法树置空; + } else { + System.out.println(count + " error(s)"); + System.out.println("parse over!!"); + } + } + //比较token + private boolean compare_token(String s) { + boolean flag = false; + + if (getToken(index)[0].equals(s)) { + flag = true; + + } + return flag; + } + //验证标识符 + private void parse_mark(String s) { + print(s); + if (getToken(index)[0].equals(s)) { + index++; + } else { + if (flag_token) { + if (getToken(index - 1)[0].equals(s)) { + flag_token = false; //在此条件下,游标不移动。 + } + } else { + flag_token = true; + count++; + String[] k = getToken(index); + index++; + error.error_print(6, s, k[2], k[3]); + } + } + + } + //验证关键字 + private boolean recognize_keyword(String key) { + //print(key); + boolean flag = false; + if (getToken(index)[0].equals(key)) { + index++; + flag = true; + } else { + + if (flag_token == true) { //若前面的token漏写了,则整体扫描会错位 + if (getToken(index - 1)[0].equals(key)) { +// index++; + flag = true; + flag_token = false; + } + } else { + + count++; + error.error_print(7, getToken(index)[1], getToken(index)[2], getToken(index)[3]); + flag_token = true; +// index++; + } + } + return flag; + } + + private Exp_func parse_P() { + + exp_fun = new Exp_func(); + exp_fun.type = type_cal.FUNC; + exp_fun.name = parse_N(); + parse_mark("("); + if (!getToken(index)[0].equals(")")) { + error.error_print(0, getToken(index)[1], getToken(index)[2], getToken(index)[3]); + index++; + } + parse_mark(")"); + parse_mark("{"); + + String next_token = getToken(index)[0]; + + while (!next_token.equals("}")) { + exp_fun.setExp(parse_if_wh()); + next_token = getToken(index)[0]; + } + index++; + return exp_fun; + } + + //由于if 和while经常交叉使用,相互包含,故孤立出一个函数 + private Exp parse_if_wh() { + Exp exp = null; + String nexttoken = getToken(index)[0]; + switch (nexttoken) { + case "if": + exp = parse_I(); + break; + case "while": + exp = parse_W(); + break; +// case "else":exp=parse_I();break; + case "num": + case "string": + case "bool": + exp = parse_M(); + break; + default: + exp = parse_E(); + break; + } + return exp; + } + //验证while语句块: + private Exp_whi parse_W() { + Exp_whi whi = new Exp_whi(); + whi.type = type_cal.WHI; + recognize_keyword("while"); + parse_mark("("); + whi.condition = parse_C(); + parse_mark(")"); + parse_mark("{"); + boolean flag = true; + Exp_B exp_b = new Exp_B(); + while (flag) { +// parse_if_wh(); + Exp exp = null; + String nexttoken = getToken(index)[0]; + switch (nexttoken) { + case "if": + exp = parse_I(); + break; + case "while": + exp = parse_W(); + break; +// case "else":exp=parse_I();break; + case "num": + case "string": + case "bool": + exp = parse_M(); + break; + default: + exp = parse_E(); + break; + } + + exp_b.setList_exp(exp); + + flag = !compare_token("}"); + if (flag && flag_token) { + if (getToken(index - 1)[0].equals("}")) { + flag = false; + } + } + } + if (!flag_token) { + index++; + } else { + flag_token = false; + } + whi.next = exp_b; + return whi; + } + + private Exp parse_I() { + Exp_ife ife = null; +// Exp_B exp_b=null; + if (ife == null) { + ife = new Exp_ife(); + + } + ife.type = type_cal.IFE; + if (recognize_keyword("if")) { + parse_mark("("); + ife.condition = parse_C(); + parse_mark(")"); + parse_mark("{"); + Exp_B exp_b = new Exp_B(); + while (!compare_token("}")) { +// parse_if_wh(); + Exp exp = null; + String nexttoken = getToken(index)[0]; + switch (nexttoken) { + case "if": + exp = parse_I(); + break; + case "while": + exp = parse_W(); + break; +// case "else":exp=parse_I();break; + case "num": + case "string": + case "bool": + exp = parse_M(); + break; + default: + exp = parse_E(); + break; + + + } + exp_b.setList_exp(exp); + ife.left = exp_b; + } + index++; + } + if (getToken(index)[1].equals("else")) { //此处不同recognize_key()函数是考虑到没有else,也不必报错 + index++; + parse_mark("{"); + Exp_B exp_b = new Exp_B(); + while (!compare_token("}")) { +// parse_if_wh(); +// ife.right=dothis(); + Exp exp = null; + String nexttoken = getToken(index)[0]; + switch (nexttoken) { + case "if": + exp = parse_I(); + break; + case "while": + exp = parse_W(); + break; +// case "else":exp=parse_I();break; + case "num": + case "string": + case "bool": + exp = parse_M(); + break; + default: + exp = parse_E(); + break; + + } + exp_b.setList_exp(exp); + ife.right = exp_b; + } + index++; + } + + return ife; + } + + //验证表达式 + private Exp_mark parse_E() { +// Exp_mark exp_m=(Exp_mark) getLine_Row(new Exp_mark(),getToken(index)); +// exp_m.type=type_cal.ID; +// exp_m.value=parse_V(); + Exp_mark exp_m = parse_id(); + if (getToken(index)[0].equals("=")) { + index++; + exp_m.got = parse_K(); + } + parse_mark(";"); + return exp_m; + } + + + //验证表达式=右边 + /* + * K->GG' + * G'->+GG' + * G'->$ + */ + //一个表达式的抽象语法树的根节点没有type; + private Exp_calcul parse_K() { + Exp_calcul exp_calcul = (Exp_calcul) getLine_Row(new Exp_calcul(), getToken(index)); + exp_calcul.left = parse_G(); + exp_calcul.right = parse_G_(); + return exp_calcul; + } + + private Exp_calcul parse_G_() { + Exp_calcul exp_calcul = null; + if (getToken(index)[0].equals("+")) { + + exp_calcul = (Exp_calcul) getLine_Row(new Exp_calcul(), getToken(index)); + index++; + exp_calcul.type = type_cal.ADD; + exp_calcul.left = parse_G(); + exp_calcul.right = parse_G_(); + + } else if (getToken(index)[0].equals("-")) { + exp_calcul = (Exp_calcul) getLine_Row(new Exp_calcul(), getToken(index)); + index++; + exp_calcul.type = type_cal.ADD; + exp_calcul.left = parse_G(); + exp_calcul.right = parse_G_(); + + } else { + + } + + return exp_calcul; + } + + /*消除左递归:G->G*A|A + * G->VA_ + * A_->*VA_ + * A_->$ + */ + private Exp_calcul parse_G() { +// Exp_mark exp_m=null; + Exp_calcul exp_calcul = new Exp_calcul(); + exp_calcul.left = parse_id(); + exp_calcul.right = parse_A_(); + return exp_calcul; + } + + //在此处解析的时候回返回一个以计算符号为根节点的树,无法保证右子树是否为空 + private Exp_calcul parse_A_() { +// Exp_mark exp_m=null; + Exp_calcul exp_calcul = null; + + if (getToken(index)[0].equals("*")) { + + exp_calcul = (Exp_calcul) getLine_Row(new Exp_calcul(), getToken(index)); + index++; + exp_calcul.type = type_cal.MUL; + exp_calcul.left = parse_id(); + exp_calcul.right = parse_A_(); + } else if (getToken(index)[0].equals("/")) { + exp_calcul = (Exp_calcul) getLine_Row(new Exp_calcul(), getToken(index)); + index++; + exp_calcul.type = type_cal.DIV; + exp_calcul.left = parse_id(); + exp_calcul.right = parse_A_(); + //do nothing + } else { + + } + return exp_calcul; + } + + private Exp_mark parse_id() { + String[] s = getToken(index); + Exp_mark exp = null; + exp = (Exp_mark) getLine_Row(new Exp_mark(), s); + + if (compare_token("id")) { + exp.value = s[1]; + exp.type = type_cal.ID; + index++; + } else if (compare_token("id_string")) { + index++; + exp.value = s[1]; + exp.type = type_cal.NORMAL_STRING; + + } else if (compare_token("id_num")) { + index++; + exp.value = s[1]; + exp.type = type_cal.NORMAL_NUM; + } else { + count++; + error.error_print(1, getToken(index)[1], getToken(index)[2], getToken(index)[3]); + index++; + } + + return exp; + } + + + +// public String parse_(){ +// String[] s=getToken(index); +//// Exp_mark exp=null; +//// exp=(Exp_mark) getLine_Row(new Exp_mark(),s); +// +// if(compare_token("id")){ +//// exp.value=s[1]; +//// exp.type=type_cal.ID; +// index++; +// }else if(compare_token("id_string")){ +// index++; +//// exp.value=s[1]; +//// exp.type=type_cal.NORMAL_STRING; +// +// }else if(compare_token("id_num")){ +// index++; +//// exp.value=s[1]; +//// exp.type=type_cal.NORMAL_NUM; +// } +// else{ +// count++; +// error.error_print(1, getToken(index)[1], getToken(index)[2], getToken(index)[3]); +// index++; +// } +// +// return s[1]; +// } + //��֤�������ʽ + private Exp_cond parse_C() { + Exp_cond exp_condition = new Exp_cond(); + exp_condition.left = parse_id(); + exp_condition.exp_comp = parse_comp(); + exp_condition.right = parse_id(); + if (getToken(index)[0].equals("&") || getToken(index).equals("|")) { + index++; + parse_C(); + } + +// index++; + return exp_condition; + } + + //�Ƚ������ + private Exp_comp parse_comp() { + String[] s = getToken(index); + Exp_comp exp_compare = (Exp_comp) getLine_Row(new Exp_comp(), s); + if (s[0].equals("<")) { + index++; + exp_compare.type = type_cal.LITTLE; + } else if (s[0].equals(">")) { + index++; + exp_compare.type = type_cal.BIG; + } else if (s[0].equals(">=")) { + index++; + exp_compare.type = type_cal.BIG_EQUAL; + } else if (s[0].equals("<=")) { + index++; + exp_compare.type = type_cal.LITTLE_EQUAL; + } else if (s[0].equals("==")) { + index++; + exp_compare.type = type_cal.EQUAL; + } else { + if (flag_token) { + String[] s1 = getToken(index - 1); + if (s1[0].equals(">") || s1[0].equals(">") || s1[0].equals(">=") || s1[0].equals("<=") || s1[0].equals("==")) { + flag_token = false; + } + } else { + count++; + flag_token = true; + error.error_print(1, s[1], s[2], s[3]); + index++; + } + } + return exp_compare; + } + + // //ֻ������֤�������ݵIJ������� +// public Exp_type parse_J(){ +//// Exp_defvar def_var=new Exp_defvar(); +// Exp_type exp_type=(Exp_type) getLine_Row(new Exp_type(),getToken(index)); +// exp_type.type=parse_T(); +// exp_type.Id=parse_id(); +// if(exp_type.type==type_cal.NUM){ +// exp_type.type=type_cal.TRANS_NUM; +// }else if(exp_type.type==type_cal.STRING){ +// exp_type.type=type_cal.TRANS_STRING; +// }else{ +// exp_type.type=type_cal.TRANS_BOOL; +// } +// return exp_type; +// } + //��֤������ + private Exp_funcname parse_N() { + Exp_funcname fun_name = new Exp_funcname(); + String s[] = getToken(index); + if (s[0].equals("main")) { + flag_main = true; + index++; + fun_name.name = s[1]; + fun_name.line = s[2]; + fun_name.row = s[3]; + fun_name.type = type_cal.STRING; + } else { + if (flag_token) { + if (getToken(index - 1)[0].equals("main")) { + flag_token = false; + } + } else { + count++; + flag_token = true; + error.error_print(2, getToken(index - 1)[1], getToken(index - 1)[2], getToken(index - 1)[3]); + index++; + + } + } + return fun_name; + } + + //验证定义变量的表达式 + /* + * string a,b; + */ + private Exp_type parse_M() { + Exp_type exp_type = (Exp_type) getLine_Row(new Exp_type(), getToken(index)); + exp_type.type = parse_T(); + exp_type.Id = parse_M__(); + return exp_type; + } + + private Exp_mark parse_M__() { + Exp_mark exp = parse_id(); + while (getToken(index)[0].equals(",")) { + index++; + exp.setListValue(parse_id()); + } + parse_mark(";"); + return exp; + } + + //验证token的类型名 + private type_cal parse_T() { + String[] s = getToken(index); + type_cal type = type_cal.STRING; + if (s[0].equals("num")) { + index++; + type = type_cal.NUM; + } else if (s[0].equals("bool")) { + index++; + type = type_cal.BOOL; + } else if (s[0].equals("string")) { + index++; + } else { + if (flag_token) { + if (getToken(index - 1)[0].equals("num") || getToken(index - 1)[0].equals("bool") || getToken(index - 1)[0].equals("string")) { + flag_token = false; + } + } else { + count++; + + flag_token = true; + error.error_print(4, s[1], s[2], s[3]); + index++; + } + } + return type; + } + + private void print(String s) { + if (flag_print) { + System.out.println(s); + } + } + + private Exp getLine_Row(Exp exp, String[] s) { + exp.line = s[2]; + exp.row = s[3]; + return exp; + } +} diff --git a/src/parse/Symbol_Table.java b/compilelib/src/main/java/com/example/compilelib/parse/SymbolTable.java similarity index 50% rename from src/parse/Symbol_Table.java rename to compilelib/src/main/java/com/example/compilelib/parse/SymbolTable.java index 8d0addc..3e89e3b 100644 --- a/src/parse/Symbol_Table.java +++ b/compilelib/src/main/java/com/example/compilelib/parse/SymbolTable.java @@ -1,72 +1,70 @@ -package parse; +package com.example.compilelib.parse; -import java.nio.MappedByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; /* - * űҪڹͬʱ˶ + * */ -public class Symbol_Table { - Map m=null; - public Symbol_Table(){ - m=new HashMap(); +public class SymbolTable { + Map symbolMap =null; + public SymbolTable(){ + symbolMap =new HashMap(); } public void put(String key,Table value){ // boolean flag=flase; - if(m!=null){ + if(symbolMap !=null){ - if(m.get(key)!=null){ - value.nexttable=m.get(key); - m.put(key, value); + if(symbolMap.get(key)!=null){ + value.nexttable= symbolMap.get(key); + symbolMap.put(key, value); }else{ - m.put(key, value); + symbolMap.put(key, value); } } } public void delete(String key){ - if(m!=null){ - if(m.get(key).nexttable!=null){ - m.put(key, m.get(key).nexttable); + if(symbolMap !=null){ + if(symbolMap.get(key).nexttable!=null){ + symbolMap.put(key, symbolMap.get(key).nexttable); }else{ - m.remove(key); + symbolMap.remove(key); } } } - //ȷر + public type_cal check_def(String key){ boolean flag=false; type_cal t=null; - if(m!=null){ - flag=m.containsKey(key); + if(symbolMap !=null){ + flag= symbolMap.containsKey(key); if(flag){ - m.get(key).apoint++; - t=m.get(key).type; + symbolMap.get(key).apoint++; + t= symbolMap.get(key).type; } } return t; } public int get_appoint(String key){ - return m.get(key).apoint; + return symbolMap.get(key).apoint; } public List check_Dimension(int dim){ - //ɾڵǰάı壬ȷ - Iterator it=m.entrySet().iterator(); + + Iterator it= symbolMap.entrySet().iterator(); List list=new ArrayList(); -// hashMap֧ڱݵʱ޸ݣΪԼݵĴСб仯׳쳣 + while(it.hasNext()){ - Map.Entry map=(Entry) it.next(); + Entry map=(Entry) it.next(); // System.out.println(map.getKey()); // if(m.get(map.getKey())!=null) - if(m.get(map.getKey()).Dimension>dim){ + if(symbolMap.get(map.getKey()).Dimension>dim){ // delete(map.getKey()); // System.out.println("---"+map.getKey()); list.add(map.getKey()); @@ -85,22 +83,22 @@ public List check_Dimension(int dim){ } public int check_appoint(String key){ int a=-1; - if(m!=null){ - if(m.get(key)!=null){ - a=m.get(key).apoint; + if(symbolMap !=null){ + if(symbolMap.get(key)!=null){ + a= symbolMap.get(key).apoint; } } if(a==0){ } - return m.get(key).Dimension; + return symbolMap.get(key).Dimension; } } class Table{ type_cal type; - int apoint=0; //ø - int Dimension=1; //ά - int code_index=0; //ڼ¼ + int apoint=0; + int Dimension=1; + int code_index=0; Table nexttable=null; } \ No newline at end of file diff --git a/src/parse/Token_List.java b/compilelib/src/main/java/com/example/compilelib/parse/TokenList.java similarity index 77% rename from src/parse/Token_List.java rename to compilelib/src/main/java/com/example/compilelib/parse/TokenList.java index e76aa4a..cb7dcc7 100644 --- a/src/parse/Token_List.java +++ b/compilelib/src/main/java/com/example/compilelib/parse/TokenList.java @@ -1,14 +1,14 @@ -package parse; +package com.example.compilelib.parse; import java.util.ArrayList; import java.util.List; -public class Token_List { +public class TokenList { private List list=null; - public Token_List(){ + public TokenList(){ this.list=new ArrayList(); } - //еtoken뵽б + public boolean appendToken(StringBuilder sb){ boolean flag; flag=this.list.add(sb); @@ -16,14 +16,14 @@ public boolean appendToken(StringBuilder sb){ return flag; } - //һtoken + public String getToken(int index){ StringBuilder sb=null; if(index>-1 && index \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..587dda5 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,3 @@ +rootProject.name = "CompileApplication" +include ':app' +include ':compilelib' diff --git a/src/parse/Exps.java b/src/parse/Exps.java deleted file mode 100644 index 93e20f8..0000000 --- a/src/parse/Exps.java +++ /dev/null @@ -1,178 +0,0 @@ -package parse; - -import java.awt.SystemColor; -import java.util.ArrayList; -import java.util.List; - -//enum type_id {NUM,STRING,BOOL,NORMAL_NUM,NORMAL_STRING}; -enum type_com{BIG,LITTLE,EQUAL,BIG_EQUAL,LITTLE_EQUAL}; -enum type_cal{BIG,LITTLE,EQUAL,BIG_EQUAL,LITTLE_EQUAL, - GOT,ADD,SUB,MUL,DIV,ID,NUM,STRING,BOOL,NORMAL_NUM, - NORMAL_STRING,TRANS_NUM,TRANS_STRING,TRANS_BOOL,FUNC,WHI,IFE}; -enum type_ret{NUM,STRING,BOOL}; - -class Exp{ - type_cal type; - String line; - String row; -} - -//ֵ߱ -class Exp_mark extends Exp{ - String value; - List list_value=null; - public void set_listvalue(Exp s){ - if(list_value==null){ - list_value=new ArrayList(); - } - list_value.add(s); - } - Exp got=null; //жϱǷһȺ -} -class Exp_type extends Exp{ - Exp Id=null; //ı -} -class Exp_calcul extends Exp{ - Exp left; - Exp right; -} -// -class Exp_cond extends Exp{ - Exp left; - Exp_comp exp_comp; - Exp right; -} -//ȽϷ -class Exp_comp extends Exp{ - -} -// -class Exp_func extends Exp{ - Exp_funcname name; - - -// String return_type; -// Exp_defvar exp; // -// List list_var=null; -// expʽĸҲ - List list_exp=null; -// whileѭĸҲ -// List list_whi=null; -// ifĸҲ -// List list_ife=null; - - public void setExp(Exp exp){ - if(list_exp==null){ - list_exp=new ArrayList(); - } - list_exp.add(exp); - } - -} -class Exp_funcname extends Exp{ - String name=null; -} -//// -//class Exp_defvar extends Exp{ -// -// String id_name; -// -//} - -class Exp_whi extends Exp{ -// Object exp; - - Exp_cond condition; -// Exp_ife if_el; - Exp next=null; -// List list_whi=null; -// List list_if=null; -// List list_exp=null; -// public void setList_whi(Exp whi){ -// if(list_whi==null){ -// list_whi=new ArrayList(); -// } -// list_whi.add(whi); -// } -// public void setList_if(Exp ife){ -// if(list_if==null){ -// list_if=new ArrayList(); -// } -// list_if.add(ife); -// } -// public void setList_exp(Exp exp){ -// if(list_exp==null){ -// list_exp=new ArrayList(); -// } -// list_exp.add(exp); -// } -} - -class Exp_ife extends Exp{ - Exp_cond condition; - - Exp left=null; //ʱ - Exp right=null; //ʱ -// List list_whi=null; -// List list_if=null; -// List list_exp=null; -// public void setList_whi(Exp whi){ -// if(list_whi==null){ -// list_whi=new ArrayList(); -// } -// list_whi.add(whi); -// } -// public void setList_if(Exp ife){ -// if(list_if==null){ -// list_if=new ArrayList(); -// } -// list_if.add(ife); -// } -// public void setList_exp(Exp exp){ -// if(list_exp==null){ -// list_exp=new ArrayList(); -// } -// list_exp.add(exp); -// } -// -//// elseµĿܵ -// Exp_cond condition1; -// List list_whi1=null; -// List list_if1=null; -// List list_exp1=null; -// public void setList_whi1(Exp whi){ -// if(list_whi1==null){ -// list_whi1=new ArrayList(); -// } -// list_whi1.add(whi); -// } -// public void setList_if1(Exp ife){ -// if(list_if1==null){ -// list_if1=new ArrayList(); -// } -// list_if1.add(ife); -// } -// public void setList_exp1(Exp exp){ -// if(list_exp1==null){ -// list_exp1=new ArrayList(); -// } -// list_exp1.add(exp); -// } -} -class Exp_B extends Exp{ -// Exp list_whi=null; -// Exp exp_if=null; - List list_exp=null; -// public void setList_whi(Exp whi){ -// list_whi=whi; -// } -// public void setList_if(Exp ife){ -// exp_if=ife; -// } - public void setList_exp(Exp exp){ - if(list_exp==null){ - list_exp=new ArrayList(); - } - list_exp.add(exp); - } -} \ No newline at end of file diff --git a/src/parse/HaveToken.java b/src/parse/HaveToken.java deleted file mode 100644 index 7de6a27..0000000 --- a/src/parse/HaveToken.java +++ /dev/null @@ -1,49 +0,0 @@ -package parse; - -import java.io.BufferedReader; -import java.io.InputStreamReader; - -public class HaveToken { - Token_List tk=null; - public HaveToken(){ - getToken(); - } - - public void getToken() { - try { - Process pr=Runtime.getRuntime().exec("D:\\Python34\\python.exe token.py test.t"); - BufferedReader in =new BufferedReader(new InputStreamReader(pr.getInputStream())); - String line; - tk=new Token_List(); - - String []wrong=null; - pr.waitFor(); - while((line=in.readLine())!=null){ - wrong=line.split(":"); - //equals()ʾȽ϶ݣ==Ƚ϶ĵַ - if(wrong[0].equals("word")){ - System.out.println(line); - tk=null; - break; - } - if(!wrong[1].equals("\"")){ //ȥַеġ - tk.appendToken(new StringBuilder(line)); -// System.out.println(line); - } - } - in.close(); - } catch (Exception e) { - // TODO Զɵ catch - e.printStackTrace(); - } - } - public Token_List getTokenList(){ - Token_List tokenlist=null; - if(this.tk!=null){ - tokenlist=this.tk; - } - return tokenlist; - } -} - - diff --git a/src/parse/Main.java b/src/parse/Main.java deleted file mode 100644 index 1d91f1b..0000000 --- a/src/parse/Main.java +++ /dev/null @@ -1,14 +0,0 @@ -package parse; - -public class Main { - - public static void main(String[] args) { - // TODO Զɵķ - Parse_Token pt =new Parse_Token(); - if(pt.exp_fun!=null){ - System.out.println("﷨"); - } - Parse_Semantics ps=new Parse_Semantics(pt.exp_fun); - } - -} diff --git a/src/parse/Parse_Token.java b/src/parse/Parse_Token.java deleted file mode 100644 index 45786be..0000000 --- a/src/parse/Parse_Token.java +++ /dev/null @@ -1,598 +0,0 @@ -package parse; - -public class Parse_Token { - boolean flag_main=false; //Ѱmain() - boolean flag_token=false,flag_print=false;//ֹɨtokenʱ - Token_List tk_list=null; - static int index=0; - int count=0;//count ¼һжٸindexȫֱȡtoken - Exp_func exp_fun=null; - analyize_error.Error error=null; - public Parse_Token(){ - this.tk_list=new HaveToken().getTokenList(); - if (this.tk_list!=null){ - error=new analyize_error.Error(); - parse_start(); - } - } - public String[] getToken(int index){ - return this.tk_list.getToken(index).toString().split(":"); - } - public void parse_start(){ - while(index0){ - exp_fun=null; //﷨д򽫳﷨ÿգ - }else{ - System.out.println(count+" error(s)"); - System.out.println("parse over!!"); - } - } - //Ƚtoken - public boolean compare_token(String s){ - boolean flag=false; - - if(getToken(index)[0].equals(s)){ - flag=true; - - } - return flag; - } - //֤ʶ - public void parse_mark(String s){ - print(s); - if(getToken(index)[0].equals(s)){ - index++; - }else{ - if(flag_token){ - if(getToken(index-1)[0].equals(s)){ - flag_token=false; //ڴ£α겻ƶ - } - }else{ - flag_token=true; - - count++; - String k[]=getToken(index); - index++; - error.error_print(6, s, k[2], k[3]); - } - } - - } - //֤ؼ - public boolean recognize_keyword(String key){ - //print(key); - boolean flag=false; - if(getToken(index)[0].equals(key)){ - index++; - flag=true; - }else{ - - if(flag_token==true){//ǰtoken©дˣɨλ - if(getToken(index-1)[0].equals(key)){ -// index++; - flag=true; - flag_token=false; - } - }else{ - - count++; - error.error_print(7,getToken(index)[1],getToken(index)[2],getToken(index)[3]); - flag_token=true; -// index++; - } - } - return flag; - } - - public Exp_func parse_P(){ -// recognize_keyword("void"); - exp_fun=new Exp_func(); - exp_fun.type=type_cal.FUNC; - exp_fun.name=parse_N(); //name - parse_mark("("); - if(!getToken(index)[0].equals(")")){ - error.error_print(0, getToken(index)[1], getToken(index)[2], getToken(index)[3]); - index++; -// exp_fun.setExp(parse_J()); //ӺĴݲ - } - parse_mark(")"); - parse_mark("{"); - //parse_B(); - String next_token=getToken(index)[0]; -// boolean flag=true; - while(!next_token.equals("}")){ - - exp_fun.setExp(parse_if_wh()); - - next_token=getToken(index)[0]; - } - index++; - return exp_fun; - } - - //if whileʹã໥ʹһ - public Exp parse_if_wh(){ - Exp exp=null; - String nexttoken=getToken(index)[0]; - switch(nexttoken){ - case "if" :exp=parse_I();break; - case "while":exp=parse_W();break; -// case "else":exp=parse_I();break; - case "num": - case "string": - case "bool":exp=parse_M();break; - default: exp=parse_E();break; - } - return exp; - } - //֤ڲ -// public void parse_B(){ -// String next_token=getToken(index)[0]; -//// boolean flag=true; -// while(!next_token.equals("}")){ -//// next_token=getToken(index)[0]; -//// if (next_token.equals("return")){ -//// index++; -//// parse_R(); -//// }else{ -// parse_if_wh(); -//// } -//// flag=!compare_token("}"); -// next_token=getToken(index)[0]; -// } -// index++; -// } - //֤while飺 - public Exp_whi parse_W(){ - Exp_whi whi=new Exp_whi(); - whi.type=type_cal.WHI; - recognize_keyword("while"); - parse_mark("("); - whi.condition=parse_C(); - parse_mark(")"); - parse_mark("{"); - boolean flag=true; - Exp_B exp_b=new Exp_B(); - while(flag){ -// parse_if_wh(); - Exp exp=null; - String nexttoken=getToken(index)[0]; - switch(nexttoken){ - case "if" :exp=parse_I();break; - case "while":exp=parse_W();break; -// case "else":exp=parse_I();break; - case "num": - case "string": - case "bool":exp=parse_M();break; - default: exp=parse_E();break; - } - - exp_b.setList_exp(exp); - - flag=!compare_token("}"); - if(flag && flag_token){ - if (getToken(index-1)[0].equals("}")){ - flag=false; - } - } - } - if(!flag_token){ - index++; - }else{ - flag_token=false; - } - whi.next=exp_b; - return whi; - } - //֤return䣺 -// public void parse_R(){ -// parse_V(); -// parse_mark(";"); -// } -// //֤else -// public void parse_EL(){ -// recognize_keyword("else"); -// -// parse_mark("{"); -// while(!compare_token("}")){ -// parse_if_wh(); -// } -// index++; -// } - //֤if-else - public Exp parse_I(){ - Exp_ife ife=null; -// Exp_B exp_b=null; - if(ife==null){ - ife=new Exp_ife(); - - } - ife.type=type_cal.IFE; - if(recognize_keyword("if")){ - parse_mark("("); - ife.condition=parse_C(); - parse_mark(")"); - parse_mark("{"); - Exp_B exp_b=new Exp_B(); - while(!compare_token("}")){ -// parse_if_wh(); - Exp exp=null; - String nexttoken=getToken(index)[0]; - switch(nexttoken){ - case "if" :exp=parse_I();break; - case "while":exp=parse_W();break; -// case "else":exp=parse_I();break; - case "num": - case "string": - case "bool":exp=parse_M();break; - default: exp=parse_E();break; - - - } - exp_b.setList_exp(exp); - ife.left=exp_b; - } - index++; - } - if(getToken(index)[1].equals("else")){ //˴ͬrecognize_key()ǿǵûelseҲر - index++;//Ӧģindex++ - parse_mark("{"); - Exp_B exp_b=new Exp_B(); - while(!compare_token("}")){ -// parse_if_wh(); -// ife.right=dothis(); - Exp exp=null; - String nexttoken=getToken(index)[0]; - switch(nexttoken){ - case "if" :exp=parse_I();break; - case "while":exp=parse_W();break; -// case "else":exp=parse_I();break; - case "num": - case "string": - case "bool":exp=parse_M();break; - default: exp=parse_E();break; - - } - exp_b.setList_exp(exp); - ife.right=exp_b; - } - index++; - } - - return ife; - } - - //֤ʽ - public Exp_mark parse_E(){ -// Exp_mark exp_m=(Exp_mark) getLine_Row(new Exp_mark(),getToken(index)); -// exp_m.type=type_cal.ID; -// exp_m.value=parse_V(); - Exp_mark exp_m=parse_id(); - if (getToken(index)[0].equals("=")){ - index++; - exp_m.got=parse_K(); - } - parse_mark(";"); - return exp_m; - } - //y֤ʽ=ұ - /* - * K->GG' - * G'->+GG' - * G'->$ - */ - //һʽij﷨ĸڵûtype; - public Exp_calcul parse_K(){ - Exp_calcul exp_calcul=(Exp_calcul) getLine_Row(new Exp_calcul(),getToken(index)); - exp_calcul.left=parse_G(); - exp_calcul.right=parse_G_(); - return exp_calcul; - } - public Exp_calcul parse_G_(){ - Exp_calcul exp_calcul=null; - if(getToken(index)[0].equals("+")){ - - exp_calcul=(Exp_calcul) getLine_Row(new Exp_calcul(),getToken(index)); - index++; - exp_calcul.type=type_cal.ADD; - exp_calcul.left=parse_G(); - exp_calcul.right=parse_G_(); - -// parse_G(); -// parse_G_(); - }else if(getToken(index)[0].equals("-")){ - exp_calcul=(Exp_calcul) getLine_Row(new Exp_calcul(),getToken(index)); - index++; - exp_calcul.type=type_cal.ADD; - exp_calcul.left=parse_G(); - exp_calcul.right=parse_G_(); - - }else{ - - } - - return exp_calcul; - } - - /*ݹ飺G->G*A|A - * G->VA_ - * A_->*VA_ - * A_->$ - */ - public Exp_calcul parse_G(){ -// Exp_mark exp_m=null; - Exp_calcul exp_calcul=new Exp_calcul(); - exp_calcul.left=parse_id(); - exp_calcul.right=parse_A_(); - return exp_calcul; - } - - //ڴ˴ʱطһԼΪڵ޷֤ǷΪ - public Exp_calcul parse_A_(){ -// Exp_mark exp_m=null; - Exp_calcul exp_calcul=null; - - if(getToken(index)[0].equals("*")){ - - exp_calcul=(Exp_calcul) getLine_Row(new Exp_calcul(),getToken(index)); - index++; - exp_calcul.type=type_cal.MUL; - exp_calcul.left=parse_id(); - exp_calcul.right=parse_A_(); - }else if(getToken(index)[0].equals("/")){ - exp_calcul=(Exp_calcul) getLine_Row(new Exp_calcul(),getToken(index)); - index++; - exp_calcul.type=type_cal.DIV; - exp_calcul.left=parse_id(); - exp_calcul.right=parse_A_(); - //do nothing - }else{ - - } - return exp_calcul; - } - - public Exp_mark parse_id(){ - String[] s=getToken(index); - Exp_mark exp=null; - exp=(Exp_mark) getLine_Row(new Exp_mark(),s); - - if(compare_token("id")){ - exp.value=s[1]; - exp.type=type_cal.ID; - index++; - }else if(compare_token("id_string")){ - index++; - exp.value=s[1]; - exp.type=type_cal.NORMAL_STRING; - - }else if(compare_token("id_num")){ - index++; - exp.value=s[1]; - exp.type=type_cal.NORMAL_NUM; - } - else{ - count++; - error.error_print(1, getToken(index)[1], getToken(index)[2], getToken(index)[3]); - index++; - } - - return exp; - } - - -// //֤߳ -// public String parse_(){ -// String[] s=getToken(index); -//// Exp_mark exp=null; -//// exp=(Exp_mark) getLine_Row(new Exp_mark(),s); -// -// if(compare_token("id")){ -//// exp.value=s[1]; -//// exp.type=type_cal.ID; -// index++; -// }else if(compare_token("id_string")){ -// index++; -//// exp.value=s[1]; -//// exp.type=type_cal.NORMAL_STRING; -// -// }else if(compare_token("id_num")){ -// index++; -//// exp.value=s[1]; -//// exp.type=type_cal.NORMAL_NUM; -// } -// else{ -// count++; -// error.error_print(1, getToken(index)[1], getToken(index)[2], getToken(index)[3]); -// index++; -// } -// -// return s[1]; -// } - //֤ʽ - public Exp_cond parse_C(){ - Exp_cond exp_condition=new Exp_cond(); - exp_condition.left=parse_id(); - exp_condition.exp_comp=parse_comp(); - exp_condition.right=parse_id(); - if(getToken(index)[0].equals("&")||getToken(index).equals("|")){ - index++; - parse_C(); - } - -// index++; - return exp_condition; - } - - //Ƚ - public Exp_comp parse_comp(){ - String[] s=getToken(index); - Exp_comp exp_compare=(Exp_comp) getLine_Row(new Exp_comp(), s); - if (s[0].equals("<") ){ - index++; - exp_compare.type=type_cal.LITTLE; - }else if(s[0].equals(">")){ - index++; - exp_compare.type=type_cal.BIG; - }else if(s[0].equals(">=")){ - index++; - exp_compare.type=type_cal.BIG_EQUAL; - }else if(s[0].equals("<=")){ - index++; - exp_compare.type=type_cal.LITTLE_EQUAL; - }else if(s[0].equals("==")){ - index++; - exp_compare.type=type_cal.EQUAL; - } - else{ - if(flag_token){ - String[] s1=getToken(index-1); - if(s1[0].equals(">") ||s1[0].equals(">")||s1[0].equals(">=")||s1[0].equals("<=")||s1[0].equals("==")){ - flag_token=false; - } - }else{ - count++; - flag_token=true; - error.error_print(1, s[1], s[2], s[3]); - index++; - } - } - return exp_compare; - } -// //ֻ֤ݵIJ -// public Exp_type parse_J(){ -//// Exp_defvar def_var=new Exp_defvar(); -// Exp_type exp_type=(Exp_type) getLine_Row(new Exp_type(),getToken(index)); -// exp_type.type=parse_T(); -// exp_type.Id=parse_id(); -// if(exp_type.type==type_cal.NUM){ -// exp_type.type=type_cal.TRANS_NUM; -// }else if(exp_type.type==type_cal.STRING){ -// exp_type.type=type_cal.TRANS_STRING; -// }else{ -// exp_type.type=type_cal.TRANS_BOOL; -// } -// return exp_type; -// } - //֤ - public Exp_funcname parse_N(){ - Exp_funcname fun_name=new Exp_funcname(); - String s[]=getToken(index); - if (s[0].equals("main")){ - flag_main=true; - index++; - fun_name.name=s[1]; - fun_name.line=s[2]; - fun_name.row=s[3]; - fun_name.type=type_cal.STRING; - }else{ - if(flag_token){ - if(getToken(index-1)[0].equals("main")){ - flag_token=false; - } - }else{ - count++; - flag_token=true; - error.error_print(2, getToken(index-1)[1], getToken(index-1)[2], getToken(index-1)[3]); - index++; -// parse_V(); ʱֻһmain - } - } - return fun_name; - } -//֤ıʽ - /* - * string a,b; - */ - public Exp_type parse_M(){ - Exp_type exp_type=(Exp_type) getLine_Row(new Exp_type(),getToken(index)); - exp_type.type=parse_T(); - exp_type.Id=parse_M__(); - return exp_type; - } - - public Exp_mark parse_M__(){ - Exp_mark exp=parse_id(); - while(getToken(index)[0].equals(",")){ - index++; - exp.set_listvalue(parse_id()); - } - parse_mark(";"); - return exp; - } - //֤token - public type_cal parse_T(){ - String[] s=getToken(index); - type_cal type=type_cal.STRING; - if(s[0].equals("num")){ - index++; - type=type_cal.NUM; - } - else if(s[0].equals("bool")){ - index++; - type=type_cal.BOOL; - }else if(s[0].equals("string")){ - index++; - } - else{ - if(flag_token){ - if(getToken(index-1)[0].equals("num")||getToken(index-1)[0].equals("bool")||getToken(index-1)[0].equals("string")){ - flag_token=false; - } - }else{ - count++; - - flag_token=true; - error.error_print(4, s[1], s[2], s[3]); - index++; - } - } - return type; - } - public void print(String s){ - if(flag_print){ - System.out.println(s); - } - } -// //֤ -// public void parse_(){ -// String s[]=getToken(index); -// if(s[0].equals("id")){ -// index++; -// }else{ -// if(flag_token){ -// if(getToken(index-1)[0].equals("id")){ -// flag_token=false; -// } -// }else{ -// count++; -// index++; -// flag_token=true; -// error.error_print(5, s[1], s[2], s[3]); -// } -// } -// -//// s=null; -// } -public Exp getLine_Row(Exp exp,String[] s){ - exp.line=s[2]; - exp.row=s[3]; - return exp; -} -} diff --git a/src/parse/Variable_Table.java b/src/parse/Variable_Table.java deleted file mode 100644 index c4e1325..0000000 --- a/src/parse/Variable_Table.java +++ /dev/null @@ -1,5 +0,0 @@ -package parse; - -public class Variable_Table { - -} diff --git a/test.t b/test.t deleted file mode 100644 index 48581b2..0000000 --- a/test.t +++ /dev/null @@ -1,18 +0,0 @@ -func main(){ -num a,b; -a=1; -b=0; -while(b<5){ - num c,k; - b=b+1; - if(b<3){ - c=b+b*a; - }else{ - c=b; - } -} -num d,c; -c=1; -a=b*a*c+b*4; - -} \ No newline at end of file