Java版编译原理Chomsky文法判断Java版

本文介绍了一个Java程序,用于根据用户输入的一组文法规则来判断其对应的Chomsky文法类型。通过逐个检查产生式,该程序能够确定文法是否属于0型、1型、2型或3型。

实验内容
输入:一组任意的规则。输出:相应的Chomsky 文法的类型

import java.util.Scanner;
public class Chomsky {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int n;
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入文法产生式的个数:");
		n = scanner.nextInt();
		String inputString;
		CSS[] p = new CSS[n];

		for (int k = 0; k < p.length; k++) {
			inputString = scanner.next();
			String temp = inputString.trim();
			p[k] = new CSS();
			p[k].left = inputString.split("->")[0];
			p[k].right = temp.split("->")[1];

		}
		scanner.close();
        Third(p);
		

	}

	public static boolean Zero(CSS[] p) {//
		int i, j;
		for (i = 0; i < p.length; i++) // 循环n次,即遍历所有产生式
		{
			for (j = 0; j < p[i].left.length(); j++) // 遍历产生式左部每一个字符
			{
				if (p[i].left.charAt(j) >= 'A' && p[i].left.charAt(j) <= 'Z') // 判断字符是否是非终结符
					break;
			}
			if (j == p[i].left.length()) {
				System.out.println("该文法不是0型文法");
				return false;
			}
		}
		if (i == p.length)
			return true;// 如果每个产生时都能找到非终结符
		return false;
	}

	public static boolean First(CSS[] p) {
		int i;
		if (Zero(p)) {
			for (i = 0; i < p.length; i++) {
				if ((p[i].left.length() > p[i].right.length())
						&& p[i].right.length() != 0) // 判断产生式左部长度是否大于右部
					break;
			}
			if (i == p.length)
				return true;
			else {
				System.out.println("0型文法");
				return false;
			}
		} else
			return false;

	}

	public static boolean Second(CSS[] p) {
		int i;
		if (First(p)) // 同上,先判断低级文法是否成立
		{
			for (i = 0; i < p.length; i++) // 同上,遍历所有文法产生式
			{
				if ((p[i].left.length() != 1)
						|| !(p[i].left.charAt(0) >= 'A' && p[i].left.charAt(0) <= 'Z'))
					break;
			}
			if (i == p.length)
				return true;
			else {
				System.out.println("1型文法");
				return false;
			}
		} else
			return false;

	}

	public static void Third(CSS[] p) // 判断3型文法
	{
		int i;
		if (Second(p)) // 同上,先判断是否是2型文法
		{
			for (i = 0; i < p.length; i++) // 同上,遍历文法所有的产生式
			{
				if ((p[i].right.length() == 0)
						|| (p[i].right.length() >= 3)
						|| (p[i].right.charAt(0) >= 'A' && p[i].right.charAt(0) <= 'Z')) // 判断产生式右部字符个数是否在12之间,判断右部第一个字符是否是非终结符
					break;
			}
			if (i == p.length) {
				for (i = 0; i < p.length; i++) {
					if (p[i].right.length() == 2) {
						if (!(p[i].right.charAt(1) >= 'A' && p[i].right
								.charAt(1) <= 'Z'))
							break;
					}
				}
				if (i == p.length) {
					System.out.println("该文法属于3型文法");
				} else
					System.out.println("该文法属于2型文法");

			} else
				System.out.println("该文法属于2型文法");
		} else
			System.out.println("END");
	}

}

class CSS {
	CSS() {
		left = new String();
		right = new String();
	}

	String left;
	String right;
}


本资源包括以下内容: 1、从官方网站下载的全书代码 2、Modern.Compiler.Implementation.in.Java.Second.Edition.chm Last year you may have seen the Modern Compiler Implementation in C: Basic Techniques (1997) which was the preliminary edition of our new 1998 textbook, Modern Compiler Implementation in C. The new, expanded version of this textbook describes all phases of a modern compiler: lexical analysis, parsing, abstract syntax, semantic actions, intermediate representations, instruction selection via tree matching, dataflow analysis, graph-coloring register allocation, and runtime systems. It includes good coverage of current techniques in code generation and register allocation, as well as functional and object-oriented languages, that are missing from most books. In addition, more advanced chapters are now included so that it can be used as the basis for two-semester or graduate course. The most accepted and successful techniques are described in a concise way, rather than as an exhaustive catalog of every possible variant. Detailed descriptions of the interfaces between modules of a compiler are illustrated with actual C header files. The first part of the book, Fundamentals of Compilation, is suitable for a one-semester first course in compiler design. The second part, Advanced Topics, which includes the advanced chapters, covers the compilation of object-oriented and functional languages, garbage collection, loop optimizations, SSA form, loop scheduling, and optimization for cache-memory hierarchies. A unique feature of the book is a well designed compiler implementation project in Java, including front-end and 'high-tech' back-end phases, so that students can build a complete working compiler in one semester. Accompanying support software is available.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡宝全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值