编译原理语法分析编译原理语法分析编译原理语法分析编译原理语法分析编译原理语法分析

源代码在线查看: test_2.java

软件大小: 2 K
上传用户: zhousiqi420
关键词: 编译原理
下载地址: 免注册下载 普通下载 VIP

相关代码

				package test_2;
				
				import java.io.IOException;
				
				class test_2 
				{
					//String s;
					char []prog=new char[80];
					StringBuffer token=new StringBuffer();//token缓冲区暂存读入的字符
					char ch;
					static int syn,p=0,sum=0,kk=0;
					String []rwtab={"begin","if","then","while","do","end"};
					void input()
					{
						System.out.println("输入源程序字符串,送到缓冲区:");
						try
							{
								int n=0;
								do
								{
									prog[n]=(char)System.in.read();
									ch=prog[n];
									n++;
								}while(ch!='#');
							}catch(IOException e){e.printStackTrace();}	
					}
				
					public static void main(String[] args)
					{
						test_2 job=new test_2();
						job.input();
						job.anaylse();
						job.lrparser();
						
					}
					void lrparser()
					{
						if(syn==1)
						{
							this.anaylse();
							this.yucu();
							if(syn==6)
							{
								this.anaylse();
								if(syn==0&&kk==0)
								{
									System.out.println("Success!");
								}
							}
							else
							{
								if(kk!=1)
									System.out.println("The error of losting end");
									kk=1;
							}
						}
						else
						{
							System.out.println("The error of losting begin");
							kk=1;
						}
					}
					void yucu()
					{
						this.statement();
						while(syn==26)
						{
							this.anaylse();
							this.statement();
						}
					}
					void statement()
					{
						if(syn==10)
						{
							this.anaylse();
							if(syn==18)
							{
								this.anaylse();
								this.expression();
							}
							else
							{
								System.out.println("赋值号错误");
								kk=1;
							}
						}
						else
						{
							System.out.println("语句错误");
							kk=1;
						}
					}
					void expression()
					{
						this.term();
						while(syn==13||syn==14)
						{
							this.anaylse();
							this.term();
						}
					}
					void term()
					{
						this.factor();
						while(syn==15||syn==16)
						{
							this.anaylse();
							this.factor();
						}
					}
					void factor()
					{
						if(syn==10||syn==11)
							this.anaylse();
						else if(syn==27)
						{
							this.anaylse();
							this.expression();
							if(syn==28)
								this.anaylse();
							else
							{
								System.out.println("')'错误");
								kk=1;
							}
						}
						else
						{
							System.out.println("表达式错误!");
							kk=1;
						}
					}
					void anaylse()
					{
						token.delete(0,token.length());		//清空token缓冲区
						ch=prog[p];							//读入一个字符
						while(ch==' '||ch=='\r'||ch=='\t'||ch=='\n')
							ch=prog[++p];
						if(Character.isLetter(ch))
						{
							while(Character.isLetterOrDigit(ch))
							{
								token.append(ch);
								ch=prog[++p];
							}
							p--;
							syn=10;
							for(int n=0;n							{
								if((token.toString()).equals(rwtab[n])==true)//缓冲区内容与关键字数组比较
								switch(n)
									{
									case 0:syn=1;break;
									case 1:syn=2;break;
									case 2:syn=3;break;
									case 3:syn=4;break;
									case 4:syn=5;break;
									case 5:syn=6;break;
									}
							}
						}
						else
							if(Character.isDigit(ch))
							{
								while(Character.isDigit(ch))
								{
									sum=sum*10+ch-'0';
									ch=prog[++p];
								}
								p--;							//缓冲区指针回退一个字符
								syn=11;
							}
							else
								switch(ch)
								{
									case '										token.append(ch);
										ch=prog[++p];
										if(ch=='>')
										{
											syn=21;
											token.append(ch);
										}
										else if(ch=='=')
										{
											syn=22;
											token.append(ch);
										}
										else
										{syn=20;
										}
										break;
									case '>':
										token.append(ch);
										ch=prog[++p];
										if(ch=='=')
											{
												syn=24;
												token.append(ch);
											}
										else
											{
												p--;
												syn=23;
											}
										break;						
									case ':':
										token.append(ch);
										ch=prog[++p];
										if(ch=='=')
										{
											syn=18;
											token.append(ch);
										}
										else
										{
											syn=17;
										}
										break;
									case '+':syn=13;token.append(ch);break;
									case '-':syn=14;token.append(ch);break;
									case '*':syn=15;token.append(ch);break;
									case '/':syn=16;token.append(ch);break;
									case '=':syn=25;token.append(ch);break;
									case ';':syn=26;token.append(ch);break;
									case '(':syn=27;token.append(ch);break;
									case ')':syn=28;token.append(ch);break;
									case '#':syn=0;token.append(ch);break;
									default :syn=-1;
								}
								++p;//指针指向缓冲区下一个字符
					}
				}
							

相关资源