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

📄 yufafenxi.c

📁 编译原理的一个语法分析程序
💻 C
字号:
/*
	 * 表达式的分析 expression = term{+term|-term}
	 */
	private void expression() {
		this.term();
		this.scanNext();
		while (this.match("+") || this.match("-")) {
			this.term();
			this.scanNext();
		}
		this.scanBack();

	}

	/*
	 * 项的分析 term = facto人{*factor|/factor)}
	 */
	private void term() {
		this.factor();
		this.scanNext();
		while (this.match("*") || this.match("\\")) {
			this.factor();
			this.scanNext();
		}
		this.scanBack();

	}

	/*
	 * 因子的分析 factor = (expression)|id|number
	 */
	private void factor() {
		this.scanNext();
		if (this.match("id") || this.match("number")) {

			// ---------------------------------------------------
		} else if (this.match("(")) {
			this.expression();
			this.matchNext(")");
		} else {
			System.out.println(row + " Error: factor error!");
		}
	}

	/*
	 * 条件的分析 condition = expression(>= | <= | == | > | < | !=)expression
	 */
	private void condition() {
		this.expression();
		this.scanNext();
		if (this.match("<=") || this.match("==") || this.match(">=")
				|| this.match(">") || this.match("<") || this.match("!=")) {

		} else {
			System.out.println(row + " ERROR: condition error!");
		}
		this.expression();
	}

	/*
	 * 条件语句 conditionStatement = if(condition)"{"statementBlock"}"{else
	 * conditionStatement}|else statementBlock
	 */
	private void conditionStatement() {
		this.matchNext("if");
		this.matchNext("(");
		this.condition();
		this.matchNext(")");
		this.statementBlock();
		this.scanNext();
		if (this.match("else")) {
			this.scanNext();
			if (this.match("{")) {
				this.scanBack();
				this.statementBlock();
			} else if (this.match("if")) {
				this.scanBack();
				this.conditionStatement();
			} else {
				System.out.println(row + " ERROR: conditionStatement error!");
			}
		} else {
			this.scanBack();
		}
	}

	/*
	 * 循环语句的分析 recycleStatement = while(condition){statementBlock}
	 */
	private void recycleStatement() {
		this.matchNext("while");
		this.matchNext("(");
		this.condition();
		this.matchNext(")");
		this.statementBlock();
	}

	/*
	 * 赋值语句分析 valueStatement = <int|char> id = expression{,id = expression};
	 */
	private void intValueStatement() {
		int nowRow = this.row;
		this.matchNext("int");
		this.matchNext("id");
		this.scanNext();
		if (this.match("=")) {
			this.expression();
		} else {
			this.scanBack();
		}
		this.scanNext();
		while (this.match(",")) {
			this.matchNext("id");
			this.scanNext();
			if (this.match("=")) {
				this.expression();
			} else {
				this.scanBack();
			}
			if (this.row != nowRow) {
				System.out.println(row + " ERROR: intValueStatement error!");
			}
			this.scanNext();
		}
		this.scanBack();
	}

	private void charValueStatement() {
		int nowRow = this.row;
		this.matchNext("char");
		this.matchNext("id");
		this.scanNext();
		if (this.match("=")) {
			this.expression();
		} else {
			this.scanBack();
		}
		this.scanNext();
		while (this.match(",")) {
			this.matchNext("id");
			this.scanNext();
			if (this.match("=")) {
				this.expression();
			} else {
				this.scanBack();
			}
			if (this.row != nowRow) {
				System.out.println(row + " ERROR: intValueStatement error!");
			}
			this.scanNext();
		}
		this.scanBack();
	}

	/*
	 * 语句块的分析
	 */
	private void statementBlock() {
		this.matchNext("{");
		this.statementSequence();
		this.matchNext("}");
	}

	/*
	 * 语句串的分析
	 */
	private void statementSequence() {
		this.scanNext();
		while (this.match("if") || this.match("while") || this.match("id")
				|| this.match(";") || this.match("int") || this.match("char")) {
			if (this.match("if")) {
				this.scanBack();
				this.conditionStatement();
			} else if (this.match("while")) {
				this.scanBack();
				this.recycleStatement();
			} else if (this.match("id")) {
				this.matchNext("=");
				this.expression();
				this.matchNext(";");
			} else if (this.match("int")) {
				this.scanBack();
				this.intValueStatement();
			} else if (this.match("char")) {
				this.scanBack();
				this.charValueStatement();
			} else if (this.match(";")) {

			}
			this.scanNext();
		}
		this.scanBack();
	}

	public void parseMain() {
		this.matchNext("main");
		this.matchNext("(");
		this.matchNext(")");
		this.statementBlock();
		System.out.println("Complete!");
	}

⌨️ 快捷键说明

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