lexical Analyser(java)

语言与编译老师留的作业:

main:

package LexicalAnalyzer;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.io.FileNotFoundException;


public class main {
    public static void main(String []args) throws IOException {
        File file = new File("D:\\Desktop\\IDEA Project\\CW2LA\\test.txt");
        File file1 = new File("D:\\Desktop\\IDEA Project\\CW2LA\\test2.txt");
        run(file);
        System.out.println("Above is the output of the first file");
        run(file1);
        System.out.println("Above is the output of the first file");
    }

    public static void run(File file) throws FileNotFoundException {
        int line = 0;
        int flag1 = 0;
        LABody test = new LABody();
        try (Scanner input = new Scanner(file)) {
            while (input.hasNextLine()) {
                line++;
                String str = input.nextLine();
                if (str.contains("/*")) {
                    flag1 = 1;
                }
                if (flag1 == 0) {
                    test.identify(str.split("//")[0],line);
                }
                if (str.contains("*/")) {
                    flag1 = 0;
                }
            }
        }
    }
}

 LABody:

package LexicalAnalyzer;

import java.util.ArrayList;
import java.util.Collections;

/*
Definition Defines the types "public","private","protect","abstract","class","extends","final","implements","interface","static","void"
as a class definition, with the type "1"
"new","this" are the instantiation type identifiers, denoted by 2
"Break," "continue" and "return", "do" and "while" and "if", "else" and "for" and "instanceof", "switch", "case", "default", "goto" loop
judgment statements, to say with "3"
"Boolean" and "byte", "char", "double", "float", "int", "long", "short", "String", "null", "true", "false" type identity statements,
to say with "4"
"+", "-", "*", "/", "%", "&", "|", "^", "-", ">", "<", "=", "!" , ". ", "+ +", "-", "- =", "* =", "/ =", "< <", "> >", "> > >", "= =", "! = > ", "=", "< =", "&", "| |"
logical operation identifier, use "5"
", ", ";" ,":","(",")","{","}". separator, denoted by "7"
 */

public class LABody {

    static int point;

    static String []classkey = {"public","private","protect","abstract","class","extends",
            "final","implements","interface","static","void"};
    static String []methodskey = {"new","this"};
    static String []loopkey = {"break","continue","return","do","while","if","else","for",
            "instanceof","switch","case","default","goto"};
    static String []typekey = {"boolean","byte","char","double","float","int","long","short","String","null","true","false"};
    static String []symblekey = {"+","-","*","/","%","&","|","^","~",">","<","=","!",".","++",
            "--","-=","*=","/=","<<",">>",">>>","==","!=",">=","<=","&&","||"};
    static String []delimiterkey = {",",";",":","(",")","{","}"};
    static ArrayList<String> classKey=null;
    static ArrayList<String> methodKey=null;
    static ArrayList<String> loopKey=null;
    static ArrayList<String> typeKey=null;
    static ArrayList<String> symbleKey=null;
    static ArrayList<String> delimiterKey=null;

    public LABody(){
        classKey = new ArrayList<String>();
        methodKey = new ArrayList<String>();
        loopKey = new ArrayList<String>();
        typeKey = new ArrayList<String>();
        symbleKey = new ArrayList<String>();
        delimiterKey = new ArrayList<String>();
        Collections.addAll(classKey, classkey);
        Collections.addAll(methodKey, methodskey);
        Collections.addAll(loopKey, loopkey);
        Collections.addAll(typeKey, typekey);
        Collections.addAll(symbleKey, symblekey);
        Collections.addAll(delimiterKey, delimiterkey);
    }

    /*
    Determine the type of token by the first letter of the word
     */
    public static void identify(String str,int line){
        point=0;
        char ch;
        str=str.trim();
        for (;point<str.length(); point++){
            ch=str.charAt(point);
            if (checkNum(ch)){
                NumIdentity(str,1,line);
            }else if(ch == '-'){
                point++;
                NumIdentity(str,0,line);
            }
            else if (checkLettle(ch)||ch=='_'){
                charIdentity(str);
            }else if (ch=='"'){
                stringIdentity(str,line);
            }
            else if (ch==' '){
                continue;
            }else {
                symbolIdentity(str,line);
            }
        }
    }

