编译原理实验1
}
}
package com.example;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
wordList.add(wList);
}else if(isFenGeFu(str)!=0){
WordList list = new WordList(row, guanjianzi.size()+isFenGeFu(str)+2, isFenGeFu(str), str);
wordList.add(list);
天津理工大学实验报告
学院(系)名称:计算机与通信工程学院
姓名
学号
专业
班级
实验项目
实验一:词法分析器
课程名称
编译原理
课程代码
实验时间
实验地点
批改意见
成绩
教师签字:
1、实验内容:实现标准C语言词法分析器。
2、实验要求:
(1)单词种别编码要求
基本字、运算符、界符:一符一种
标识符:统一为一种;
常量:按类型编码;
ArrayList<String> fuhaobiao = new ArrayList<>();
ArrayList<String> changLiangBiao = new ArrayList<>();
//将字符加入到符号表中
public ArrayList<String> addToFuHaoBiao(String s){
nc = str.charAt(i+1);
if(ch=='\n'){
ch = '$';
ts = ts+ch;
}else if(ch==' '||ch=='\t'||ch=='\r'){
if(nc==' '||nc=='\t'||nc=='\r'){
continue;
}else{
ch=' ';
ts = ts + ch;
return type;
}
public void setType(int type) {
this.type = type;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}else{
ts = ts+ch;
}
}
ch = str.charAt(str.length()-1);
if(ch!=' '&&ch!='\r'&&ch!='\t'&&ch!='\n'){
ts = ts + ch;
}
return ts;
}
//将一个长字符串按照分割规则,分割成一个个的短的字符串
public ArrayList<Word> fenGe(String str){
e.printStackTrace();
}
return str;
}
//处理字符串中的换行和空格
public String yuChuLi(String str){
String ts = "";
int i;
char ch,nc;
for(i=0;i<str.length()-1;i++){
ch = str.charAt(i);
try {
Reader reader = new FileReader(file);
char c[] = new char[1024];
int len = reader.read(c);
str = new String(c, 0, len);
reader.close();
} catch (Exception e) {
public boolean isIdent(String str){
char ch;
int i;
for(i=0;i<str.length();i++){
ch=str.charAt(i);
if((i==0&&!isLetter(ch))||(!isDigit(ch)&&!isLetter(ch))){
public class HanShu {
//定义符号表数组,常量表数组,关键字数组
List<String> guanjianzi = Arrays.asList("double","long","char","int","if","else","for","while","return"
,"break","continue","switch","case","default","void","struct","static","do","short");
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
@Override
public String toString() {
return "Word [row=" + row + ", string=" + string + "]";
this.row = row;
this.type = type;
this.number = number;
this.str = str;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getType() {
}else
return true;
}
//将一个集合中的数据分类,判断他们是否是关键字或者是常量,亦或是标识符
public ArrayList<WordList> fenlei(ArrayList<Word> arr){
ArrayList<WordList> wordList = new ArrayList<>();
+ ", str=" + str + "]e com.example;
public class Word {
private int row;
private String string;
public Word() {
super();
// TODO Auto-generated constructor stub
public boolean isLetter(char ch) {
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
return true;
}else{
return false;
}
}
//判断一个字符是否是数字
public boolean isDigit(char ch) {
s = s + ch;
}else{
list.add(new Word(row,s));
s = "";
s = s + ch;
}
}else{
if(isTwoOperator(s,ch)){
s = s + ch;
}else{
if(s==""&&ch!='$'){
s = s + ch;
}else if(s==""&&ch=='$'){
List<String> caozuofu = Arrays.asList("+","-","*","/","%","=",">","<","!","==","!=",">=","<=","++","--",
"&","&&","||","[","]");