如何用JAVA编写词法分析器程序 如何用JAVA编写词法分析器程序 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我也做过这个作业package source;import java.util.LinkedList;public class LexicalAnalysis { //私有变量声明 private LinkedList<Word> optr = new LinkedList<Word>(); private String exp; //词法分析 public LinkedList<Word> lexical_analysis(String exp) { char ch = '\0'; //当前文件指针内容 int index = 0; //文件指针 StringBuffer strToken = new StringBuffer(""); //扫描处理字符串 while(true) { ch = exp.charAt(index); index++; //标识符(字母开头,数字或字符组成) if(Character.isLetter(ch)) { while(Character.isLetter(ch) || Character.isDigit(ch)) { strToken.append(ch); ch = exp.charAt(index); index++; } index--; String str = strToken.toString(); if(str.equals("if")) optr.add(new Word(str, 13)); else if(str.equals("else")) optr.add(new Word(str, 14)); else if(str.equals("then")) optr.add(new Word(str, 15)); else optr.add(new Word(str, 26)); } //数字 else if(Character.isDigit(ch)) { while(Character.isDigit(ch)) { strToken.append(ch); ch = exp.charAt(index); index++; } index--; optr.add(new Word(strToken.toString(), 26)); } //加号或自加 else if(ch == '+') { ch = exp.charAt(index); index++; if(ch == '+') optr.add(new Word("++", 21)); else if(ch == '=') optr.add(new Word("+=", 16)); else { index--; optr.add(new Word("+", 19)); } } //加号或自加 else if(ch == '-') { ch = exp.charAt(index); index++; if(ch == '-') optr.add(new Word("--", 21)); else if(ch == '=') optr.add(new Word("-=", 16)); else { index--; optr.add(new Word("-", 19)); } } //乘法或乘幂 else if(ch == '*') { ch = exp.charAt(index); index++; if(ch == '*') optr.add(new Word("**", 20)); else if(ch == '=') optr.add(new Word("*=", 16)); else { index--; optr.add(new Word("*", 20)); } } //除法或注释 else if(ch == '/') { ch = exp.charAt(index); index++; //多行注释 if(ch == '*') { while(true) { ch = exp.charAt(index); index++; if(ch == '*') { ch = exp.charAt(index); index++; if(ch == '/') break; else if(ch == '\n') { exp = Input.newLine(); index = 0; ch = exp.charAt(index); index++; } else index--; } else if(ch == '#') { int tIndex = index - 1; if(exp.length() > tIndex+9) { String end = exp.substring(tIndex, tIndex+9); if(end.equals("#?e_N_d?#")) break; } else { System.out.println("非法符号\'#\'后的语句忽略!"); exp = Input.newLine(); index = 0; break; } } else if(ch == '\n') { exp = Input.newLine(); index = 0; } } } //单行注释 else if(ch == '/') break; else if(ch == '=') optr.add(new Word("/=", 16)); else { index--; optr.add(new Word("/", 20)); } } //大于或大于等于或右移 else if(ch == '>') { ch = exp.charAt(index); index++; if(ch == '=') optr.add(new Word(">=", 18)); else if(ch == '>') optr.add(new Word(">>", 20)); else { index--; optr.add(new Word(">", 18)); } } //小于或小于等于或左移 else if(ch == '<') { ch = exp.charAt(index); index++; if(ch == '=') optr.add(new Word("<=", 18)); else if(ch == '<') optr.add(new Word("<<", 20)); else { index--; optr.add(new Word("<", 18)); } } //赋值或等于 else if(ch == '=') { ch = exp.charAt(index); index++; if(ch == '=') optr.add(new Word("==", 18)); else { index--; optr.add(new Word("=", 16)); } } //或运算按位或 else if(ch == '|') { ch = exp.charAt(index); index++; if(ch == '|') optr.add(new Word("||", 17)); else { index--; optr.add(new Word("|", 20)); } } //与运算 else if(ch == '&') { ch = exp.charAt(index); index++; if(ch == '&') optr.add(new Word("&&", 17)); else { index--; optr.add(new Word("&", 20)); } } //非运算或不等于 else if(ch == '!') { ch = exp.charAt(index); index++; if(ch == '=') optr.add(new Word("!=", 18)); else { index--; optr.add(new Word("!", 21)); } } //按位亦或 else if(ch == '^') optr.add(new Word("^", 20)); //取模运算 else if(ch == '%') optr.add(new Word("%", 20)); //左括号 else if(ch == '(') optr.add(new Word("(", 22)); //右括号 else if(ch == ')') optr.add(new Word(")", 23)); //左大括号 else if(ch == '{') optr.add(new Word("{", 24)); //右大括号 else if(ch == '}') optr.add(new Word("}", 25)); //结束扫描标志为:#?e_N_d?# else if(ch == '\n') { break; } else if(ch == '#') { int tIndex = index - 1; if(exp.length() > tIndex+9) { String end = exp.substring(tIndex, tIndex+9); if(end.equals("#?e_N_d?#")) { optr.add(new Word("#", 27)); break; } } else { System.out.println("非法符号\'#\'后的语句忽略!"); optr.add(new Word("#", 27)); break; } } //清空扫描串 strToken.setLength(0); } return optr; }} 警告: Failed to load the sqljdbc_auth.dll 类加载的一个问题! 为什么会重复的读取文件??? 关于IO,求问 有一年以上工作经验的进! java中怎样实现自定义事件 Java里面怎么调用Com程序,望高手指教 关于CallalbleStatement的问题 重分求weblogic文档 VJ最新版是?.0(新手) scjp 关于main的问题 今天做的一个小题: 要用到两个变量 ,是俩日期:- 一个从数据库中读出来后, 月份和日子为单数是自动加零的。而另一个没有从数据库中读,月份单数不加零的,请问大虾们 java 中哪个类的方法可以转换它啊?。 感谢。。
{
//私有变量声明
private LinkedList<Word> optr = new LinkedList<Word>();
private String exp;
//词法分析
public LinkedList<Word> lexical_analysis(String exp)
{
char ch = '\0'; //当前文件指针内容
int index = 0; //文件指针
StringBuffer strToken = new StringBuffer("");
//扫描处理字符串
while(true)
{
ch = exp.charAt(index);
index++;
//标识符(字母开头,数字或字符组成)
if(Character.isLetter(ch))
{
while(Character.isLetter(ch) || Character.isDigit(ch))
{
strToken.append(ch);
ch = exp.charAt(index);
index++;
}
index--;
String str = strToken.toString();
if(str.equals("if"))
optr.add(new Word(str, 13));
else if(str.equals("else"))
optr.add(new Word(str, 14));
else if(str.equals("then"))
optr.add(new Word(str, 15));
else
optr.add(new Word(str, 26));
}
//数字
else if(Character.isDigit(ch))
{
while(Character.isDigit(ch))
{
strToken.append(ch);
ch = exp.charAt(index);
index++;
}
index--;
optr.add(new Word(strToken.toString(), 26));
}
//加号或自加
else if(ch == '+')
{
ch = exp.charAt(index);
index++;
if(ch == '+')
optr.add(new Word("++", 21));
else if(ch == '=')
optr.add(new Word("+=", 16));
else
{
index--;
optr.add(new Word("+", 19));
}
}
//加号或自加
else if(ch == '-')
{
ch = exp.charAt(index);
index++;
if(ch == '-')
optr.add(new Word("--", 21));
else if(ch == '=')
optr.add(new Word("-=", 16));
else
{
index--;
optr.add(new Word("-", 19));
}
}
//乘法或乘幂
else if(ch == '*')
{
ch = exp.charAt(index);
index++;
if(ch == '*')
optr.add(new Word("**", 20));
else if(ch == '=')
optr.add(new Word("*=", 16));
else
{
index--;
optr.add(new Word("*", 20));
}
}
//除法或注释
else if(ch == '/')
{
ch = exp.charAt(index);
index++;
//多行注释
if(ch == '*')
{
while(true)
{
ch = exp.charAt(index);
index++;
if(ch == '*')
{
ch = exp.charAt(index);
index++;
if(ch == '/') break;
else if(ch == '\n')
{
exp = Input.newLine();
index = 0;
ch = exp.charAt(index);
index++;
}
else index--;
}
else if(ch == '#')
{
int tIndex = index - 1;
if(exp.length() > tIndex+9)
{
String end = exp.substring(tIndex, tIndex+9);
if(end.equals("#?e_N_d?#")) break;
}
else
{
System.out.println("非法符号\'#\'后的语句忽略!");
exp = Input.newLine();
index = 0;
break;
}
}
else if(ch == '\n')
{
exp = Input.newLine();
index = 0;
}
}
}
//单行注释
else if(ch == '/')
break;
else if(ch == '=')
optr.add(new Word("/=", 16));
else
{
index--;
optr.add(new Word("/", 20));
}
}
//大于或大于等于或右移
else if(ch == '>')
{
ch = exp.charAt(index);
index++;
if(ch == '=')
optr.add(new Word(">=", 18));
else if(ch == '>')
optr.add(new Word(">>", 20));
else
{
index--;
optr.add(new Word(">", 18));
}
}
//小于或小于等于或左移
else if(ch == '<')
{
ch = exp.charAt(index);
index++;
if(ch == '=')
optr.add(new Word("<=", 18));
else if(ch == '<')
optr.add(new Word("<<", 20));
else
{
index--;
optr.add(new Word("<", 18));
}
}
//赋值或等于
else if(ch == '=')
{
ch = exp.charAt(index);
index++;
if(ch == '=')
optr.add(new Word("==", 18));
else
{
index--;
optr.add(new Word("=", 16));
}
}
//或运算按位或
else if(ch == '|')
{
ch = exp.charAt(index);
index++;
if(ch == '|')
optr.add(new Word("||", 17));
else
{
index--;
optr.add(new Word("|", 20));
}
}
//与运算
else if(ch == '&')
{
ch = exp.charAt(index);
index++;
if(ch == '&')
optr.add(new Word("&&", 17));
else
{
index--;
optr.add(new Word("&", 20));
}
}
//非运算或不等于
else if(ch == '!')
{
ch = exp.charAt(index);
index++;
if(ch == '=')
optr.add(new Word("!=", 18));
else
{
index--;
optr.add(new Word("!", 21));
}
}
//按位亦或
else if(ch == '^')
optr.add(new Word("^", 20));
//取模运算
else if(ch == '%')
optr.add(new Word("%", 20));
//左括号
else if(ch == '(')
optr.add(new Word("(", 22));
//右括号
else if(ch == ')')
optr.add(new Word(")", 23));
//左大括号
else if(ch == '{')
optr.add(new Word("{", 24));
//右大括号
else if(ch == '}')
optr.add(new Word("}", 25));
//结束扫描标志为:#?e_N_d?#
else if(ch == '\n')
{
break;
}
else if(ch == '#')
{
int tIndex = index - 1;
if(exp.length() > tIndex+9)
{
String end = exp.substring(tIndex, tIndex+9);
if(end.equals("#?e_N_d?#"))
{
optr.add(new Word("#", 27));
break;
}
}
else
{
System.out.println("非法符号\'#\'后的语句忽略!");
optr.add(new Word("#", 27));
break;
}
}
//清空扫描串
strToken.setLength(0);
}
return optr;
}
}