展开dqdzt
我也做过这个作业
包源;
importjava.util.LinkedList;
publicclass淡淡的帅哥/p {
//私有变量声明
privatelinkedlistoptr=new linked list (;
私有字符串执行;
//词法分析
publiclinkedlistlexical _ analysis (string exp )
{
charch=' '; //当前文件指针的内容
intindex=0; //文件指针
//扫描处理62616964757 a 686964616 Fe 58685 e 5a EB 93133361323566字符串
为wile (真)
{
ch=exp.charat(index;
索引;
//标识符(由字母开头、数字或字符组成) )。
if(character.isletter(ch ) )
{
while (character.is letter (ch|| isdigit ) ) ) ) ) ) ) ) ) ) ) ) 65
{
StrToken.append(ch;
ch=exp.charat(index;
索引;
}
索引--- -;
Stringstr=strToken.toString (;
if(str.equals('if ' ) )
optr.add(newword ) str,13 );
elseif(str.equals(else ) )
optr.add(newword ) str,14 );
elseif(str.equals('then ' ) )
optr.add(newword ) str,15 );
else
optr.add(newword ) str,26 );
}
//数字
ELSEif(Character.isdigit ) (ch ) )
{
while(Character.isdigit(ch ) ) ) ) ) ) ) ) ) )。
{
StrToken.append(ch;
ch=exp.charat(index;
索引;
}
索引--- -;
optr.add (new word (str token.tostring (),26 );
}
//加号或自加
ELSEif(ch==' ' )
{
ch=exp.charat(index;
索引;
if(ch==' ' )
optr.add(newword (',21 ) );
elseif(ch=='=' )
optr.add(newword('=',16 );
else
{
索引--- -;
optr.add(newword (',19 ) );
}
}
//加号或自加
elseif(ch=='-' )
{
ch=exp.charat(index;
索引;
if(ch=='-' )
optr.add(newword('-- ',21 );
elseif(ch=='=' )
optr.add(newword('-=',16 );
else
{
索引--- -;
optr.add(newword('-',19 );
}
}
//乘法或乘方
elseif(ch=='* ' )
{
ch=exp.charat(index;
索引;
if(ch=='* ' )
optr.add(newword('** ',20 );
elseif(ch=='=' )
optr.add(newword('*=',16 );
else
{
索引--- -;
optr.add(newword('* ',20 );
}
}
p>//除法或注释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
{
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("<
else
{
index--;
optr.add(new Word("
}
}
//赋值或等于
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
{
optr.add(new Word("#", 27));
break;
}
}
//清空扫描串
strToken.setLength(0);
}
return optr;
}
}