资源简介

使用Java语言完成的两个实验代码(不是C++),实验一注释齐全,完成质量较高;实验二的实现仅达到作业最低要求(得不了高分) 实验报告质量很高 请同学在下载后,如果没有能力自己独立完成,还请阅读代码后经过简单更改再交作业,最好能搞明白代码实现的思路(尤其是实验一)

资源截图

代码片段和文件信息

import com.sun.java.accessibility.util.Translator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Recognizer {
    private static final String NUMBER = “0123456789“;
    private static final String ALPHABET = “qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_“;


    private FileReader fileReader;
    private int currentCharacter = -2;  //当前读到的字符,fileReader()的返回值为int,使用强制转换转为char
                                        //-2表示还没有读文件

    private int state = 1;  //状态值,初始值为1,每识别一个Token后清0,恢复为1
    private String type;    //根据结束状态值,识别出的值类型
                            //identifier——标识符,integerConstant——整数型数值常量(字符常量不由词法分析器识别出来),floatConstant——浮点型数值常量,keyword——关键字,delimiter——定界符
    private ArrayList integerConstants = new ArrayList<>();
    private ArrayList floatConstants = new ArrayList<>();
    private static final String[] keywords = {“abstract“ “assert“ “boolean“ “break“ “byte“ “case“ “catch“ “char“ “class“
                                 “const“ “continue“ “default“ “do“ “double“ “else“ “enum“ “extends“ “final“
                                 “finally“ “float“ “for“ “goto“ “if“ “implements“ “import“ “instanceof“ “int“
                                 “interface“ “long“ “native“ “new“ “package“ “private“ “protected“ “public“
                                 “return“ “short“ “static“ “strictfp“ “super“ “switch“ “synchronized“ “this“
                                 “throw“ “throws“ “transient“ “try“ “void“ “volatile“ “while“};
    private ArrayList identifiers = new ArrayList<>();
    private static final char[] singleDelimiter = {‘;‘ ‘‘ ‘(‘ ‘)‘ ‘:‘ ‘+‘ ‘-‘ ‘*‘ ‘/‘ ‘%‘ ‘=‘  ‘>‘ ‘<‘
                                      ‘{‘ ‘}‘ ‘.‘ ‘[‘ ‘]‘ ‘?‘ ‘!‘ ‘&‘ ‘~‘ ‘|‘ ‘^‘};
    private static final String[] doubleDelimiter = {“[]“ “()“ “{}“ “<>“ “++“ “--“ “+=“ “-=“ “*=“ “/=“ “%=“ “&=“ “|=“
                                        “^=“ “&&“ “||“ “>=“ “<=“ “==“ “!=“ “<<“ “>>“};         //对于更多位的界符>>>、instanceof、<<=、>>=、>>>=不支持

    private ArrayList delimiter = new ArrayList<>();
    private Token currentToken;

    public Recognizer(String fileName) throws FileNotFoundException {
        fileReader = new FileReader(new File(fileName));
    }


    public int execute() {
        //在Windows中,\n是换行,即将光标移到下一行,但水平位置不变;\r是将光标移到该行的前面
        while ((char)currentCharacter == ‘ ‘ || (char)currentCharacter == ‘\n‘ || (char)currentCharacter == ‘\r‘ || (char)currentCharacter == ‘\t‘ || currentCharacter == -2) {
            try {
                currentCharacter = fileReader.read();
            } catch (IOException e) {
                System.out.println(“Recognizer类start()方法中,while循环中的fileReader.read()抛出异常“);
            }
        }

        if (currentChar

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-10-22 17:12  实验一+实验二的报告\
     文件     1194399  2020-10-22 17:06  实验一+实验二的报告\实验报告.docx
     文件      147456  2020-10-22 17:00  实验一+实验二的报告\编译方法实验指导书.doc
     目录           0  2020-10-22 16:57  实验一+实验二的源代码\
     目录           0  2020-10-22 16:57  实验一+实验二的源代码\实验1\
     文件       13817  2020-03-25 14:55  实验一+实验二的源代码\实验1\Recognizer.java
     文件        2433  2020-03-25 15:14  实验一+实验二的源代码\实验1\Scanner.java
     文件         306  2020-04-07 23:17  实验一+实验二的源代码\实验1\testcase.txt
     文件         377  2020-03-20 22:45  实验一+实验二的源代码\实验1\Token.java
     目录           0  2020-10-22 16:57  实验一+实验二的源代码\实验2\
     文件        3559  2020-04-13 14:49  实验一+实验二的源代码\实验2\Main.java
     文件         495  2020-04-13 12:47  实验一+实验二的源代码\实验2\Operand.java
     文件         222  2020-04-13 12:47  实验一+实验二的源代码\实验2\OperSymbol.java
     文件         896  2020-04-13 12:47  实验一+实验二的源代码\实验2\Quat.java
     文件          22  2020-04-11 10:33  实验一+实验二的源代码\实验2\testcase.txt

评论

共有 条评论