当前位置:网站首页>Lexical analyzer for compiling principle notes
Lexical analyzer for compiling principle notes
2022-06-11 03:57:00 【Java Development Zone】
package com.xawl.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class WordImplements {
/* * 1 For keywords * 2 Represent identifier * 3 It's a constant * 4 Representation operator * 5 Representation delimiter * 6 Representation string * */
// keyword
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"};
// Operator
static String []operation={
"+","-","*","/","%","++","--","-=","*=","/=","&","|","^","~","<<",">>",">>>","==","!=",
">","<","=",">=","<=","&&","||","!","."};
// Boundary symbol
static String []symbol={
",",";",":","(",")","{","}"};
static ArrayList<String> keyWords=null;
static ArrayList<String> operations=null;
static ArrayList<String> symbols=null;
// Pointer to the position of the currently read string
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++;
}
}
}
// Initialization converts the array to 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);
}
}
}
/* Identification of numbers * 1、 Identify exit : * 1.1、 Space character encountered * 1.2、 Encountered operator or delimiter * 2、 Wrong situation : * 2.1、 Two or more decimal points * 2.2、 Doped letters * */
public static void digitCheck(String str){
String token= String.valueOf(str.charAt(p++));
// Judge whether the decimal point of the number has and is greater than 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--;
}
}
// identifier , Keyword identification
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--;
}
}
// Identification of symbols
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");
}
}
}
// String checking
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+")");
}
}
}
边栏推荐
- Docker builds a redis Cluster - three machines, three masters and three slaves
- Thoughts on the number of threads and CPU caused by the CPU load high alarm
- Le tri le plus couramment utilisé de JS - - série JS déchiquetée à la main
- Go failing - expected ‘package‘, found ‘EOF‘
- OPENSSL ASN. 1, DER, PEM, X509
- 常用测试用例设计方法之场景法详解
- Student teacher examination management system based on SSM framework
- Understand single chip microcomputer drive 8080lcd
- 雷达辐射源调制信号仿真
- Writing shell scripts using vscode
猜你喜欢

Management system of College Students' associations based on SSM

基于SSM框架的学生老师考试管理系统

Chinese classics for children

JMeter piezometric interface programming North

基于SSM框架的学生在线教育教学课程管理系统

OpenGL Chapter 7 basic lighting

SQL注入关联分析

Large factory outsourcing or self research company? How to choose a job for a tester?

Docker swarm installs redis cluster (bitnami/redis cluster:latest)

人与人的一些不同
随机推荐
Market prospect analysis and Research Report of marking laser in 2022
高考结束,可以干什么事情,专业应该如何选择?-- 来自一个在校大学生的肺腑之言
基于SSM框架的学生老师考试管理系统
[elt.zip] openharmony paper Club - electronic device software update compression
【CNN】|How much position information do convolutional neural networks encode?
How PTP helps several major operators meet 5g timing requirements
OpenGl第九章 光照贴图
Rhel7 switch character encoding to GBK
After the installation of Damon database is completed, query whether it is case sensitive
让人感到心灵平静,阳光温暖的图片
Maui migration guide
Management system of College Students' associations based on SSM
Discussion on the development trend of remote power management unit (Intelligent PDU)
【网络篇】套接字编程
Record a VMware problem
Composition and configuration of GPS Beidou clock synchronization (satellite time synchronization system) in power plant
A - Eddy‘s AC难题(C语言)
Several time synchronization methods of Beidou timing system (GPS timing equipment)
MAUI 遷移指南
OpenSSL enc encryption and decryption