编译原理实验一:简单词法分析程序设计(Java)

根据PASCAL语言的说明语句结构,设计一个对PASCAL语言常量说明语句进行词法分析的简单程序,并用C、C++、Java或Python语言编程实现。要求程序能够对从键盘输入或从文件读入的形如“const count = 10, sum = 81.5, char1 = 'f', string1 = "abcds f 89h", max = 169;”的字符串进行分析处理,如果是,识别出该输入串中所说明的各种常量名、常量类型及常量值,以二元组的形式输出所有常量,并统计各种类型的常量个数。

代码实现如下:

public class exercise {
    static String name;
    static String type;
    static String errormessage;//错误信息
    static String value;
    static int intnum = 0;
    static int floatnum = 0;
    static int charnum = 0;
    static int stringnum = 0;
    static int correctname;//0 = error,1 = correct
    static int correctvalue;//0 = error,1 = correct
    public static void main(String args[]) {
         Scanner in = new Scanner(System.in);
         System.out.println("请输入一个字符串:");
         String s = in.nextLine();
         s = s.trim();
         boolean re = s.startsWith("const") && s.endsWith(";");
         while(!re)
         {
             System.out.println("It is not a constant declaration statement!");
             System.out.println("Please input a string again!");
             s = in.nextLine();
             s = s.trim();
             re = s.startsWith("const") && s.endsWith(";");
         }
         Output(s);
         in.close();
         }
    //判断常量名
    public static int checkname(char[] a,int i)
    {
        name = "";
        while(a[i] != '=') {
            name += a[i];
            i++;
        }
        name = name.trim();
        String regex = "[a-zA-Z][a-zA-Z0-9_]*";
        boolean re = name.matches(regex);//检测字符串是否匹配给定正则表达式,匹配返回真,否则返回假
        if(re)
        {
            correctname = 1;
        }
        else
        {
            correctname = 0;
        }
        System.out.println(i);
        return i;
       
    }

   
    //判断类型
    public static int checktype(char a[],int i)
    {
        errormessage = "";
        value = "";
        while(a[i] != ',' && a[i] != ';') {
            value += a[i];
            i++;
        }
        value = value.trim();
        String regexr1 = "[+|-]?[0-9]*";
        boolean va = value.matches(regexr1);//检测字符串是否匹配给定正则表达式,匹配返回真,否则返回假
        if(correctname == 1)
        {
            String str = value;
            if(va) {//判断整数
                if(value.charAt(0)=='+' || value.charAt(0)=='-')//检索value是否以+或—开头,即正数或负数
                {
                    str = value.substring(1);//从下标为1开始截取后面字符串
                }
                if(str.equals("0") || str.matches("[1-9][0-9]*"))//判断整数是否合法
                {
                    correctvalue = 1;
                    type = "integer";//整数
                    intnum++;
                }else {
                    errormessage = "Wrong!The integer can't be started with '0'.";//报错,整数不可以0开头
                    correctvalue = 0;
                }
            }
                else if(value.matches("[+|-]?[0-9]*[.][0-9]+")||value.matches("[+|-]?[0-9]*[.][0-9]+e[+|-]?[0-9]*")) {//判断浮点数
                    correctvalue = 1;
                    type = "float";
                    floatnum++;
                }
                else if(value.startsWith("\'")&&value.endsWith("\'")) {//判断字符型
                    if(value.length()==3) {
                        correctvalue = 1;
                        type = "char";
                        charnum++;
                    }else {
                        errormessage += "Wrong!There are more than one char in ''.";
                        correctvalue = 0;
                    }
                }
                else if(value.startsWith("\"")&&value.endsWith("\"")) {
                    correctvalue = 1;
                    type = "String";
                    stringnum++;
                }
                else {
                    correctvalue = 0;
                    errormessage += "Wrong!The format of the value string is not correct!";
                }
            }
        return i;
        }
        
    
    static void Output(String s) {
        // TODO Auto-generated method stub
        char str[] = s.toCharArray();
        int i = 5;
        while(i<str.length - 1) {
            i = checkname(str,i);
            i = checktype(str,i+1)+1;
            if(correctname == 1) {
                if(correctvalue==1) {
                    System.out.println(name+"("+type+","+value+")");//输入合法
                }
                else {
                    System.out.println(name+"("+errormessage+")");//名对值错
                }
            }
            else {
                System.out.println("("+name+","+"Wrong!It is not a identifier!"+")");
            }
        }
        System.out.println("int_num="+intnum+"; float_num="+floatnum+"; char_num="+charnum+"; string_num="+stringnum+".");
    }
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值