编译原理-词法分析器的java实现

编译原理-词法分析器的java实现

  • 实验步骤
    • 编程环境
    • 实验步骤
      • 1. 构建DFA
      • 流程图
      • 3. 代码实现分析
      • 4.编号规定
      • 5.运行结果

实验步骤

编程环境

eclipse开发软件

实验步骤

1. 构建DFA

编译原理-词法分析器的java实现_第1张图片

流程图

编译原理-词法分析器的java实现_第2张图片

3. 代码实现分析

  1. 给标识符、常量等给定一个标识符
  2. 给定一个字符串,将字符串转换为字符数组
  3. 判断当前读入的字符是什么字符
  4. 根据DFA来判断和输出

4.编号规定

001:加法运算符 +
002:减法运算符 -
003:乘法运算符 *
004:除法运算符 /
005:左括号 (
013:右括号 )
008.标识符
010.整数
011.等号运算符 ==
012.浮点数
014:错误字段 error
015:赋值运算符 =
016:大于号 >
017:小于号 <
018:不等于号 !=
019:小于等于号 <=
020:大于等于号 >=
021:{ 左中括号 {
022:} 右中括号 }
023: ; 分号 ;

import java.util.*;

/*
编号规定:
001:加法运算符
002:减法运算符
003:乘法运算符
004:除法运算符
005:左括号
013:右括号	
008.标识符
010.整数
011.等号运算符
012.浮点数
014:错误字段
015:赋值运算符
016:大于号
017:小于号 
018:不等于号
019:小于等于号
020:大于等于号
021:{ 左中括号
022:} 右中括号
023: ;	分号
*/
public class lexicalAnalysis {
     
    private static char[] Code;//转化为字符数组
    public static ArrayList<String> tokens=new ArrayList<>();//用于之后的语法分析传值使用

    public static void main(String[] args) {
      
//        String s = "public static void main(String[] args) { 
//      String s = "abc2@e=num2+12.3.23)*123-12.#;#";//输入符号串
//        String s = "public static void main(String[] args) { \r\n" + 
//        		"        String s = \"abc2@e=num2+12.3.23)*123-12.#\";#";//表达式为A1=1.2+3*(4-6/3)
    	String s = "vhguiyv7=-okllpl,-=popl'>=><==#";
    	
        StartLexicalAnalysis(s);
    }

    public static void StartLexicalAnalysis(String s) {
     //开始进行语法分析
        if (IsCode(s)) {
     //判断
        	tokens.add("begin") ;
        System.out.println("当前输入符号为:" + s.substring(0,s.length()-1));
        System.out.println("词法分析结果如下:");
        StartState(0, 0);
        }
    }
    private static boolean IsCode(String s){
     //判断源代码是否为空,为空则不能进行词法分析
        if (s.isEmpty()||s.toCharArray()[0]=='#') {
     
            System.out.println("源代码为空,无法进行词法分析!");
            return false;
        }
        else {
     
            Code = s.toCharArray();
            return true;
        }

    }

    private static void StartState(int basicPointer, int currentPointer)//状态0,初始状态
    {
     

        if (Code[currentPointer] == '#') {
     
            System.out.println("词法分析结束");
            tokens.add("end") ;
        }
        else if (IsEqual(Code[currentPointer]))//如果当前字符是等于号进入EqualState
        {
     
            EqualState(currentPointer,basicPointer);
        }
        else if (IsAdd(Code[currentPointer]))//如果当前字符是加号进入AddState
        {
     
            AddState(currentPointer);
        }
        else if (IsSub(Code[currentPointer]))//如果当前字符是减号进入SubState
        {
     
            SubState(currentPointer);
        }
        else if (IsMul(Code[currentPointer]))//如果当前字符是乘号进入MulState
        {
     
            MulState(currentPointer);
        }
        else if (IsDiv(Code[currentPointer]))//如果当前字符是除号进入DivState
        {
     
            DivState(currentPointer);
        }
        else if (IsLParent(Code[currentPointer]))//如果当前字符是左括号进入LParentState
        {
     
            LParentState(currentPointer);
        }
        else if (IsRParent(Code[currentPointer]))//如果当前字符是右括号进入RParentState
        {
     
            RParentState(currentPointer);
        }

        else if (IsAlpha(Code[currentPointer]))//如果当前字符是字母号进入IdentState
        {
     
            IdentState(basicPointer, currentPointer + 1);//是字母就进入状态6
        }
        else if (IsDigit(Code[currentPointer]))//如果当前字符是数字进入IntState
        {
     
            IntState(basicPointer, currentPointer + 1);//是数字就进入状态7
        }
        else if ( IsGreater(Code[currentPointer]) ) 
        {
      //如果当前字符是大于号
			GreaterState(basicPointer, currentPointer + 1);
		}
        else if ( IsLess(Code[currentPointer]) )
        {
      //如果当前字符是小于号
			LessState(basicPointer, currentPointer + 1);
		}
        else if ( IsExc(Code[currentPointer]) )
        {
      //如果当前字符是!
			ExcState(basicPointer, currentPointer + 1);
		}
        else if ( IsLZKH(Code[currentPointer]) )
        {
      //如果当前字符是{
     
        	LZKHState(basicPointer, currentPointer);
		}
        else if ( IsRZKH(Code[currentPointer]) )
        {
      //如果当前字符是}
        	RZKHState(basicPointer, currentPointer);
		}
        else if ( IsFH(Code[currentPointer]) )
        {
      //如果当前字符是;
        	FHState(basicPointer, currentPointer);
		}
        else {
     

            System.out.println("(014,错误," + Code[currentPointer] + ")");
            String tempSt="014,"+Code[currentPointer];
            tokens.add(tempSt);
            StartState(basicPointer + 1, currentPointer + 1);
        }
    }

    
    private static void FHState(int basicPointer, int j) {
     
    	System.out.println("(023,分号," + Code[j] + ")");
        String tempSt="023,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
	}