    /*
    After judging that the token is an identifier type, and judging which identifier is output token
     */
    public static void charIdentity(String str){
        String token ="";
        char unit;
        for(;point<str.length();point++){
            unit = str.charAt(point);
            if(!checkLettle(unit)&&unit!='_'){
                break;
            }else{
                token = token + unit;
            }
        }//for
        if(classKey.contains(token)){
            System.out.print("<"+1+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");

        }
        else if(methodKey.contains(token)){
            System.out.print("<"+2+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");
        }
        else if(loopKey.contains(token)){
            System.out.print("<"+3+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");
        }
        else if(typeKey.contains(token)){
            System.out.print("<"+4+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");
        }
        else{
            System.out.print("<"+8+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");
        }
        if (point!=str.length()-1||(point==str.length()-1&&!Character.isDigit(str.charAt(point)))){
            point--;
        }

    }//charIdentity

    /*
    A numeric identifier that determines whether it is signed or unsigned and can output an error
    The signed number is 10 and the unsigned number is 9
    Judge the numbers
     */
    public static void NumIdentity(String str, int signal,int line){
        String token ="";
        int flag=0;
        boolean err=false;
        char unit;
        for (;point<str.length();point++) {
            unit = str.charAt(point);
            if (unit==' '||(!checkLettle(unit)&&!checkNum(unit)&&unit!='.')){
                break;
            }
            else if(unit == '.'){
                flag ++;
                if(flag == 1){
                    token = token+unit;
                }
                else{
                    err = true;
                    token = token+unit;
                }
            }
            else if(checkLettle(unit)){
                err = true;
                token = token + unit;
            }
            else{
                token = token+unit;
            }
        }
        if (err){
            System.out.println(line+"line"+": "+token+" is not in a correct form");
        }
        else{
            if(signal==1){
                System.out.print("<"+9+", ");
                System.out.printf("%-10s",token);
                System.out.println(">");}
            else{
                System.out.print("<"+10+", -");
                System.out.printf("%-8s",token);
                System.out.println(">");
            }
        }
        if (point!=str.length()-1||(point==str.length()-1&&!Character.isDigit(str.charAt(point)))){
            point--;
        }
    }

    /*
    Symbol identifier
     */
    public static void symbolIdentity(String str,int line){
        String token ="";
        token= String.valueOf(str.charAt(point));
        char unit;
        if (delimiterKey.contains(token)) {
            System.out.print("<"+7+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");
        }
        else {
            if (symbleKey.contains(token)){
                if (point<str.length()){
                    point++;
                    unit=str.charAt(point);
                    if (symbleKey.contains(token+unit)){
                        token+=unit;
                        point++;
                        if (point<str.length()){
                            unit=str.charAt(point);
                            if (symbleKey.contains(token+unit)){
                                token+=unit;
                                System.out.print("<"+5+", ");
                                System.out.printf("%-10s",token);
                                System.out.println(">");
                            }else{
                                point--;
                                System.out.print("<"+5+", ");
                                System.out.printf("%-10s",token);
                                System.out.println(">");
                            }
                        }else{
                            System.out.print("<"+5+", ");
                            System.out.printf("%-10",token);
                            System.out.println(">");
                        }
                    }else {
                        point--;
                        System.out.print("<"+5+", ");
                        System.out.printf("%-10s",token);
                        System.out.println(">");
                    }
                }
            }else {
                point--;
                System.out.println(line+"line"+": "+token+" is not in a correct form");
            }
        }
    }//symbolIdentity

    /*
    String identifier
     */
    public static void stringIdentity(String str,int line){
        String token = "";
        char unit;
        for(;point<str.length();point++){
            unit=str.charAt(point);
            token+=unit;
            if (unit=='"'){
                break;
            }
        }
        if(token.charAt(token.length()-1)!='"'){
            System.out.println(line+"line"+": "+token+" is not in a correct form");
        }else{
            System.out.print("<"+6+", ");
            System.out.printf("%-10s",token);
            System.out.println(">");
        }

    }

    /*
    Determine whether it is an English letter
     */
    public static boolean checkLettle(char data){
        if(((data >= 'a'&& data<= 'z')||(data>='A'&&data<='Z'))){
            return true;
        }
        else{
            return false;
        }
    }//checkLettle
    /*
    Determine if it's a number
     */
    public static boolean checkNum(char num){
        if((num >= '0'&& num<= '9')){
            return true;
        }
        else{
            return false;
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值