问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

用java编写程序实现判断及识别标识符的功能

发布网友 发布时间:2022-04-24 03:17

我来回答

2个回答

热心网友 时间:2023-10-24 08:43

这个是我做编译原理词法分析用的,可以识别包括,标示符,关键字,常数,变量,操作符,界符等,你先拿去用吧!!

import java.io.FileReader;
//import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

class Tools{
public final int maxsize=200;
String savwd[]={"main","int","float","char","double","long","return","if","case","switch","break","default","printf","scanf","define","include"};//保留字数组;
char operate[]={'=','>','<','?','!','+','-','*','/','%','(',')',':'};//操作符数组
char seprate[]={',',';','{','}','"'};//分隔符数组
String doperate[]={"==","+=","-=",">=","<=","!=","=","+","-","<",">","!","*","/","%","(",")","++","--","()"};
}

class Monitor{
public int flag;
public String string;
}

public class test {
static Monitor analyste(char array[]){//对读取的窜进行分析
Tools get=new Tools();
Monitor monitor=new Monitor();
int flag=0,numbercount=0;
int countnumber;
for(countnumber=0;array[countnumber]!='\0';countnumber++);
String str=new String(array);
str=str.substring(0,countnumber);
for(int count=0;count<get.savwd.length;count++)
if(str.equals(get.savwd[count]))
{
flag=1;
break;
}
if(flag==1){//是否为关键字
monitor.flag=1 ;
}
else if(array[0]>='0'&&array[0]<='9'){//判断是否为常量
numbercount=1;
for(int count=numbercount;count<str.length();count++)
if(array[count]>='0'&&array[count]<='9')
numbercount++;
if(numbercount<str.length()){//判断是否为错误表示符
monitor.flag=2;
}
else if(numbercount==str.length()){//判断是否是常量
monitor.flag=3;
}
}
else if((array[0]>='a'&&array[0]<='z')||(array[0]>='A'&&array[0]<='Z')){//判断是否是表示符
monitor.flag=4;
}
monitor.string=str;
return monitor;
}

static boolean iseprate(char o)//判断是否是分割符
{
Tools get=new Tools();
int flag=0;
for(int count=0;count<get.seprate.length;count++){
if(o==get.seprate[count]){
flag=1;
break;
}
}

return (flag==1);
}

static boolean isoperate(char c){//判断是否是运算符
Tools get=new Tools();
int flag=0;
for(int count=0;count<get.operate.length;count++){
if(c==(get.operate[count])){
flag=1;
break;
}
}
return (flag==1);
}

static Monitor analystoperate(char doubles[])//此函数用于分析操作数组,并返回分析状态值给MONITOR,共输出程序调用
{
int count;
int flag=0;
Tools tl=new Tools();
Monitor temp=new Monitor();
String str=new String(doubles);
for( count=0;doubles[count]!='\0';count++);
str=str.substring(0,count);
for(int i=0;i<tl.doperate.length;i++){//判断操作符的合法性,并且给MONITOR 设定状态
if(str.equals(tl.doperate[i])){
flag=1;
break;
}
}
temp.flag=flag;
temp.string=str;
return temp;
}

public static void main(String args[])throws IOException{
Tools get=new Tools();

FileReader in=new FileReader("C:\\myjava\\testdata.txt");
FileOutputStream file=new FileOutputStream("C:\\myjava\\result.txt",true);
OutputStreamWriter out=new OutputStreamWriter(file);
int temp;
int flag=0;
char operate[]=new char[get.maxsize];
int operatecount=0;
int operateflag=0;
Monitor result=new Monitor();
Monitor oresult=new Monitor();

char temparray[]=new char[get.maxsize];
System.out.println("符号 名称 说明");
out.write("符号 名称 说明"+"\n");
System.out.println();
while((temp=in.read())!=-1){//读文件
if(temp!=' '&&!iseprate((char)temp)&&!isoperate((char)temp)&&temp!='\n')
temparray[flag++]=(char)temp;
else {
result=analyste(temparray);
char character[]={(char)temp};
String word=result.string;
if(result.flag==1){
System.out.println(word+" 关键字");
out.write(word+" 关键字");
out.write("\n");
}
if(result.flag==2){
System.out.println(word+" 标示符 error");
out.write(word+" 标示符 error");
out.write("\n");
}
if(result.flag==3){
System.out.println(word+" 常量 ");
out.write(word+" 常量 ");
out.write("\n");
}
if(result.flag==4){
System.out.println(word+" 标示符");
out.write(word+" 标示符");
out.write("\n");
}
if(result.flag==5){
System.out.println(word+" 关键字");
out.write(word+" 关键字");
out.write("\n");
}
if(iseprate((char)temp))
{

String s=new String(character);
System.out.println(s+" 分隔符");
out.write(s+" 分隔符");
out.write("\n");
}

if(isoperate((char)temp)){//判断是否是操作符并保存操作符到相应的数组
operate[operatecount++]=(char)temp;
operateflag++;
//continue;
}
else if(operateflag!=0){
oresult=analystoperate(operate);//分析操作符数组
if(oresult.flag==1){//输出正确的操纵符
out.write(oresult.string+" 操作符"+"\n");
System.out.println(oresult.string+" 操作符");
}
else{//输出错误操作符
out.write(oresult.string+" 操作符"+" ERROR"+"\n");
System.out.println(oresult.string+" 操作符"+" ERROR");
}

for(int count=0;count<get.maxsize;count++){//操作符数组归零
operate[count]='\0';
}
operateflag=0;
operatecount=0;
}

flag=0;
for(int count=0;count<result.string.length();count++)//标示符数组归零
temparray[count]='\0';
continue;
}
}
System.out.println("分析完毕");
in.close();
out.close();

}
}

