www.pudn.com > BianYiYuanLi.rar > YFrame.java


import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
 
class YFrame extends Frame {//此处包含了2个主要的类,一个是词法分析的类(已经分析过了),另一个是语法分析的类 
 
	public YFrame() {//框架类就不重复了 
		super("赋值语句的翻译程序"); 
	}								 
 
	Button b2 = new Button("comp"); 
 
	Button b1 = new Button("save"); 
 
	JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7; 
 
	JLabel label1, label2, label3, label4, label5; 
 
	JTextArea textArea11 = new JTextArea(6, 28); 
 
	JTextArea textArea12 = new JTextArea(6, 28); 
 
	JTextArea textArea2 = new JTextArea(2, 30); 
 
	JTextArea textArea3 = new JTextArea(2, 30); 
 
	JTextArea textArea4 = new JTextArea(2, 30); 
 
	JTextArea textArea5 = new JTextArea(2, 30); 
 
	JTextArea textArea6 = new JTextArea(2, 30); 
 
	public void launchFrame() { 
		this.setBounds(0, 0, 500, 500); 
		label1 = new JLabel("关键字:"); 
		label2 = new JLabel("标识符:"); 
		label3 = new JLabel("操作符:"); 
		label4 = new JLabel("分界符:"); 
		label5 = new JLabel("数字符:"); 
		panel3 = new JPanel(); 
		panel4 = new JPanel(); 
		panel5 = new JPanel(); 
		panel6 = new JPanel(); 
		panel7 = new JPanel(); 
		panel3.add(label1); 
		panel3.add(new JScrollPane(textArea2)); 
		panel4.add(label2); 
		panel4.add(new JScrollPane(textArea3)); 
		panel5.add(label3); 
		panel5.add(new JScrollPane(textArea4)); 
		panel6.add(label4); 
		panel6.add(new JScrollPane(textArea5)); 
		panel7.add(label5); 
		panel7.add(new JScrollPane(textArea6)); 
		JPanel panel = new JPanel(); 
		panel.setLayout(new GridLayout(5, 1)); 
		panel.add(panel3); 
		panel.add(panel4); 
		panel.add(panel5); 
		panel.add(panel6); 
		panel.add(panel7); 
		add(panel, BorderLayout.CENTER); 
		panel1 = new JPanel(); 
		panel2 = new JPanel(); 
		panel1.add(b1); 
		panel1.add(new JScrollPane(textArea11)); 
		panel2.add(b2); 
		panel2.add(new JScrollPane(textArea12)); 
		add(panel1, BorderLayout.NORTH); 
		add(panel2, BorderLayout.SOUTH); 
		textArea11.setFont(new Font("Arial Narrow", Font.BOLD, 14)); 
		textArea12.setFont(new Font("BinnerGothic", Font.BOLD, 14)); 
		textArea12.append("请在最上方的文本区中输入语句,并以#结束! \n"); 
		this.setVisible(true); 
		b1.addActionListener(new Monitor()); 
		b2.addActionListener(new Monitor()); 
		this.addWindowListener(new WindowAdapter() { 
			public void windowClosing(WindowEvent e) { 
				System.exit(0); 
			} 
		}); 
	} 
 
	class Monitor implements ActionListener {//在时间监听类中添加了语法分析以及语义分析 
 
		public void actionPerformed(ActionEvent e) { 
			if (e.getSource() == b1) { 
				save(); 
				textArea12.append("文件保存成功! \n"); 
			} 
			if (e.getSource() == b2) { 
				CJudgement cj = new CJudgement();//创建一个词法分析的对象,并调用其中的方法 
				cj.cJudge(); 
				YJudgement yj = new YJudgement();//创建一个语法分析的对象,并调用其中的方法 
				yj.yJudge(); 
				System.out.println(yj.bl); 
				while (yj.bl) {//如果语法分析通过,则进行语义分析,即转换成后缀表达式 
					String input = textArea11.getText(); 
					InToPost itp = new InToPost(input);//创建一个中缀转后缀的对象 
					String output = itp.Translate(); 
					textArea12.append(output); 
					yj.bl = false; 
					//System.out.println("ok"); 
				} 
 
			} 
		} 
 
		private void save() {//文件的保存 
			String name = new String("MyFile.txt"); 
			// count ++; 
			String source = textArea11.getText().trim(); 
			byte buffer[] = source.getBytes(); 
			int b = buffer.length; 
			try { 
				File file = new File(name); 
				FileOutputStream fos = new FileOutputStream(file); 
				fos.write(buffer, 0, b); 
				fos.close(); 
			} catch (FileNotFoundException e) { 
				e.printStackTrace(); 
			} catch (IOException e1) { 
				e1.printStackTrace(); 
			} 
 
		} 
	} 
 
