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

📄 sentences.java

📁 自己写的一个用java实现的一个语法分析器
💻 JAVA
字号:
package Syntax;
//项目集
public class Sentences {
	private int current=0;//当前产生式个数
	private int max=10;//每个项目集中,最多产生式个数
	private int increment=5;
	public Sentence[] sentences=new Sentence[max];
	private Characters Symbols;
	private void increase()
	{
		int temp=max;
		max+=increment;
		Sentence[] s=new Sentence[max];
		for(int i=0;i<temp;i++)
		{
			s[i]=sentences[i];
		}
		sentences=s;
	}
	public void add(Sentence s)
	{
		if(current==max)increase();
		sentences[current++]=s;
	}
	private Sentence getSentence()
	{
		return sentences[current-1];
	}
	public Sentence getSentences(int i)
	{
		return sentences[i];
	}
	public int getCurrent()
	{
		return this.current-1;
	}
	private void clear()
	{
		current=0;
		sentences=new Sentence[max];
	}
	public Queue findSentence(int left)
	{
		Queue q=new Queue();
		for(int i=1;i<current;i++)
		{
			if(left==sentences[i].getLeft())
			{
				q.insert(i);
			}
		}
		return q;
	}
	public String showSingle(int i,int d)
	{
		String show="";
		int index=sentences[i].getLeft();
		
		show+=Symbols.getCharacter(index);
		if(i==0)show+="'";
		show+="->";
		int[] right=sentences[i].getRight();
		for(int j=0;right[j]!=-1;j++)
		{
			if(d==j)show+=".";
			index=right[j];
			show+=Symbols.getCharacter(index);
		}
		show+="\n";	
		
		return show;
	}
	public String showAll()
	{
		String show="";
		/*for(int i=0;i<current;i++)
		{
			show+="("+i+")"+sentences[i].getLeft()+"->";
			int[] right=sentences[i].getRight();
			for(int j=0;right[j]!=-1;j++)
			{
				show+=right[j];
			}
			show+="\n";	
		}*/
		int index=sentences[0].getLeft();
		int[] right;
		show+="(0)"+Symbols.getCharacter(index)+"'->"+Symbols.getCharacter(index)+"\n";
		for(int i=1;i<current;i++)
		{
			index=sentences[i].getLeft();
			show+="("+i+")"+Symbols.getCharacter(index)+"->";
			right=sentences[i].getRight();
			for(int j=0;right[j]!=-1;j++)
			{
				index=right[j];
				show+=Symbols.getCharacter(index);
			}
			show+="\n";	
		}
		return show;
	}
	public String initialize(String str,Characters characters)
	{
		int i=0;
		int index=-2;//产生式符号在字符集中的索引值
		int length=str.length();
		String error="";
		Symbols=characters;//字符集
		Character c=new Character();
		Sentence s=new Sentence();
		s.setLeft(Symbols.partition+1);
		s.setRight(Symbols.partition+1);
		add(s);//加入扩展生成式
		while(i<length)
		{
			c.value=str.charAt(i)+"";
			s=new Sentence();//生成下一个产生式
			if(c.value.equals(" ")|| c.value.equals("\n"))i++;//跳过空格和回车
			else
			{
			    index=Symbols.find(c);//产生式的字符是否合法
			    if(index!=-1 && index>Symbols.partition)//合法,生成产生式左部
			    {
			    	s.setLeft(index);
			    }
			    else
			    {
			    	error="输入的产生式左部发现错误,不存在非终结符'"+c.value+"'!";
			    	return error;
			    }
			    if(++i<length)c.value=str.charAt(i)+"";
			    else
			    {
			    	error="输入的产生式无右部!";
			    	return error;
			    }
			    if(c.value.equals("\n") || c.value.equals(" "))//右部前不能含有空格或回车
			    {
		    	    error="输入的产生式无右部!";
		    	    return error;
		        }
			    if(str.charAt(i)!='-')//略过中间箭头
			    {
			    	error="输入的产生式找不到右部!"+c.value+'1';
			    	return error;
			    }
			    if(++i<length)c.value=str.charAt(i)+"";
			    else
			    {
			    	error="输入的产生式找不到右部!";
			    	return error;
			    }
			    if(c.value.equals("\n") || c.value.equals(" "))//右部前不能含有空格或回车
			    {
		    	    error="输入的产生式找不到右部!";
		    	    return error;
		        }
			    if(str.charAt(i)!='>')
			    {
		    	    error="输入的产生式找不到右部,请用->区分左右部!";
		    	    return error;
		        }
			    if(++i<length)c.value=str.charAt(i)+"";
			    else
			    {
			    	error="输入的产生式找不到右部!";
			    	return error;
			    }
			    if(c.value.equals("\n") || c.value.equals(" "))//右部前不能含有空格或回车
			    {
		    	    error="输入的产生式找不到右部!";
		    	    return error;
		        }
			    while(i<length && str.charAt(i)!='\n' && str.charAt(i)!=' ' )//开始初始化右部
			    {
			    	c.value=str.charAt(i)+"";
			        index=Symbols.find(c);
			        if(index!=-1)
			        {
			    	    s.setRight(index);i++;
			        }
			        else
			        {
			    	    error="输入的产生式右部发现错误字符'"+c.value+'\'';
			    	    return error;
			        }
			    }
			    add(s);//将该产生式添加到产生式集中
			}
		}
		/*for(int k=0;k<Symbols.current;k++)
			error+=k+Symbols.getCharacter(k)+'\n';
		for(int k=0;k<str.length();k++)
		    error+="\n'"+k+"':'"+str.charAt(k)+"'\n";*/
		//error+=showAll();
		return error;
	}
	public void closure()
	{
		
	}
	public void go(Sentences I,Character c)
	{
		
	}
	
}

⌨️ 快捷键说明

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