/**
* pl/x 语言语法分析器的java实现
* ps:这是我的编译作业,只是帖出来一部分,呵呵,大家指正!
* @author 作者:小进 <br>
* 时间:2004年20/12* <hr>
*/
package plx;
public class Parser extends GetSymbol {
GetSymbol sym;
int word;
public Parser() {
sym = new GetSymbol();
}
/**
* 语法分析开始方法
*/
public void start() {
word = sym.getNextSymbol();
if (word == programsym) {
ds();
if (word == beginsym) {
ss();
if (word == endsym) {
word = sym.getNextSymbol();
if (word != period) {
error(7);
}
} else {
error(6);
}
} else {
error(5);
}
} else {
error(0);
}
}
private void ss() {
s();
while (word == semicolon) {
s();
}
}
private void s() {
word = sym.getNextSymbol();
switch (word) {
case aident: {
word = sym.getNextSymbol();
if (word == become) {
ae();
} else {
error(8);
}
break;
}
case bident: {
word = sym.getNextSymbol();
if (word == become) {
be();
} else {
error(8);
}
break;
}
case ifsym: {
be();
if (word == thensym) {
ss();
if (word == elsesym) {
ss();
} else if (word != endsym) {
error(9);
}
word = sym.getNextSymbol();
} else {
error(10); //if then 不匹配
}
break;
}
case whilesym: {
be();
if (word == dosym) {
ss();
if (word == endsym) {
word = sym.getNextSymbol();
}else {
error(11);//end;
}
}
break;
}
case repeatsym: {
ss();
if (word == untilsym) {
be();
} else {
error(12);//repeat until不匹配
}
break;
}
case writesym: {
ae();
break;
}
}
}
private void be() {
bt();
while (word == orsym) {
bt();
word = sym.getNextSymbol();
}
}
/**
*
*/
private void bt() {
bf();
while (word == andsym) {
bf();
}
}
private void bf() {
word = sym.getNextSymbol();
switch (word) {
case bident: {
word = sym.getNextSymbol();
break;
}
case truesym: {
word = sym.getNextSymbol();
break;
}
case falsesym: {
word = sym.getNextSymbol();
break;
}
case notsym: {
bf();
break;
}
case lparen: {
be();
if (word != rparen) {
error();
}
word = sym.getNextSymbol();
break;
}
default:
re();
break;
}
}
/**
*
*/
private void re() {
if ((word == aident) || (word == number)) {
word = sym.getNextSymbol();
if ((word == equal) || (word == notEqu) || (word == leq)
|| (word == geq) || (word == gtr) || (word == lss)) {
ae();
} else {
error(14);
}
} else {
error(13);
}
}
/**
*
*/
private void ae() {
word = sym.getNextSymbol();
if (word == minus) {
System.out.println("fdsafsadf");
word = sym.getNextSymbol();
at();
while ((word == minus) || (word == plus)) {
word = sym.getNextSymbol();
at();
}
} else {
at();
while ((word == minus) || (word == plus)) {
word = sym.getNextSymbol();
at();
}
}
}
/**
*
*/
private void at() {
af();
word = sym.getNextSymbol();
while ((word == times) || (word == slash)) {
word = sym.getNextSymbol();
af();
word = sym.getNextSymbol();
}
}
private void af() {
if (word == aident) {
} else if (word == number) {
} else if (word == lparen) {
ae();
if (word != rparen) {
error(15);
}
} else {
error(16);
}
}
private void ds() {
d();
while (word == semicolon) {
d();
}
}
private void d() {
word = sym.getNextSymbol();
if (word == integersym) { //整型变量定义
word = sym.getNextSymbol();
if (word == aident) {
word = sym.getNextSymbol();
while (word == comma) {
word = sym.getNextSymbol();
if (word != aident) {
error(18);
}
word = sym.getNextSymbol();
}
} else {
error(17);
}
} else if (word == logicalsym) { //逻辑变量定义
word = sym.getNextSymbol();
if (word == bident) {
word = sym.getNextSymbol();
while (word == comma) {
word = sym.getNextSymbol();
if (word != bident) {
error(20);
}
word = sym.getNextSymbol();
}
} else {
error(19);
}
}
}
private void error() {
System.out.println("error!");
}
public static void main(String[] args) {
(new Parser()).start();
}
}
另附:PL/X语言语法
prog=”program” ds ”begin” ss ”end” ”.”.
ds=d {“;” d}.
ss=s {“s” s}.
d=”integer” aident {“,” aident}|”logical” bident {“,” bident}.
s=aident ”:=” ae | biednt “:=”be | “if” be “then” ss [“else” ss] “end”
| “while” be “do” ss “end” | “repeat” ss “until” be | “write” ae.
ae=[“-”] at {(“-” | ”+”) at}.
at=af {(“*” | ”/”) af}.
af=aident | number | “(”ae”)”.
be=bt {“or”bt}.
bt=bf (“and”bf).
bf=bident | “true” | “false” | “not” bf | “(”be”)” | re.
re=(aident | number) (“=”|”>’|”>=”|”<”|”<=”|”/=”) ae.