当前位置:网站首页>编译原理笔记之词法分析器
编译原理笔记之词法分析器
2022-06-11 03:38:00 【JAVA开发区】
package com.xawl.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class WordImplements {
/* * 1表示关键字 * 2表示标识符 * 3表示常数 * 4表示运算符 * 5表示界符 * 6表示字符串 * */
//关键字
static String []keyWord={
"private","protected","public","abstract","class","extends","final","implements",
"interface","native","new","static","strictfp","break","continue","return","do","while","if","else","for",
"instanceof","switch","case","default","boolean","byte","char","double","float","int","long","short",
"String","null","true","false","void","this","goto"};
//运算符
static String []operation={
"+","-","*","/","%","++","--","-=","*=","/=","&","|","^","~","<<",">>",">>>","==","!=",
">","<","=",">=","<=","&&","||","!","."};
//界符
static String []symbol={
",",";",":","(",")","{","}"};
static ArrayList<String> keyWords=null;
static ArrayList<String> operations=null;
static ArrayList<String> symbols=null;
//指向当前所读到字符串的位置的指针
static int p,lines;
public static void main(String []args) throws FileNotFoundException {
init();
File file=new File("E:\\test.txt");
lines=1;
try(Scanner input=new Scanner(file)) {
while (input.hasNextLine()){
String str=input.nextLine();
analyze(str);
lines++;
}
}
}
//初始化把数组转换为ArrayList
public static void init(){
keyWords=new ArrayList<>();
operations=new ArrayList<>();
symbols=new ArrayList<>();
Collections.addAll(keyWords, keyWord);
Collections.addAll(operations, operation);
Collections.addAll(symbols, symbol);
}
public static void analyze(String str){
p=0;
char ch;
str=str.trim();
for (;p<str.length();p++){
ch=str.charAt(p);
if (Character.isDigit(ch)){
digitCheck(str);
}else if (Character.isLetter(ch)||ch=='_'){
letterCheck(str);
}else if (ch=='"'){
stringCheck(str);
}
else if (ch==' '){
continue;
}else {
symbolCheck(str);
}
}
}
/*数字的识别 * 1、识别退出: * 1.1、遇到空格符 * 1.2、遇到运算符或者界符 * 2、错误情况: * 2.1、两个及以上小数点 * 2.2、掺杂字母 * */
public static void digitCheck(String str){
String token= String.valueOf(str.charAt(p++));
//判断数字的小数点是否有且是否大于1
int flag=0;
boolean err=false;
char ch;
for (;p<str.length();p++) {
ch = str.charAt(p);
if (ch==' '||(!Character.isLetterOrDigit(ch)&&ch!='.')) {
break;
}else if (err){
token+=ch;
}
else {
token+=ch;
if (ch == '.') {
if (flag == 1) {
err = true;
} else {
flag++;
}
}else if (Character.isLetter(ch)){
err=true;
}
}
}
if (token.charAt(token.length()-1)=='.'){
err=true;
}
if (err){
System.out.println(lines+"line"+": "+token+" is wrong");
}else {
System.out.println("("+3+","+token+")");
}
if (p!=str.length()-1||(p==str.length()-1&&!Character.isDigit(str.charAt(p)))){
p--;
}
}
//标识符,关键字的识别
public static void letterCheck(String str){
String token= String.valueOf(str.charAt(p++));
char ch;
for (;p<str.length();p++){
ch=str.charAt(p);
if (!Character.isLetterOrDigit(ch)&&ch!='_'){
break;
}else{
token+=ch;
}
}
if (keyWords.contains(token)){
System.out.println("("+1+","+token+")");
}else {
System.out.println("("+2+","+token+")");
}
if (p!=str.length()-1||(p==str.length()-1&&(!Character.isLetterOrDigit(str.charAt(p))&&str.charAt(p)!='_'))){
p--;
}
}
//符号的识别
public static void symbolCheck(String str){
String token= String.valueOf(str.charAt(p++));
char ch;
if (symbols.contains(token)){
System.out.println("("+5+","+token+")");
p--;
}else {
if (operations.contains(token)){
if (p<str.length()){
ch=str.charAt(p);
if (operations.contains(token+ch)){
token+=ch;
p++;
if (p<str.length()){
ch=str.charAt(p);
if (operations.contains(token+ch)){
token+=ch;
System.out.println("("+4+","+token+")");
}else{
p--;
System.out.println("("+4+","+token+")");
}
}else{
System.out.println("("+4+","+token+")");
}
}else {
p--;
System.out.println("("+4+","+token+")");
}
}
}else {
p--;
System.out.println(lines+"line"+": "+token+" is wrong");
}
}
}
//字符串检查
public static void stringCheck(String str){
String token= String.valueOf(str.charAt(p++));
char ch;
for (;p<str.length();p++){
ch=str.charAt(p);
token+=ch;
if (ch=='"'){
break;
}
}
if (token.charAt(token.length()-1)!='"'){
System.out.println(lines+"line"+": "+token+" is wrong");
}else {
System.out.println("("+6+","+token+")");
}
}
}
边栏推荐
- Detailed explanation of scenario method for common test case design methods
- JS to realize coritization
- OPENSSL ASN.1, DER, PEM, X509
- [signalr complete series] Net6 Zhongshi signalr communication
- 代码复现CSRF攻击并解决它
- js实现柯里化
- Solution to the problem of gd32f4 serial port DMA reception
- Run Skynet for the first time
- Picking peaches (double pointer)
- [elt.zip] openharmony paper Club - multi tier storage hierarchical data compression
猜你喜欢

Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?

Writing shell scripts using vscode

Product milestones in May 2022

618 coming! Can oppo reno6, which is sold through all channels with high price and low configuration, win?

/10个值得推荐的学习编程的网站 世界已经进入了互联网的时代。据最近发布的一篇《2016年互联网趋势》报告显示,中国已成为互联网市场的领导者,中国互联网用户的数量达到了6.68亿。可以预见,有

Thinkphp3.2.3 deserialization using chain analysis

Record a VMware problem

JMeter piezometric interface programming North

Run Skynet for the first time

手工测试转不了自动化测试,缺的是什么?
随机推荐
联易融一面(已过)
RHEL7 切换字符编码为GBK
Kirin V10 installation of tongweb7.0
js最常用的排序---手撕js系列
The key data of music genuine rate is missing. What is the odds of Netease cloud music IPO?
什么样的人才是幸福的?
[CNN]|CNN与Transformer区别
开源项目 英雄联盟 之WPF
Iqoo 8 measured hands-on experience: return of the king, never high profile
【SignalR全套系列】之在.Net6中实SignalR通信
Chinese classics for children
Xu Li 618, how can Suning fight this hard battle?
Quel genre de personne est heureuse?
OpenGL Chapter 9 lighting map
什麼樣的人才是幸福的?
Nsthread of the multithreaded Trilogy
[CNN]|平移不变性
What kind of people are happy?
Mavros controls UAV to conduct binocular slam in gazebo environment
PostgreSQL catch exception in function