语法分析器

这是一个Java语法分析器的实现,包含保留字、运算符和界符的处理。代码定义了一个名为`Demo`的类,虽然没有具体实现细节,但可以看出是用于解析和理解Java源代码的结构。
摘要由CSDN通过智能技术生成

package com.analysis4;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
*1.保留字
*2.运算符
*3.界符
*/

public class Demo {

public static int baoliuzi = 0;
public static int yunsuanfu = 0;
public static int biaoshifu = 0;
public static int jiefu = 0;
public static int elsewords = 0;

//将txt文件转为数组
public  String txt2String(File file) throws IOException
{
	StringBuilder result =new StringBuilder();
	try {
		BufferedReader br= new BufferedReader(new FileReader(file));
		String s=null;
		while((s=br.readLine())!=null){
			result.append(System.lineSeparator()+s);
		}
		br.close();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return result.toString();
}

//对文本进行预处理
public  char[] preTreatment(char[] sourcefile)
{
	char []afterfile = new char[10000];
	int index=0;
	if(sourcefile.length!=0)
	{
		for(int i=0;i<sourcefile.length;i++)
		{
			//去掉//注释后的一整行
			if(sourcefile[i]=='/'&&sourcefile[i+1]=='/')
			{
				while(sourcefile[i]!='\n'){
				i++;
				}
			}
			//去掉/**/型注释中间的字符,若只检测到/*,未检测到*/,则提示注释有误
			if(sourcefile[i]=='/'&&sourcefile[i+1]=='*')
			{
				i=i+2;
				while(sourcefile[i]!='*'||sourcefile[i+1]!='/')
				{
					i++;
					if(i==(sourcefile.length-1))
					{
						System.out.println("注释有误,未找到注释尾");
						return afterfile;
					}
				}
				i=i+2;
			}
			if(sourcefile[i]!='\n'&&sourcefile[i]!='\r'&&sourcefile[i]!='\t')
			{
				afterfile[index]=sourcefile[i];
				index++;
			}
		}
		index++;
		afterfile[index]='\0';
	}
	return afterfile;
}

//判断是否为字母
public boolean isLetter(char c)
{
	if((c>='a'&&c<='z')||(c>='A'&&c<'Z'))
	{
		return true;
	}
	else
		return false;
}

//判断是否为保留字,并返回编号
public int isReserve(String s,String []reserve)
{
	int index=-1;
	for(int i=0;i<reserve.length;i++)
	{
		if(s.equals(reserve[i]))
		{
			index=i;
			break;
		}
	}
	
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值