import java.io.*;
import java.util.*;
public class Scanning
{
//存放识别出的token
private ArrayList<Token> lt = new ArrayList<Token>();
//记录取出的token个数
private static int i=0;
//存放所有关键字
private static final String[] reserveWord={"if","else","int","return","void","while"};
//存放源程序路径
private String dir;
//记录行号
private static int lineCount=0;
//源文件每行代码的字符数组的下标,指向当前分析的下一个字符
private int index=0;
private String token;
//注释起止标志
private boolean isComm = false;
//构造方法
public Scanning(String dir)
{
this.dir=dir;
}
//词法分析的算法实现
public void scanAll(){
File f=new File(dir);
try
{
if(!f.exists())
{
System.out.println("源文件不存在");
}
else
{
BufferedReader br=new BufferedReader(new FileReader(dir));
String s;
//开始词法分析,源文件一行一行地读入
while((s=br.readLine())!=null){
lineCount++;
//过滤行字符串,截去字符串开头和末尾的空白
s = s.trim();
System.out.println(lineCount + ":" + s);
//将每行的字符串转为字符数组
char[] c = s.toCharArray();
//将此行源代码一个字符一个字符地扫描
while(index < c.length){
token = " ";
char ch = this.getChar(c);
switch(ch){
//此字符为字母
case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':
case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case 'v':case 'w':case 'x':
case 'y':case 'z':case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':case 'H':case 'I':case 'J':
case 'K':case 'L':case 'M':case 'N':case 'O':case 'P':case 'Q':
case 'R':case 'S':case 'T':case 'U':case 'V':case 'W':case 'X':case 'Y':case 'Z':
token = this.contact(token, ch);
if(index == c.length){
//全部由字母组成,token判断为id
if(!isComm) lt.add(new Token("id",token,lineCount));
break;
}else{
ch=this.getNextChar(c);
while(this.isLetter(ch)){
token=this.contact(token, ch);
if(index==c.length) break;
else ch=this.getNextChar(c);
}
}
//回退一个字符的位置
if(index <= c.length) this.retract();
//判断此字符串为标识符
token=token.trim();
//检查此字符串是否为保留字
if(this.isReserve(token)){
if(!isComm) System.out.println(" " + " " + "reserved word:" + token);
if(!isComm) lt.add(new Token(token,token,lineCount));
}else{
if(!isComm) System.out.println(" " + " " + "ID: " + token );
if(!isComm) lt.add(new Token("id",token,lineCount));
}
token=" ";
break;
//此字符为数字
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9':
token=this.contact(token, ch);
if(index==c.length){
if(!isComm) lt.add(new Token("num",token,lineCount));
break;
}else{
ch=this.getNextChar(c);
while(this.isDigit(ch)){
token=this.contact(token, ch);
if(index==c.length) break;
else ch=this.getNextChar(c);
}
}
if(index<=c.length) this.retract();
//判断此字符串为数字
token = token.trim();
if(!isComm) System.out.println(" " + " " + "num: " + token );
if(!isComm) lt.add(new Token("num", token, lineCount));
token=" ";
break;
case '=':
token=this.contact(token, ch);
ch=this.getNextChar(c);
//如果下一个字符为 = ,则为关系运算符
if(ch=='='){
index ++;
token=this.contact(token, ch);
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("relop",token,lineCount));
}else{
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("equalop",token,lineCount));
}
this.retract();
token=" ";
break;
case '!':
token=this.contact(token, ch);
ch=this.getNextChar(c);
//如果下一个字符为=,则为不等号
if(ch=='='){
index ++;
token=this.contact(token, ch);
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token);
if(!isComm) lt.add(new Token("relop",token,lineCount));
}
this.retract();
token=" ";
break;
case '<':
token=this.contact(token, ch);
ch=this.getNextChar(c);
if(ch=='='){
index++;
token=this.contact(token, ch);
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token);
if(!isComm) lt.add(new Token("relop",token,lineCount));
}else{
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token);
if(!isComm) lt.add(new Token("relop",token,lineCount));
}
this.retract();
token=" ";
break;
case '>':
token=this.contact(token, ch);
ch=this.getNextChar(c);
if(ch=='='){
index++;
token=this.contact(token, ch);
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("relop",token,lineCount));
}else{
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("relop",token,lineCount));
}
this.retract();
token=" ";
break;
case '+':
token=this.contact(token, ch);
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("addop",token,lineCount));
token=" ";
break;
case '-':
token=this.contact(token, ch);
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("addop",token,lineCount));
token=" ";
break;
case '/':
token=this.contact(token, ch);
ch=this.getNextChar(c);
//如果下一个字符为 * ,则为注释符号
if(ch=='*'){
index ++;
isComm=true;
}else{
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("mulop",token,lineCount));
}
this.retract();
token=" ";
break;
case '*':
token=this.contact(token, ch);
ch=this.getNextChar(c);
//如果下一个字符为 / ,则为注释符号
if(ch=='/'){
index ++;
isComm=false;
}else{
if(!isComm) System.out.println(" " + " " + "SpecialSymbol: " + token );
if(!isComm) lt.add(new Token("mulop",token,lineCount));
}
this.retract();
token=" ";
break;
case '(':
token=this.contact(toke
没有合适的资源?快使用搜索试试~ 我知道了~
递归下降法实现语法分析器(java)
共13个文件
class:5个
java:5个
project:1个
4星 · 超过85%的资源 需积分: 30 97 下载量 21 浏览量
2011-08-09
06:35:27
上传
评论 5
收藏 15KB ZIP 举报
温馨提示
递归下降法实现语法分析器源代码(java语言编写),将src文件导入eclipse工程即可运行处结果。
资源推荐
资源详情
资源评论
收起资源包目录
parser.zip (13个子文件)
parser
.project 382B
bin
Scanning.class 7KB
Token.class 702B
TreeNode.class 378B
Main.class 1KB
Parsing.class 8KB
.settings
org.eclipse.jdt.core.prefs 629B
src
Scanning.java 11KB
Parsing.java 9KB
Main.java 455B
TreeNode.java 252B
Token.java 391B
.classpath 301B
共 13 条
- 1
资源评论
- yangqianqian2282012-05-16我学习的是java,但是还是不会,也就随便看了看~~
- shitiantian2014-06-30不错的程序,很好的,可以运行
- feng20093035322012-06-11思路挺清晰的,基本上就是C语言版翻译过来的。但是没什么新意,不是面向对象的写法,最让我佩服的是作者判断是英文字母和数字时竟然用switch语句挨个匹配,碉堡了。。。
- chi_bny2019-10-26不行啊,我试了个错的代码,没有报错,而且结果没有显示推导过程,结果显示就像是单纯的此法分析,这代码对我没有用唉
lin_yang89
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功