编译原理实验三之语义分析

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/**
* JavaCC file
*/

options {
JDK_VERSION = "1.5";
static=false;
}
PARSER_BEGIN(whileParse)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class whileParse {
public int count=0; /*四元式标号*/
public static ArrayList sysList = new ArrayList();
public int ncount=0; /*临时变量下标*/

public static void main(String args[]) throws ParseException {
FileInputStream fileStream;
try {
fileStream = new FileInputStream("test.c");
// fileStream = new FileInputStream("test1.c");
// fileStream = new FileInputStream("test2.c");
whileParse parser = new whileParse(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; iSystem.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();
}
}
}
PARSER_END(whileParse)



SKIP :
{
" "
| "\r"
| "\t"
| "\n"
| <"/*"(~["*"])*"*"(~["/"](~["*"])*"*")*"/">
}
TOKEN : /* OPERATORS */
{
< PLUS: "+" >
| < MINUS: "-" >
| < MULTIPLY: "*" >
| < DIVIDE: "/" >
}
TOKEN :
{
< CONSTANT: ( )+| ( )+"."( )+|( )+"."|"."( )+>
| < #DIGIT: ["0" - "9"] >
}
TOKEN:
{

|
|
|
|
|
|
|
|
|< ID: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","0"-"9"] )* >

}

TOKEN:
{
">
|
| =">
|
|
|
}

TOKEN:
{

|
|
|
|
|
}
TOKEN :
{

|
}
int start() : {}
{
Procedure() { return 0; }
| { return 1; }
}


/*处理主程序*/
void Procedure():{}
{


Code()
}
void Code():{}
{
(|||)()*Code()
|state()(state())*
}
void state() :{}
{
Ifstate()
| Elsestate()
| WhileStatement()
| idstate()
}
void idstate():
{
String temp;
Token id;
Sys newsys;
}
{
id=temp=Expression()
{
count++;
newsys =

new Sys(count+"","J=",temp, "_", id+"");
sysList.add(newsys);
}
}
void Ifstate():
{
Token node;
int j1;
int next;
Sys newsys;
}
{

{
next=count+1;
}
Condition()
{
count++;
j1=count;

newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
}
(state())*
{
count++;
j1=count;

newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
}
}

void Elsestate():
{
Token node;
int j1;
int next;
Sys newsys;
}
{

(state())*
{
count++;
j1=count;

newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
}
}

void WhileStatement():
{
Token node;
int j1;
int next;
Sys newsys;
}
{

{
next=count+1;
}
Condition()
{
count++;
j1=count;

newsys = new Sys(count+"","J","_", "_", j1+"#");
sysList.add(newsys);
}
(state())*
{
count++;
newsys = new Sys(count+"","J","_", "_", next+"");
sysList.add(newsys);
EditSys.huitian(sysList, j1+"#", count+1+"");
}
}

String Expression() :
{
String first;
String middle;
String temp="";
Sys newsys;
}
{
first=Term()
{
temp=first;
}
( middle=Term(){
count++;
temp="T"+ncount;
ncount++;
newsys = new Sys(count+"","+",first,middle,temp);
sysList.add(newsys);
first = temp;
//first=Integer.parseInt(first)+Integer.parseInt(middle)+"";
}
| middle=Term(){
count++;
temp="T"+ncount;
ncount++;
newsys = new Sys(count+"","-",first,middle,temp);
sysList.add(newsys);
first = temp;
//first=Integer.parseInt(first)-Integer.parseInt(middle)+"";
}
)*
{
return temp;
//return first;
}
}
String Term() :
{
String first;
String middle;
String temp="";
Sys newsys;
}
{
first=unary()
{
temp=first;
}
(
( middle= unary()
{
count++;
temp="T"+ncount;
ncount++;
newsys = new Sys(count+"","*",first,middle,temp);
sysList.add(newsys);
first = temp;
//first=Integer.parseInt(first)*Integer.parseInt(middle)+"";
}

| middle=unary()
{
count++;
temp="T"+ncount;
ncount++;
newsys = new Sys(count+"","/",first,middle,temp);
sysList.add(newsys);
first = temp;
//first=Integer.parseInt(first)/Integer.parseInt(middle)+"";
}
)
)*
{
return temp;
//return first;
}
}
String unary() :
{
String str;
}
{
// str= element(){return -Double.parseDouble(str)+"";}
str= element(){return str;}
}

String element() :
{
String str;
Token node;
}
{
node={return node.image;}
| str=Expression(){return str;}
|node={return node.image;}
}

int C

ondition():
{
Token node;
int next;
String first;
String middle;
Sys newsys;
}
{
first=Expression()(node="<" middle=Expression()
{
count++;
next=count+1;
newsys = new Sys(count+"","j<",first,middle,next+1+"");
sysList.add(newsys);
}
| node=">" middle=Expression()
{
count++;
next=count+1;
newsys = new Sys(count+"","j>",first,middle,next+1+"");
sysList.add(newsys);
}
| node=">=" middle=Expression()
{
count++;
next=count+1;
newsys = new Sys(count+"","j>=",first,middle,next+1+"");
sysList.add(newsys);
}
| node="<=" middle=Expression()
{
count++;
next=count+1;
newsys = new Sys(count+"","j<=",first,middle,next+1+"");
sysList.add(newsys);
})
{
return next;
}
}

相关文档
最新文档