简单C语言的词法分析器(Java实现)

一、C语言子集的单词符号及内置码

单词符号 种别编号 助记符 内置码
while 1 while null
if 2 if null
else 3 else null
switch 4 switch null
case 5 case null
标识符 6 id id的实际内容
常数 7 num nmu的实际内容
+ 8 + null
- 9 - null
* 10 * null
<= 11 <= null
< 12 < null
== 13 == null
= 14 = null
; 15 ; null

二、状态转换图
在这里插入图片描述
三、实验结果截图
在这里插入图片描述
四、实验使用的类的方法、属性及其作用截图
在这里插入图片描述
五、实验代码

import java.util.Scanner;
public class Test{
    public static void main(String[] args){
        System.out.println("请输入源程序(以“#”结束):");
        Scanner input = new Scanner(System.in);
        input.useDelimiter("\n");  //将分隔符号修改为“\n”(回车)
        String1 test = new String1(input.next());
        test.result();
        System.out.println("分析结束!");
    }//测试的主函数
}
class String1{
    private String string1 =null;  //存放输入的字符串
    private String token = null;  //存放构成单词符号的字符串
    private int point = 0; //设置指针指向当前所读string1的位置
    private char character = ' ';  //字符变量,存放最新读入的源程序字符
    private final String[] reserved ={" ","while","if","else","switch","case"};  //保留字
    public String1(){
    }  //无参构造函数
    public String1(String a){
        string1 = a;
        point = -1;  //方便之后对character的判断
    }  //含参构造函数
    public void updateCharacter(){
        point++;
        character = string1.charAt(point);
    }  //更新字符character的内容
    public void getbe(){
        while(character == ' '){
            updateCharacter();
        } 
    }  //滤除空格
    public void concatenation(){
        token = token + character;
    }  //将字符串token与字符character拼接
    public boolean letter(){
        switch(character){
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                return true;
            default:
                return false;
        }
    }  //判断character中的字符是否为字母
    public boolean digit(){
         switch(character){
             case '0':
             case '1':
             case '2':
             case '3':
             case '4':
             case '5':
             case '6':
             case '7':
             case '8':
             case '9':
                 return true;
             default:
                 return false;
         }
    }  //判断character中的字符是否为数字
    public int reserve(){
        for(int i = 1;i<reserved.length;i++){
            if(token.equals(reserved[i]))
                return i;
        }
        return 0;
    }  //判断字符串token是否是保留字
    public void retract(){
        point = point - 1;
        character = ' ';
    }  //point指针退后一位,character清空
    public boolean reminder(){
        if(string1.charAt(string1.length()-1) != '#'){
            System.out.println("请以“#”符号结尾!");
            return true;
        }
        else return false;
    }  //检查输入字符串是否是以“#”号结尾
    public void result(){
        if(reminder()) return;
        character = string1.charAt(0);
        if(character == '#')  //当只有一个#时
            return;
        do{
            token = "";  
            updateCharacter();  //更新字符character
            getbe();  //滤除空格
            switch(character){
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                case 'E':
                case 'F':
                case 'G':
                case 'H':
                case 'I':
                case 'J':
                case 'K':
                case 'L':
                case 'M':
                case 'N':
                case 'O':
                case 'P':
                case 'Q':
                case 'R':
                case 'S':
                case 'T':
                case 'U':
                case 'V':
                case 'W':
                case 'X':
                case 'Y':
                case 'Z':
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                case 'g':
                case 'h':
                case 'i':
                case 'j':
                case 'k':
                case 'l':
                case 'm':
                case 'n':
                case 'o':
                case 'p':
                case 'q':
                case 'r':
                case 's':
                case 't':
                case 'u':
                case 'v':
                case 'w':
                case 'x':
                case 'y':
                case 'z':  //以字母开头字符串
                    while(letter()||digit()){
                        concatenation();
                        updateCharacter();
                    }
                    retract();
                    if(reserve() == 0){
                        System.out.println( "(id,"+token+")");
                    }
                    else
                        System.out.println( "("+reserved[reserve()]+",null)");
                    break;
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':  //以数字开头
                    while(digit()){
                        concatenation();
                        updateCharacter();
                    }
                    retract();
                    System.out.println("(num,"+token+")");
                    break;
                case '+':
                    System.out.println("(+,null)");
                    break;
                case '-':
                    System.out.println("(-,null)");
                    break;
                case '*':
                    System.out.println("(*,null)");
                    break; 
                case '<':
                    updateCharacter();
                    if(character == '=')
                        System.out.println("(<=,null)");
                    else{
                        retract();
                        System.out.println("(<,null)");
                    }
                    break;
                case '=':
                    updateCharacter();
                    if(character == '=')
                        System.out.println("(==,null)");
                    else{
                        retract();
                        System.out.println("(=,null)");
                    }
                    break;
                case ';':
                    System.out.println("(;,null)");
                    break;
                case '#':
                    break;
                default:
                    System.out.println("出现非法字符:"+character);
                    break;
            }
        }while(character!='#');
    }  //产生词法分析结果
}
发布了18 篇原创文章 · 获赞 23 · 访问量 1397

猜你喜欢

转载自blog.csdn.net/qq_44329476/article/details/102536820
今日推荐