⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanner.java

📁 编译器
💻 JAVA
字号:
package compiler;

public class Scanner 
{
	public static int index;//index为目前要分析单词在源程序中的起始位置;
	public static boolean if_variable_assigned_value;//判断变量声明是否赋值
	public static int variable_value;
    public char nextch;//当前读入字符;
	public Token token;
	public StringBuffer word;//存储当前读入的单词;
	
    public Scanner()
    {
    	index = 0;
    	if_variable_assigned_value = false;
    	variable_value = 0;
    	nextch = '?';
    	token = new Token();
    	word = new StringBuffer();
    }
	
	
	
	//词法分析,返回单词的属性字
	public Token lexAnalyze(String source ,SymbolTable symbolTable)
	{
		//如果所有字符已经读完
		if(index >= source.length())
		{
			token.setTypeAndVal(999,"overflow");
		}
		
        //如果尚有字符没有读完
		else
		{
			//每次新生成一个word,以保证word里的内容是新的
			word = new StringBuffer();
			
			//读单词的第一个字符;
			nextch = source.charAt(index);
			
			//当单词的第一个字符是字母,即词法分析被期望得到标识符
			if(nextch >= 'a' && nextch <= 'z')
			{
				while(nextch >= 'a' && nextch <= 'z' 
					  || nextch >= '1' && nextch <= '9')
				{
					word = word.append(nextch);
					index++;
					nextch = source.charAt(index);
				}
				//判断是否为关键字
	            if(word.toString().equals("int"))
				{
					token.setTypeAndVal(0,"int");
				}
				else if(word.toString().equals("if"))
				{
					token.setTypeAndVal(1,"if");
				}
				else if(word.toString().equals("then"))
				{
					token.setTypeAndVal(2,"then");
				}
				else if(word.toString().equals("else"))
				{
					token.setTypeAndVal(3,"else");
				}
				else if(word.toString().equals("while"))
				{
					token.setTypeAndVal(4,"while");
				}
				else if(word.toString().equals("do"))
				{
					token.setTypeAndVal(17,"do");
				}//判断是否为关键字
				
				//不是关键字,即是变量名:
				else 
				{
					token.setTypeAndVal(15,word.toString());
					
					//如果符号表中已有该变量,即不是第一次定义,则不需判断是否初始化
					if(!symbolTable.contains(word.toString()))
					{
						//用来判断声明的变量是否赋初值,填入符号表时用
						for(int i = 0 ; i < source.length() ; i ++)
						{
							nextch = source.charAt(index + i);
							
							//过滤掉变量名后面的空格、TAB、回车符
							if(nextch == ' ' || nextch == '	' || nextch == '\n')
							{
								continue;
							}
							//遇到=号说明变量由初始值
							else if(nextch == '=')
							{
								StringBuffer word2 = new StringBuffer();
								int index2 = index + i + 1;
								nextch = source.charAt(index2);
								
								while(nextch == ' ' || nextch == '	' || nextch == '\n')
								{
									nextch = source.charAt(index2++);
								}
								while(Character.isDigit(nextch))
								{
									word2 = word2.append(nextch);
									index2++;
									nextch = source.charAt(index2);
								}
								
								if_variable_assigned_value = true;
								variable_value = Integer.parseInt(word2.toString());
								break;
							}
							//如果没遇到=号就遇到其他符号,说明变量没有初始化
							else
							{
								if_variable_assigned_value = false;
								variable_value = 0;
								break;
							}
						}//用来判断声明的变量是否赋初值,填入符号表时用
					}
				}//不是关键字,即是变量名:
			}//当单词的第一个字符是字母,即词法分析被期望得到标识符
			
			else if(nextch >= '0' && nextch <= '9')//当单词的第一个字符是数字,即词法分析被期望得到常量
			{
				while(nextch >= '0' && nextch <= '9')
				{
					word = word.append(nextch);
					index++;
					nextch = source.charAt(index);
				}
			    token.setTypeAndVal(16,word.toString());
			}
			
	        //既不是变量也不是常量则被期望得到操作符或分界符
			else if(nextch == '{')
			{
				token.setTypeAndVal(7,"{");index++;
			}
			else if(nextch == '}')
			{
				token.setTypeAndVal(8,"}");index++;
			}
			else if(nextch == '(')
			{
				token.setTypeAndVal(5,"(");index++;
			}
			else if(nextch == ')')
			{
				token.setTypeAndVal(6,")");index++;
			}
			else if(nextch == '+')
			{
				token.setTypeAndVal(9,"+");index++;
			}
			else if(nextch == '-')
			{
				token.setTypeAndVal(10,"-");index++;
			}
			else if(nextch == '*')
			{
				token.setTypeAndVal(11,"*");index++;
			}
			else if(nextch == '/')
			{
				token.setTypeAndVal(12,"/");index++;
			}
			else if(nextch == '=')
			{
				token.setTypeAndVal(14,"=");index++;
			}
			else if(nextch == ';')
			{
				token.setTypeAndVal(13,";");index++;
			}
			else if(nextch == ' ' || nextch == '	' || nextch == '\n')//词法分析被期望得到空格
			{
				token.setTypeAndVal(25,"SPACE");index++;
			}
			else if(nextch == '#')//词法分析被期望得到结束符
			{
				token.setTypeAndVal(18,"#");index++;
			}
	        
			//以上都不是,词法分析检查出词法错误
			else
			{
				token.setTypeAndVal(400,"ERROR");
			}
		}//else如果尚有字符没有读完
		return token;
	}//lexAnalyze方法
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -