zoukankan      html  css  js  c++  java
  • 0917 词法分析程序(java版)

    1.运行结果:

    2.源代码:

    package 词法分析;
    import java.util.Scanner;
    public class fenxi {public static void main(String[] args) {   //主函数

    Scanner scanner=new Scanner(System.in);
    int k=0,i=0;
    String[] word=new String[20];
    word[0]="";
    System.out.println("请输入一个程序(以'#'结束):");
    String sent=scanner.nextLine();
    // System.out.println(sent);
    judgeType(k, i, word, sent);//判断分析组成类型
    justOne(word);//只有一个英文字母的特殊情况统一为"l(l|d)*"
    outPut(word);
    }

    private static void justOne(String[] word) {//判断是否有单个字母的特殊情况
    int i = 0,k = 0;
    while(word[i]!= null){
    if(word[i]!=""){
    if(Character.isLetter(word[i].charAt(0))&&word[i].length()==1){
    word[i]="l(l|d)*";
    k++;
    if(k>1)
    word[i]="NULL";//不要重复的
    }
    }
    i++;
    }
    }

    private static void outPut(String[] word) { //对应种别码,并输出列表
    int i = 0;
    System.out.println("单词符号 "+"种别码");
    while(word[i]!=null){
    switch(word[i]){
    case "begin":
    System.out.println(word[i]+" "+"1");
    break;
    case "if":
    System.out.println(word[i]+" "+"2");
    break;
    case "then":
    System.out.println(word[i]+" "+"3");
    break;
    case "while":
    System.out.println(word[i]+" "+"4");
    break;
    case "do":
    System.out.println(word[i]+" "+"5");
    break;
    case "end":
    System.out.println(word[i]+" "+"6");
    break;
    case "l(l|d)*":
    System.out.println(word[i]+" "+"7");
    break;
    case "dd*":
    System.out.println(word[i]+" "+"8");
    break;
    case "+":
    System.out.println(word[i]+" "+"9");
    break;
    case "-":
    System.out.println(word[i]+" "+"10");
    break;
    case "*":
    System.out.println(word[i]+" "+"11");
    break;
    case "/":
    System.out.println(word[i]+" "+"12");
    break;
    case ":":
    System.out.println(word[i]+" "+"13");
    break;
    case ":=":
    System.out.println(word[i]+" "+"14");
    break;
    case "<":
    System.out.println(word[i]+" "+"15");
    break;
    case "<=":
    System.out.println(word[i]+" "+"16");
    case "<>":
    System.out.println(word[i]+" "+"17");
    break;
    case ">":
    System.out.println(word[i]+" "+"18");
    break;
    case ">=":
    System.out.println(word[i]+" "+"19");
    break;
    case "=":
    System.out.println(word[i]+" "+"20");
    break;
    case ";":
    System.out.println(word[i]+" "+"21");
    break;
    case "(":
    System.out.println(word[i]+" "+"22");
    break;
    case ")":
    System.out.println(word[i]+" "+"23");
    break;
    case "#":
    System.out.println(word[i]+" "+"24");
    break;
    case "for":
    System.out.println(word[i]+" "+"25");
    break;
    }
    i++;
    }
    }

    private static void judgeType(int k, int i, String[] word, String sent) {
    while(sent.charAt(k)!='#'){
    word[i]="";
    while(Character.isLetter(sent.charAt(k))){//是否为字母
    word[i]=word[i]+sent.charAt(k);
    k++;

    }
    i++;
    word[i]="";

    //是否为连续的运算符
    while((sent.charAt(k)=='<'||sent.charAt(k)=='>'||sent.charAt(k)=='='||sent.charAt(k)==':')&&(sent.charAt(k+1)=='<'||sent.charAt(k+1)=='>'||sent.charAt(k+1)=='='||sent.charAt(k+1)==':')){
    word[i]=""+sent.charAt(k)+sent.charAt(k+1);
    i++;
    k=k+2;
    word[i]="";
    }

    //是否为单个符号或运算符
    while(sent.charAt(k)=='('||sent.charAt(k)==')'||sent.charAt(k)==';'||sent.charAt(k)==':'||sent.charAt(k)=='+'||sent.charAt(k)=='-'||sent.charAt(k)=='*'||sent.charAt(k)=='/'||sent.charAt(k)=='<'||sent.charAt(k)=='>'||sent.charAt(k)=='='){
    word[i]=""+sent.charAt(k);
    k++;
    i++;
    word[i]="";
    }


    while(sent.charAt(k)==' '){//是否为空格
    // System.out.println("space");
    word[i]="NULL";
    k++;
    i++;
    word[i]="";
    }

    while(Character.isDigit(sent.charAt(k))){//是否为数字
    word[i]="dd*";
    k++;
    i++;
    word[i]="";
    }

    }

    word[i]=""+'#';
    // System.out.println("跳出循环"+word[i]+i);
    }
    }

  • 相关阅读:
    想用Nginx代理一切?行!
    [SuProxy]Ngnix+Lua 实现SSH2,LDAP,ORACLE,SQLSERVER等TCP/IP协议分析,劫持,代理,会话及负载
    hive分区表详细介绍
    hive 中自定义UDF函数和自定义UDTF函数
    yarn工作原理
    HDFS小文件问题
    HDFS读写流程
    利用 canvas 实现签名效果
    idea里面自带的翻译插件
    idea的set,get插件
  • 原文地址:https://www.cnblogs.com/liangzhilin/p/4856939.html
Copyright © 2011-2022 走看看