	private static void RZKHState(int basicPointer, int j) {
     
		System.out.println("(022,右中括号," + Code[j] + ")");
        String tempSt="022,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
	}

	private static void LZKHState(int basicPointer, int j) {
     
		System.out.println("(021,左中括号," + Code[j] + ")");
        String tempSt="021,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
	}

	private static void ExcState(int basicPointer, int j) {
     
    	if ( Code[j] == '=' ) {
     
			ExcState(basicPointer,j+1);
		}else {
     
			if ( j - basicPointer == 2 ) {
     
				System.out.print("(018,不等于号,");
				String tempSt = "!" ;
				printA(basicPointer,j,tempSt);
			}else {
     
				System.out.print("(014,错误," );
	            String tempSt="014,";
	            printA(basicPointer,j,tempSt);
			} 
		}
	}
 
	private static void LessState(int basicPointer, int j) {
     
		if ( Code[j] == '=' ) {
     
			LessState(basicPointer,j+1);
		}else {
     
			if ( j - basicPointer == 2 ) {
     
				System.out.print("(019,小于等于号,");
				String tempSt = "<" ;
				printA(basicPointer,j,tempSt);
			}else if (j - basicPointer == 1) {
     
				System.out.print("017,小于号," + Code[j-1] + ")");
			    String tempSt="017,"+Code[j-1];
		        tokens.add(tempSt);
		        StartState(j + 1, j + 1);
			}
			else {
     
				System.out.print("(014,错误," );
	            String tempSt="014,";
	            printA(basicPointer,j,tempSt);
			} 
		}
	}

	private static void GreaterState(int basicPointer, int j) {
     
		if ( Code[j] == '=' ) {
     
			GreaterState(basicPointer,j+1);
		}else {
     
			if ( j - basicPointer == 2 ) {
     
				System.out.print("(020,大于等于号,");
				String tempSt = ">" ;
				printA(basicPointer,j,tempSt);
			}else if (j - basicPointer == 1) {
     
				System.out.println("(016,大于号," + Code[j-1] + ")");
			    String tempSt="016,"+Code[j-1];
		        tokens.add(tempSt);
		        StartState(j , j + 1);
			}
			else {
     
				System.out.print("(014,错误," );
	            String tempSt="014,";
	            printA(basicPointer,j,tempSt);
			} 
		}
	}

	private static void EqualState(int j, int basicPointer)//表示字符为等号运算符
    {
       
    	if ( IsEqual(Code[ j+1 ]) ) {
     
			EqualState(j+1, basicPointer);
		}else {
     
			if ( j - basicPointer == 1 ) {
     
				System.out.print("(011,等于号,");
				String tempSt="011," ;
				printA(basicPointer,j+1,tempSt);
			}else if (j - basicPointer == 0) {
     
				//赋值运算符
				System.out.println("(015,赋值运算符," + Code[j] + ")");
			    String tempSt="015,"+Code[j];
		        tokens.add(tempSt);
		        StartState(j + 1, j + 1);
			}else {
     
				//错误代码
				System.out.print("(014,错误," );
	            String tempSt="014,"; 
	            printA(basicPointer,j+1,tempSt); 
			}
		}
    	
//        System.out.println("(011,等于号," + Code[j] + ")");
//        String tempSt="011,"+Code[j];
//        tokens.add(tempSt);
//        StartState(j + 1, j + 1);
    }

    private static void AddState(int j)//表示字符为加法运算符
    {
     
        System.out.println("(001,加号," + Code[j] + ")");
        String tempSt="001,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
    }

    private static void SubState(int j)//表示字符为减法运算符
    {
     
        System.out.println("(002,减号," + Code[j] + ")");
        String tempSt="002,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
    }

    private static void MulState(int j)//表示字符为乘法运算符
    {
     
        System.out.println("(003,乘号," + Code[j] + ")");
        String tempSt="003,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
    }

    private static void DivState(int j)//表示字符为除法运算符
    {
     
        System.out.println("(004,除号," + Code[j] + ")");
        String tempSt="004,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
    }

    private static void LParentState(int j)//字符为左括号
    {
     
        System.out.println("(005,左括号," + Code[j] + ")");
        String tempSt="005,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
    }