/*程序说明:
* 此程序用于简单C语言程序的词法分析,如果想扩大其检索的范围包括语言的种类等等,可在Tools类内部进行扩充文件。
* 此程序有一个弊端,操作符的输出有一定的滞后性,还有待改进
*/

热心网友 时间:2023-10-24 08:44

我每次看见你们提问一句话,描述不完整的时候我就只想说,其实我也不知道你在说什么。

热心网友 时间:2023-10-24 08:43

这个是我做编译原理词法分析用的,可以识别包括,标示符,关键字,常数,变量,操作符,界符等,你先拿去用吧!!

import java.io.FileReader;
//import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

class Tools{
public final int maxsize=200;
String savwd[]={"main","int","float","char","double","long","return","if","case","switch","break","default","printf","scanf","define","include"};//保留字数组;
char operate[]={'=','>','<','?','!','+','-','*','/','%','(',')',':'};//操作符数组
char seprate[]={',',';','{','}','"'};//分隔符数组
String doperate[]={"==","+=","-=",">=","<=","!=","=","+","-","<",">","!","*","/","%","(",")","++","--","()"};
}

class Monitor{
public int flag;
public String string;
}

public class test {
static Monitor analyste(char array[]){//对读取的窜进行分析
Tools get=new Tools();
Monitor monitor=new Monitor();
int flag=0,numbercount=0;
int countnumber;
for(countnumber=0;array[countnumber]!='\0';countnumber++);
String str=new String(array);
str=str.substring(0,countnumber);
for(int count=0;count<get.savwd.length;count++)
if(str.equals(get.savwd[count]))
{
flag=1;
break;
}
if(flag==1){//是否为关键字
monitor.flag=1 ;
}
else if(array[0]>='0'&&array[0]<='9'){//判断是否为常量
numbercount=1;
for(int count=numbercount;count<str.length();count++)
if(array[count]>='0'&&array[count]<='9')
numbercount++;
if(numbercount<str.length()){//判断是否为错误表示符
monitor.flag=2;
}
else if(numbercount==str.length()){//判断是否是常量
monitor.flag=3;
}
}
else if((array[0]>='a'&&array[0]<='z')||(array[0]>='A'&&array[0]<='Z')){//判断是否是表示符
monitor.flag=4;
}
monitor.string=str;
return monitor;
}

static boolean iseprate(char o)//判断是否是分割符
{
Tools get=new Tools();
int flag=0;
for(int count=0;count<get.seprate.length;count++){
if(o==get.seprate[count]){
flag=1;
break;
}
}

return (flag==1);
}

static boolean isoperate(char c){//判断是否是运算符
Tools get=new Tools();
int flag=0;
for(int count=0;count<get.operate.length;count++){
if(c==(get.operate[count])){
flag=1;
break;
}
}
return (flag==1);
}

static Monitor analystoperate(char doubles[])//此函数用于分析操作数组,并返回分析状态值给MONITOR,共输出程序调用
{
int count;
int flag=0;
Tools tl=new Tools();
Monitor temp=new Monitor();
String str=new String(doubles);
for( count=0;doubles[count]!='\0';count++);
str=str.substring(0,count);
for(int i=0;i<tl.doperate.length;i++){//判断操作符的合法性,并且给MONITOR 设定状态
if(str.equals(tl.doperate[i])){
flag=1;
break;
}
}
temp.flag=flag;
temp.string=str;
return temp;
}

public static void main(String args[])throws IOException{
Tools get=new Tools();

FileReader in=new FileReader("C:\\myjava\\testdata.txt");
FileOutputStream file=new FileOutputStream("C:\\myjava\\result.txt",true);
OutputStreamWriter out=new OutputStreamWriter(file);
int temp;
int flag=0;
char operate[]=new char[get.maxsize];
int operatecount=0;
int operateflag=0;
Monitor result=new Monitor();
Monitor oresult=new Monitor();

char temparray[]=new char[get.maxsize];
System.out.println("符号 名称 说明");
out.write("符号 名称 说明"+"\n");
System.out.println();
while((temp=in.read())!=-1){//读文件
if(temp!=' '&&!iseprate((char)temp)&&!isoperate((char)temp)&&temp!='\n')
temparray[flag++]=(char)temp;
else {
result=analyste(temparray);
char character[]={(char)temp};
String word=result.string;
if(result.flag==1){
System.out.println(word+" 关键字");
out.write(word+" 关键字");
out.write("\n");
}
if(result.flag==2){
System.out.println(word+" 标示符 error");
out.write(word+" 标示符 error");
out.write("\n");
}
if(result.flag==3){
System.out.println(word+" 常量 ");
out.write(word+" 常量 ");
out.write("\n");
}
if(result.flag==4){
System.out.println(word+" 标示符");
out.write(word+" 标示符");
out.write("\n");
}
if(result.flag==5){
System.out.println(word+" 关键字");
out.write(word+" 关键字");
out.write("\n");
}
if(iseprate((char)temp))
{

String s=new String(character);
System.out.println(s+" 分隔符");
out.write(s+" 分隔符");
out.write("\n");
}

if(isoperate((char)temp)){//判断是否是操作符并保存操作符到相应的数组
operate[operatecount++]=(char)temp;
operateflag++;
//continue;
}
else if(operateflag!=0){
oresult=analystoperate(operate);//分析操作符数组
if(oresult.flag==1){//输出正确的操纵符
out.write(oresult.string+" 操作符"+"\n");
System.out.println(oresult.string+" 操作符");
}
else{//输出错误操作符
out.write(oresult.string+" 操作符"+" ERROR"+"\n");
System.out.println(oresult.string+" 操作符"+" ERROR");
}

for(int count=0;count<get.maxsize;count++){//操作符数组归零
operate[count]='\0';
}
operateflag=0;
operatecount=0;
}

flag=0;
for(int count=0;count<result.string.length();count++)//标示符数组归零
temparray[count]='\0';
continue;
}
}
System.out.println("分析完毕");
in.close();
out.close();

}
}

