java实现词法分析器

package com;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;






public class WordAnalysis {
public static int length = 0;
static WordsType sym = WordsType.SYM_IDENTIFIER;// 识别出来的字符串的类型
static String id = ""; // 识别出来的字符串
static Integer countID = 1; // 记录标示符列表
static Integer countNUM = 1;// 记录数组列表

static Map<String, Integer> comMap = new HashMap<String, Integer>();// 普通类型的列表
static Map<String, Integer> idMap = new HashMap<String, Integer>();// 标示符列表
static Map<String, Integer> numMap = new HashMap<String, Integer>();// 数字列表


public enum WordsType {
SYM_IDENTIFIER, // 标识符
SYM_NUMBER, // 常数
SYM_PLUS, // +
SYM_MINUS, // -
SYM_TIMES, // *
SYM_SLASH, // /
SYM_ODD, // odd
SYM_EQU, // =
SYM_NEQ, // <>
SYM_LES, // <
SYM_LEQ, // <=
SYM_GTR, // >
SYM_GEQ, // >=
SYM_LPAREN, // (
SYM_RPAREN, // )
SYM_COMMA, // ,
SYM_SEMICOLON, // ;
SYM_PERIOD, // .
SYM_BECOMES, // :=
SYM_BEGIN, // begin
SYM_END, // end
SYM_IF, // if
SYM_THEN, // then
SYM_WHILE, // while
SYM_DO, // do
SYM_CONST, // const
SYM_VAR, // var
SYM_CALL, // call
SYM_PROCEDURE; // procedure
public String toString(WordsType wt){
String word="";
switch(wt){
case SYM_IDENTIFIER:word="id";break;
case SYM_PLUS:word="+";break;
case SYM_TIMES:word="*";break;
case SYM_MINUS:word="-";break;
case SYM_SLASH:word="/";break;
case SYM_LPAREN:word="(";break;
case SYM_RPAREN:word=")";break;
case SYM_NUMBER:word="num";break;
}
return word;
}
}


// 其中len是开始位置


public static WordsType analysis(char[] temp, int len) {


String sys = "";
length = len;
while (temp[length] == '\t' || temp[length] == ' ')
length++; // 过滤制表符,空格

// 字母序列
if (length < temp.length && isLetter(temp[length])) {
while (length < temp.length && temp[length] != ' '
&& isLetter(temp[length])) {
sys += temp[length];
length++;
}
id = sys;
WordsType wt = reserve(id);
if(length==temp.length)
{
comMap.put(id ,null);

}
if (length < temp.length && !isLetter(temp[length])) {

if (wt == WordsType.SYM_IDENTIFIER) {
if(!idMap.containsKey(id)){
idMap.put(id, countID);// 标示符列表,可以添加如果标示符已经存在则不再添加
countID++;
}
} else
comMap.put(id, null);
}
return wt;
}
// 数字序列
if (length < temp.length && isDigit(temp[length])) {
while (length < temp.length && temp[length] != ' '
&& isDigit(temp[length])) {
sys += temp[length];
length++;
}
if (length < temp.length && !isDigit(temp[length])) {
// ?是否赋值为id,转型
id = sys;
sym = WordsType.SYM_NUMBER;
if(!numMap.containsKey(id)){
numMap.put(id, countNUM); // 数字列表
countNUM++;
}


return sym;
}
}


// 运算符
if (length < temp.length)
return caculator(temp);
return null;
}


// 判断基本运算
public static WordsType caculator(char[] temp) {
sym = null;
int len = length;
length++;
id = temp[len] + "";
switch (temp[len]) {
case '+':
sym = WordsType.SYM_PLUS;
break;
case '-':
sym = WordsType.SYM_MINUS;
break;
case '*':
sym = WordsType.SYM_TIMES;
break;
case '/':
sym = WordsType.SYM_SLASH;
break;
case '(':
sym = WordsType.SYM_LPAREN;
break;
case ')':
sym = WordsType.SYM_RPAREN;
break;
case ',':
sym = WordsType.SYM_COMMA;
break;
case ';':
sym = WordsType.SYM_SEMICOLON;
break;
case '.':
sym = WordsType.SYM_PERIOD;
break;
case '=':
sym = WordsType.SYM_BECOMES;
break;
}
if (temp[len] == '>' && (len + 1 < temp.length) && temp[len + 1] == '=') {
length++;
id += temp[len + 1];
sym = WordsType.SYM_GEQ;
} else if (temp[len] == '>' && (len + 1 < temp.length)
&& temp[len + 1] != '=') {
sym = WordsType.SYM_GTR;
} else if (temp[len] == '<' && (len + 1 < temp.length)
&& temp[len + 1] != '=') {
sym = WordsType.SYM_LES;
} else if (temp[len] == '<' && (len + 1 < temp.length)
&& temp[len + 1] == '=') {
length++;
id += temp[len + 1];
sym = WordsType.SYM_LEQ;
}

return sym;
}


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


// 判断单个字符是否为数字
public static boolean isDigit(char a) {
if (a >= '0' && a <= '9')
return true;
return false;
}


// 保留字
public static WordsType reserve(String str) {


if ("begin".equals(str))
return WordsType.SYM_BEGIN;
else if ("end".equals(str))
return WordsType.SYM_END;
else if("const".equals(str))
return WordsType.SYM_CONST;
else if ("if".equals(str))
return WordsType.SYM_IF;
else if ("then".equals(str))
return WordsType.SYM_THEN;
else if ("while".equals(str))
return WordsType.SYM_WHILE;
else if ("do".equals(str))
return WordsType.SYM_DO;
else if ("var".equals(str))
return WordsType.SYM_VAR;
else if ("procedure".equals(str))
return WordsType.SYM_PROCEDURE;// 保留字
else
return WordsType.SYM_IDENTIFIER;// 标示符


}


public static List<String> getInputStream(String filename)
throws IOException {


List<String> list = new ArrayList<String>();
FileInputStream fis = new FileInputStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String buffer = "";
while ((buffer = br.readLine()) != null) {
list.add(buffer);
}
return list;
}






public static void output(String str) {
length = 0;// 必须初始化,因为读取文件时每读一行进行清零操作
char[] chs = str.toCharArray();
while (length < str.length()) {
WordsType wt = analysis(chs, length);
if (wt == WordsType.SYM_NUMBER)
System.out.println(wt + ":" + id + ",\t位置:" + numMap.get(id));
else if (wt == WordsType.SYM_IDENTIFIER)
System.out.println(wt + ":" + id + ",\t位置:" + idMap.get(id));
else
System.out.println(wt + ":" + id + ",\t位置:" + comMap.get(id));


}


}


/*public static void main(String[] args) throws IOException {
List<String> list = getInputStream("ypf.txt");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
output(list.get(i));
System.out.println("*************");
}
}*/
}
1.根据状态转换图直接编程 编写一个词法分析程序,它从左到右逐个字符的对源程序进行扫描,产生一个个的单词的二元式,形成二元式(记号)流文件输出。在此,词法分析程序作为单独的一遍,如下图所示。 具体任务有: (1)组织源程序的输入 (2)识别单词的类别并记录类别编号和值,形成二元式输出,得到单词流文件 (3)删除注释、空格和无用符号 (4)发现并定位词法错误,需要输出错误的位置在源程序中的第几行。将错误信息输出到屏幕上。 (5)对于普通标识符和常量,分别建立标识符表和常量表(使用线性表存储),当遇到一个标识符或常量时,查找标识符表或常量表,若存在,则返回位置,否则返回0并且填写符号表或常量表。 标识符表结构:变量名,类型(整型、实型、字符型),分配的数据区地址 注:词法分析阶段只填写变量名,其它部分在语法分析、语义分析、代码生成等阶段逐步填入。 常量表结构:常量名,常量值 单词的构词规则: 字母=[A-Z a-z] 数字=[0-9] 标识符=(字母|_)(字母|数字|_)* 数字=数字(数字)*( .数字+|) 2.S语言表达式和语句说明 1.算术表达式:+、-、*、/、% 2.关系运算符:>、>=、<、<=、==、!= 3.赋值运算符:=,+=、-=、*=、/=、%= 4.变量说明:类型标识符 变量名表; 5.类型标识符:int char float 6.If语句:if 表达式then 语句 [else 语句] 7.For语句:for(表达式1;表达式2;表达式3) 语句 8.While语句:while 表达式 do 语句 9.S语言程序:由函数构成,函数不能嵌套定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值