• 大小: 43KB
    文件类型: .zip
    金币: 2
    下载: 2 次
    发布日期: 2021-10-08
  • 语言: Java
  • 标签: 编译原理  Java  

资源简介

语法分析器java实现,包含词法分析器。程序代码作为词法分析器的输入,词法分析器的输出作为语法分析器的输入,由语法分析器输出语法分析的结果。

资源截图

代码片段和文件信息

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 词法分析器
 */
public class LexicalAnalyzer {

    // 单词种别码 1-17为关键字种别码
    public static final int CHAR = 1;
    public static final int SHORT = 2;
    public static final int INT = 3;
    public static final int LONG = 4;
    public static final int FLOAT = 5;
    public static final int DOUBLE = 6;
    public static final int FINAL = 7;
    public static final int STATIC = 8;
    public static final int IF = 9;
    public static final int ELSE = 10;
    public static final int WHILE = 11;
    public static final int DO = 12;
    public static final int FOR = 13;
    public static final int BREAK = 14;
    public static final int CONTINUE = 15;
    public static final int VOID = 16;
    public static final int RETURN = 17;

    // 20为标识符种别码
    public static final int ID = 20;

    // 30为常量种别码
    public static final int NUM = 30;

    // 31-40为运算符种别码
    public static final int AS = 31; // =
    public static final int EQ = 32; // ==
    public static final int GT = 33; // >
    public static final int LT = 34; // <
    public static final int GE = 35; // >=
    public static final int LE = 36; // <=
    public static final int ADD = 37; // +
    public static final int SUB = 38; // -
    public static final int MUL = 39; // *
    public static final int DIV = 40; // /

    // 41-49为界限符种别码
    public static final int LP = 41; // (
    public static final int RP = 42; // )
    public static final int LBT = 43; // [
    public static final int RBT = 44; // ]
    public static final int LBS = 45; // {
    public static final int RBS = 46; // }
    public static final int COM = 47; // 
    public static final int COL = 48; // :
    public static final int SEM = 49; // ;

    // -1为无法识别的字符标志码
    public static final int ERROR = -1;
    public static int errorNum = 0; // 记录词法分析错误的个数

    // 判断是否为字母
    public static boolean isLetter(char c) {
        if (((c >= ‘a‘) && (c <= ‘z‘)) || ((c >= ‘A‘) && (c <= ‘Z‘))) {
            return true;
        }
        return false;
    }

    // 判断是否为关键字,若是则返回关键字种别码
    public static int isKeyID(String str) {
        String keystr[] = {“char“ “short“ “int“ “long“ “float“ “double“ “final“ “static“ “if“ “else“ “while“
                “do“ “for“ “break“ “continue“ “void“ “return“};
        for (int i = 0; i < keystr.length; i++) {
            if (str.equals(keystr[i])) {
                return i + 1;
            }
        }
        return 0;
    }

    // 判断是否为常量(整数、小数、浮点数)
    public static boolean isNum(String str) {
        int dot = 0; // .的个数
        int notNum = 0; // 不是数字的个数
        for (int i = 0; i < str.length(); i++) {
            if (!(str.charAt(i) >= ‘0‘ && str.charAt(i) <= ‘9‘)) {
                notNum++;
                if (notNum > dot + 1) {
           

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-06 18:08  SyntaxAnalyzer\
     目录           0  2019-01-18 10:22  SyntaxAnalyzer\.idea\
     文件         278  2018-12-05 15:49  SyntaxAnalyzer\.idea\misc.xml
     文件         275  2018-12-05 15:49  SyntaxAnalyzer\.idea\modules.xml
     文件       17040  2019-01-18 10:22  SyntaxAnalyzer\.idea\workspace.xml
     目录           0  2018-12-05 20:10  SyntaxAnalyzer\out\
     目录           0  2018-12-05 20:10  SyntaxAnalyzer\out\production\
     目录           0  2018-12-06 19:31  SyntaxAnalyzer\out\production\SyntaxAnalyzer\
     目录           0  2018-12-06 20:02  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\
     文件          70  2018-12-06 19:42  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input1.txt
     文件          56  2018-12-06 19:46  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input2.txt
     文件          53  2018-12-06 19:49  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input3.txt
     文件         112  2018-12-06 19:52  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input4.txt
     文件          77  2018-12-06 19:56  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input5.txt
     文件         375  2018-12-06 20:02  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\input6.txt
     文件        3275  2018-12-06 19:44  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output1.txt
     文件        3838  2018-12-06 19:47  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output2.txt
     文件        1818  2018-12-06 19:51  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output3.txt
     文件        6756  2018-12-06 17:03  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output4.txt
     文件        5089  2018-12-06 20:01  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output5.txt
     文件       29494  2018-12-06 19:39  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\output6.txt
     文件          38  2018-12-06 19:39  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test1.txt
     文件          20  2018-12-06 19:44  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test2.txt
     文件          17  2018-12-06 19:47  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test3.txt
     文件          53  2018-12-06 19:51  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test4.txt
     文件          36  2018-12-06 17:03  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test5.txt
     文件         179  2018-12-06 20:01  SyntaxAnalyzer\out\production\SyntaxAnalyzer\file\test6.txt
     文件         517  2018-12-05 22:00  SyntaxAnalyzer\out\production\SyntaxAnalyzer\LexicalAnalyzer$Pair.class
     文件        9945  2018-12-05 22:00  SyntaxAnalyzer\out\production\SyntaxAnalyzer\LexicalAnalyzer.class
     文件         595  2018-12-06 19:31  SyntaxAnalyzer\out\production\SyntaxAnalyzer\SyntaxAnalyzer$Production.class
     文件       14143  2018-12-06 19:31  SyntaxAnalyzer\out\production\SyntaxAnalyzer\SyntaxAnalyzer.class
............此处省略23个文件信息

评论

共有 条评论