/*程序说明:
* 此程序用于简单C语言程序的词法分析,如果想扩大其检索的范围包括语言的种类等等,可在Tools类内部进行扩充文件。
* 此程序有一个弊端,操作符的输出有一定的滞后性,还有待改进
*/

热心网友 时间:2023-10-24 08:44

我每次看见你们提问一句话,描述不完整的时候我就只想说,其实我也不知道你在说什么。

热心网友 时间:2023-10-24 08:43

这个是我做编译原理词法分析用的,可以识别包括,标示符,关键字,常数,变量,操作符,界符等,你先拿去用吧!!

import java.io.FileReader;
//import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

class Tools{
public final int maxsize=200;
String savwd[]={"main","int","float","char","double","long","return","if","case","switch","break","default","printf","scanf","define","include"};//保留字数组;
char operate[]={'=','>','<','?','!','+','-','*','/','%','(',')',':'};//操作符数组
char seprate[]={',',';','{','}','"'};//分隔符数组
String doperate[]={"==","+=","-=",">=","<=","!=","=","+","-","<",">","!","*","/","%","(",")","++","--","()"};
}

class Monitor{
public int flag;
public String string;
}

public class test {
static Monitor analyste(char array[]){//对读取的窜进行分析
Tools get=new Tools();
Monitor monitor=new Monitor();
int flag=0,numbercount=0;
int countnumber;
for(countnumber=0;array[countnumber]!='\0';countnumber++);
String str=new String(array);
str=str.substring(0,countnumber);
for(int count=0;count<get.savwd.length;count++)
if(str.equals(get.savwd[count]))
{
flag=1;
break;
}
if(flag==1){//是否为关键字
monitor.flag=1 ;
}
else if(array[0]>='0'&&array[0]<='9'){//判断是否为常量
numbercount=1;
for(int count=numbercount;count<str.length();count++)
if(array[count]>='0'&&array[count]<='9')
numbercount++;
if(numbercount<str.length()){//判断是否为错误表示符
monitor.flag=2;
}
else if(numbercount==str.length()){//判断是否是常量
monitor.flag=3;
}
}
else if((array[0]>='a'&&array[0]<='z')||(array[0]>='A'&&array[0]<='Z')){//判断是否是表示符
monitor.flag=4;
}
monitor.string=str;
return monitor;
}

static boolean iseprate(char o)//判断是否是分割符
{
Tools get=new Tools();
int flag=0;
for(int count=0;count<get.seprate.length;count++){
if(o==get.seprate[count]){
flag=1;
break;
}
}

return (flag==1);
}

static boolean isoperate(char c){//判断是否是运算符
Tools get=new Tools();
int flag=0;
for(int count=0;count<get.operate.length;count++){
if(c==(get.operate[count])){
flag=1;
break;
}
}
return (flag==1);
}

static Monitor analystoperate(char doubles[])//此函数用于分析操作数组,并返回分析状态值给MONITOR,共输出程序调用
{
int count;
int flag=0;
Tools tl=new Tools();
Monitor temp=new Monitor();
String str=new String(doubles);
for( count=0;doubles[count]!='\0';count++);
str=str.substring(0,count);
for(int i=0;i<tl.doperate.length;i++){//判断操作符的合法性,并且给MONITOR 设定状态
if(str.equals(tl.doperate[i])){
flag=1;
break;
}
}
temp.flag=flag;
temp.string=str;
return temp;
}

public static void main(String args[])throws IOException{
Tools get=new Tools();

FileReader in=new FileReader("C:\\myjava\\testdata.txt");
FileOutputStream file=new FileOutputStream("C:\\myjava\\result.txt",true);
OutputStreamWriter out=new OutputStreamWriter(file);
int temp;
int flag=0;
char operate[]=new char[get.maxsize];
int operatecount=0;
int operateflag=0;
Monitor result=new Monitor();
Monitor oresult=new Monitor();

char temparray[]=new char[get.maxsize];
System.out.println("符号 名称 说明");
out.write("符号 名称 说明"+"\n");
System.out.println();
while((temp=in.read())!=-1){//读文件
if(temp!=' '&&!iseprate((char)temp)&&!isoperate((char)temp)&&temp!='\n')
temparray[flag++]=(char)temp;
else {
result=analyste(temparray);
char character[]={(char)temp};
String word=result.string;
if(result.flag==1){
System.out.println(word+" 关键字");
out.write(word+" 关键字");
out.write("\n");
}
if(result.flag==2){
System.out.println(word+" 标示符 error");
out.write(word+" 标示符 error");
out.write("\n");
}
if(result.flag==3){
System.out.println(word+" 常量 ");
out.write(word+" 常量 ");
out.write("\n");
}
if(result.flag==4){
System.out.println(word+" 标示符");
out.write(word+" 标示符");
out.write("\n");
}
if(result.flag==5){
System.out.println(word+" 关键字");
out.write(word+" 关键字");
out.write("\n");
}
if(iseprate((char)temp))
{

String s=new String(character);
System.out.println(s+" 分隔符");
out.write(s+" 分隔符");
out.write("\n");
}

if(isoperate((char)temp)){//判断是否是操作符并保存操作符到相应的数组
operate[operatecount++]=(char)temp;
operateflag++;
//continue;
}
else if(operateflag!=0){
oresult=analystoperate(operate);//分析操作符数组
if(oresult.flag==1){//输出正确的操纵符
out.write(oresult.string+" 操作符"+"\n");
System.out.println(oresult.string+" 操作符");
}
else{//输出错误操作符
out.write(oresult.string+" 操作符"+" ERROR"+"\n");
System.out.println(oresult.string+" 操作符"+" ERROR");
}

for(int count=0;count<get.maxsize;count++){//操作符数组归零
operate[count]='\0';
}
operateflag=0;
operatecount=0;
}

flag=0;
for(int count=0;count<result.string.length();count++)//标示符数组归零
temparray[count]='\0';
continue;
}
}
System.out.println("分析完毕");
in.close();
out.close();

}
}