    private static void RParentState(int j)//字符为右括号
    {
     
        System.out.println("(013,右括号," + Code[j] + ")");
        String tempSt="013,"+Code[j];
        tokens.add(tempSt);
        StartState(j + 1, j + 1);
    }

    private static void IdentState(int i, int j)//标识符
    {
     
        //Code[i]为准标识符的第一个字符,是个字母 ,而Code[j-1]是字母或数字
        if (IsDigit(Code[j]) || IsAlpha(Code[j]))
        //如果当前字符仍然为字母或数字则再次进入IdentState
        {
     
            IdentState(i, j + 1);
        } else//如果当前字符为非数字及字母字符,则表明Code[i]~Code[j-1]这一段是标识符
        {
     
            System.out.print("(008,标识符,");
            String tempSt="008,";
            printA(i, j, tempSt);
        }
    }

    private static void IntState(int i, int j)//状态7,准实数
    {
     
        //Code[i]为准实数的第一个字符,是个字母 ,而Code[j-1]是数字或小数点
        if (IsDigit(Code[j]))//如果当前字符仍然是数字,则再次进入IntState
        {
     
            IntState(i, j + 1);
        }
        if (Code[j] == '.')//如果当前字符是小数点,进入PointState
        {
     
            PointState(i, j + 1);
        }
        if ((Code[j] != '.') && !IsDigit(Code[j]))
        //如果当前字符既不为小数点也不是数字 ,则表明Code[i]~Code[j-1]这一段是个实数
        {
     
            System.out.print("(010,整数,");
            String tempSt="010,";
            printA(i, j, tempSt);
        }
    }

    private static void PointState(int i, int j)//整数后接个小数点的中间态
    {
     
        //Code[i]~Code[j-1]之中含有小数点
        if (!IsDigit(Code[j])) {
     //小数点后还是小数点或者不是数字时报错
            System.out.print("(014,错误,");
            String tempSt="014,";
            int q = i;//从Code[i]开始算起
            while ((IsDigit(Code[q])) || (Code[q]) == '.')
            //直接相连的数字或小数点都属于这个无效字段的一部分
            {
     
                tempSt=tempSt+Code[q];
                System.out.print(Code[q]);
                q++;
            }
            System.out.println(")");
            tokens.add(tempSt);
            //Code[q]此时为无效字段的下一个字符
            StartState(q, q);

        }
        if (IsDigit(Code[j]))//如果当前字符是数字,则进入FloatState
        {
     
            FloatState(i, j + 1);
        }
    }

    private  static void FloatState(int i,int j){
     

        if (IsDigit(Code[j]))//如果当前字符是数字,则再次进入FloatState
        {
     
            FloatState(i, j + 1);
        }
        if (!IsDigit(Code[j]))
        //如果当前字符是非数字字符,说明Code[i]~Code[j-1]这一段是浮点数
        {
     
            System.out.print("(012,浮点数,");
            String tempSt="012,";
            printA(i, j, tempSt);
        }
    }

    private static void printA(int i, int j, String tempSt) {
     //用于输出非单一字符
        for (int k = i; k < j; k++) {
     
            tempSt = tempSt + Code[k];
            System.out.print(Code[k]);
        }
        System.out.println(")");
        tokens.add(tempSt);
        StartState(j, j);
    }

    private static boolean IsEqual(char ch) {
     //判断是否是字符'='
        return ch == '=';
    }

    private static boolean IsAdd(char ch)//判断是否是字符'+'
    {
     
        return ch == '+';
    }

    private static boolean IsSub(char ch)//判断是否是字符'-'
    {
     
        return ch == '-';
    }

    private static boolean IsMul(char ch)//判断是否是字符'*'
    {
     
        return ch == '*';
    }

    private static boolean IsDiv(char ch)//判断是否是字符'/'
    {
     
        return ch == '/';
    }

    private static boolean IsLParent(char ch)//判断是否左括号
    {
     
        return ch == '(';
    }

    private static boolean IsRParent(char ch)//判断是否是有括号
    {
     
        return ch == ')';
    }

    private static boolean IsDigit(char ch) {
     //判断是否是数字
        return Character.isDigit(ch);
    }

    private static boolean IsAlpha(char ch) {
     //判断是否是字母
        return Character.isLetter(ch);
    }

	private static boolean IsExc(char ch) {
     
		return ch == '!';
	}

	private static boolean IsLess(char ch) {
     
		return ch == '<';
	}

	private static boolean IsGreater(char ch) {
     
		return ch == '>';
	}

	private static boolean IsFH(char ch) {
     
		return ch == ';';
	}

	private static boolean IsRZKH(char ch) {
     
		return ch == '}';
	}

	private static boolean IsLZKH(char ch) {
     
		return ch == '{';
	}
}

5.运行结果

编译原理-词法分析器的java实现_第3张图片
编译原理-词法分析器的java实现_第4张图片

源代码下载https://download.csdn.net/download/Simple__Code/12809398

你可能感兴趣的:(经验分享)