	public class YJudgement {//此为语法分析的核心部分,主要采取递归子程序的方法,通过编写FIRST集和FOLLOW集完成 
		boolean bl = true; 
						 
		int i = 0; 
 
		char c = getChar(); 
 
		public void yJudge() {//程序的入口 
			S(); 
			if (c == '#') { 
				textArea12.append("该语句是LL(1)型文法的简单赋值语句!\n"); 
			} else 
				error(); 
 
		} 
 
		public void S() { 
			if (isLetter()) { 
				while (isLetter()) {//如果是字母,则全部取出 
					//System.out.println("c1 = " + c); 
					getChar(); 
				} 
				if (c == '=') {//判断下个字符是否为“=”号 
					//System.out.println("c2 = " + c); 
					getChar(); 
					E();//调用E()方法 
				} 
			} else 
				error();//出错处理 
			return; 
		} 
 
		public void E() {//通过FIRST集,作为其判断条件 
			if (c == '-' || c == '(') { 
				//System.out.println("c3 = " + c); 
				getChar(); 
				E();//如果满足条件,则调用自身 
			} else if (isLetter()) { 
				while (isLetter()) {//如果是字母,则将其全部取出 
					//System.out.println("c4 = " + c); 
					getChar(); 
				} 
				E1();//再调用E1()方法 
			} else 
				error();//出错处理 
			return; 
		} 
 
		public void E1() {//E1的由来是因为消除左递归而来 
			if (c == '+' || c == '*') {//同样根据其FIRST集来做为判断条件 
				//System.out.println("c5 = " + c); 
				getChar(); 
				E();//满足条件,则调用E()方法 
			} else if (c == ';') { 
				getChar(); 
			} else if (c == '#') {//遇到“#”号,不做操作 
 
			} else if (c == ')') {//遇到“)”号,调用自身 
				getChar(); 
				E1(); 
			} else 
				error();//出错处理 
			return; 
		} 
 
		public boolean isLetter() {//判断是否为字母 
			if (c >= 'a' && c <= 'z') 
				return true; 
			else 
				return false; 
		} 
 
		public char getChar() {//自定义的取字符方法,同词法分析 
			char[] ch = textArea11.getText().toCharArray(); 
			if (i < (textArea11.getText().length())) { 
				c = ch[i]; 
				i++; 
			} 
			return c; 
		} 
 
		public void error() {//出错处理的方法 
			textArea12.append("该语句不是LL(1)型文法的简单赋值语句!\n"); 
			bl = false; 
		} 
 
	} 
 
	public class CJudgement {//词法分析的注释 ,见词法分析程序 
		KeyWord key = new KeyWord(); 
 
		int i = 0; 
 
		char c = getChar(); 
 
		String input = new String(); 
 
		String output = new String(); 
 
		public void cJudge() { 
			while (c != ' ') { 
				isKeyword(); 
			} 
		} 
 
		public void isKeyword() { 
			boolean flag = false; 
			if (isLetter()) { 
				while (isLetter()) { 
					output = output + c; 
					getChar(); 
					System.out.println("c1——" + output); 
				} 
				for (int i = 0; i < key.x; i++) { 
					System.out.println("i——" + i); 
					if (output.equals(key.k[i])) { 
						flag = true; 
						break; 
					} 
				} 
				if (flag) { 
					textArea2.append(output + " "); 
				} else { 
					textArea3.append(output + " "); 
				} 
				output = ""; 
 
			} else if (isNumber()) { 
				while (isNumber()) { 
					output = output + c; 
					getChar(); 
					System.out.println("c2——" + output); 
				} 
				textArea6.append(output + " "); 
				output = ""; 
			} else if (isOperate()) { 
				textArea4.append(c + " "); 
				getChar(); 
			} else if (isFenJie()) { 
				textArea5.append(';' + " "); 
				getChar(); 
			} else if (c == '#') { 
				c = ' '; 
			} 
		} 
 
		public boolean isLetter() { 
			if (c >= 'a' && c <= 'z') 
				return true; 
			else 
				return false; 
		} 
 
		public boolean isNumber() { 
			if (c >= '0' && c <= '9') 
				return true; 
			else 
				return false; 
 
		} 
 
		public boolean isOperate() { 
			Operate op = new Operate(); 
 
			for (int i = 0; i < op.y; i++) { 
				if (c == op.o[i]) 
					return true; 
			} 
			return false; 
 
		} 
 
		public boolean isFenJie() { 
			if (c == ';') { 
 
				return true; 
			} else 
				return false; 
 
		} 
 
		public char getChar() { 
			char[] ch = textArea11.getText().toCharArray(); 
			if (i < (textArea11.getText().length())) { 
				c = ch[i]; 
				i++; 
			} 
			return c; 
		} 
	} 
 
}