/*程序说明:
* 此程序用于简单C语言程序的词法分析,如果想扩大其检索的范围包括语言的种类等等,可在Tools类内部进行扩充文件。
* 此程序有一个弊端,操作符的输出有一定的滞后性,还有待改进
*/

热心网友 时间:2023-10-24 08:44

我每次看见你们提问一句话,描述不完整的时候我就只想说,其实我也不知道你在说什么。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
...DE分别在BC AC上,且AE=CD,连接AD,BE交于点p 过B作BQ⊥AD Q为垂足... ...AC上的点,且AE=CD 连接AD BE 交于点P 作BQ垂 ...ac上的点,且ae=cd,连接ad、be交于点p作BQ⊥AD,垂足为Q,求证BP=2... ...AC上的点,且AE=CD,链接AD、BE交于点P,作BQ⊥AD ...AC上的点,且AE=CD,连结AD、BE交于点P,作BQ⊥AD,垂足为Q. ...刚刚大学毕业。由于生病,导致目前没有工作,大学四年的女友也因为我... 形容“荷花的特点”有什么词语 梦见比人还高大的石头蛤蟆,杀人拆房子。我和其他人一起翘辫子了._百度... 梦见别人送我一条蛇,我却把他不小心放了.可是我用石头打了它一下... 作文 我生活在鼓励中 java中标识符 java 需要标识符 齐河海底世界好玩吗 齐河海底世界上午几点开馆? 现在齐河海底世界门票多少钱 有哪些合法的java标识符??&#xF601; 从济阳县去齐河县海底世界怎么走? 齐河泉城极地海洋世界 齐河海底世界在齐河什麽地方 Java初学者:什么是标识符?关键字有哪些?谢谢! 齐河的海底世界在哪里?齐河县海底世界怎么去? Java中标识符是怎么定义的? 济南齐河海底世界门票价格 齐河海底世界是几级景区啊 齐河海底世界具体位置? 就是去齐河海洋世界,我今天下午到的买了票,不进去 第二天去可以吗 主要怕排队 “齐河”的海洋馆好玩吗? 以前年度有多缴税的情况,该怎么从账上查? 齐河海底世界门票老人是否半票 想知道: 山东省 济南齐河海底世界 在哪 java 错误:需要标识符 安装空调能不能在玻璃上打孔 安装空调时,打孔把钢筋打断了,严重吗?应该怎么处理? 如何给超市取个好名字? 各位,开生鲜超市,取什么名字好 小型超市名字 超市名字,急。。。有创意点的。有趣的。 超市起什么名字好?好听的超市名字大全 超市该起个什么名字好呢 超市 商店起名大全 开超市取什么名字好 开超市起什么名字好 给连锁超市起个好听的名字 抖音直播怎么播放提前录制好的视频? 老师,word中有20个章节,怎样把每个章节的大标题字体同时调整 热水器的隔电墙是什么工作原理?真的能有效隔电吗? 电热水器隔电墙怎么装? 隔电墙是起什么作用的 怎样加入闪送跑腿? 电热水器隔电墙有作用吗