/* Generated By:JavaCC: Do not edit this line. ifParse.java */
package ifelsesematic;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
@SuppressWarnings("all")
public class ifParse implements ifParseConstants {
public int count=0; /*四元式标号*/
public static ArrayList<Sys> sysList = new ArrayList<Sys>();
public int ncount=0; /*临时变量下标*/
public static void main(String args[]) throws ParseException {
FileInputStream fileStream;
try {
fileStream = new FileInputStream("test1.c");
ifParse parser = new ifParse(fileStream);
System.out.println("Reading from standard input...");
System.out.println("Enter c programe only main() with only while();\" :");
try {
switch (parser.start()) {
case 0:
System.out.println("OK.");
break;
case 1:
System.out.println("Goodbye.");
break;
default:
break;
}
for(int i = 0; i<sysList.size(); i++){
System.out.println(sysList.get(i));
}
} catch (Exception e) {
System.out.println("NOK.");
System.out.println(e.getMessage());
parser.ReInit(System.in);
} catch (Error e) {
System.out.println("Oops.");
System.out.println(e.getMessage());
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
final public int start() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case VOID:
Procedure();
jj_consume_token(0);
{if (true) return 0;}
break;
case 0:
jj_consume_token(0);
{if (true) return 1;}
break;
default:
jj_la1[0] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
throw new Error("Missing return statement in function");
}
/*处理主程序*/
final public void Procedure() throws ParseException {
jj_consume_token(VOID);
jj_consume_token(MAIN);
jj_consume_token(LC);
jj_consume_token(RC);
jj_consume_token(LB);
Statement();
jj_consume_token(RB);
}
final public void Statement() throws ParseException {
IfStatement();
label_1:
while (true) {
if (jj_2_1(2)) {
;
} else {
break label_1;
}
ElseifStatement();
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ELSE:
ElseStatement();
break;
default:
jj_la1[1] = jj_gen;
;
}
}
/* IfStatement */
final public void IfStatement() throws ParseException {
Token node; //
int j1;
int next; //下一个四元式标号
Sys newsys;
jj_consume_token(IF);
jj_consume_token(LC);
next=count+1;
Condition();
jj_consume_token(RC);
count++;
j1=count;
newsys = new Sys(count+"","J","_", "_", j1+"#"); //声明一个新的四元式(1:J,_,_,1#)
sysList.add(newsys);
jj_consume_token(LB);
next=count+1;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
Assign();
jj_consume_token(28);
break;
case IF:
Statement();
break;
default:
jj_la1[2] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jj_consume_token(RB);
count++; //count=2
newsys = new Sys(count+"","J","_", "_", next+""); //声明一个新的四元式(2:J,_,_,1#)
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+""); //回填如果四元式列表中有result==1#的则将其result置为2
}
/* ElseStatement */
final public void ElseStatement() throws ParseException {
Token node;
int j1;
int next; //下一个四元式标号
Sys newsys;
jj_consume_token(ELSE);
jj_consume_token(LB);
next=count+1;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
Assign();
jj_consume_token(28);
break;
case IF:
Statement();
break;
default:
jj_la1[3] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jj_consume_token(RB);
count++;
j1=count; //count=2
newsys = new Sys(count+"","J","_", "_", next+""); //声明一个新的四元式(2:J,_,_,1#)
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+""); //回填如果四元式列表中有result==1#的则将其result置为2
}
final public void ElseifStatement() throws ParseException {
Token node;
int j1;
int next; //下一个四元式标号
Sys newsys;
jj_consume_token(ELSE);
jj_consume_token(IF);
jj_consume_token(LC);
next=count+1;
Condition();
jj_consume_token(RC);
count++;
j1=count;
newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
jj_consume_token(LB);
next=count+1;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
Assign();
jj_consume_token(28);
break;
case IF:
Statement();
break;
default:
jj_la1[4] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jj_consume_token(RB);
count++;
newsys = new Sys(count+"","J","_", "_",j1+"#"+"");
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+"");
}
/*
void IfStatement():
{
Token node;
int j1;
int next;
Sys newsys;
}
{
<IF><LC>
{
next=count+1;
}
Condition()<RC>
{
count++;
j1=count;
newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
}
<LB>
{
next=count+1;
}
((Assign()";")|Statement())<RB>
{
count++;
newsys = new Sys(count+"","J","_", "_", j1+"#"+"");
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+"");
}
}
void ElseifStatement():
{
Token node;
int j1;
int next;
Sys newsys;
}
{
<ELSE><IF><LC>
{
next=count+1;
}
Condition()<RC>
{
count++;
j1=count;
newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
}
<LB>
{
next=count+1;
}
((Assign()";")|Statement())<RB>
{
count++;
newsys = new Sys(count+"","J","_", "_",j1+"#"+"");
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+"");
}
}
void ElseStatement():
{
Token node;
int j1;
int next;
Sys newsys;
}
{
<ELSE>
<LB>
{
next=count+1;
}
((Assign()";")|Statement())<RB>
{
count++;
j1=count;
newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+"");
}
}*/
final public String Expression() throws ParseException {
//定义变量
String first;
String middle;
String temp="";
Sys newsys;
//函数体
first = Term();
temp=first;
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
case MINUS:
;
break;
default:
jj_la1[5] = jj_gen;
break label_2;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS:
jj_consume_token(PLUS);
middle = Term();
//加法要分步计算
count++;
temp="T"+ncount;
ncount++;
newsys = new Sys(count+"","+",first,middle,temp);
sysList.add(newsys);
first = temp;
//fi
- 1
- 2
前往页