用java实现词法分析器_如何用JAVA编写词法分析器程序

展开全部

我也做过这个作业

package source;

import java.util.LinkedList;

public class LexicalAnalysis

{

//私有变量声明

private LinkedList optr = new LinkedList();

private String exp;

//词法分析

public LinkedList lexical_analysis(String exp)

{

char ch = '\0';          //当前文件指针内容

int index = 0;           //文件指针

StringBuffer strToken = new StringBuffer("");

//扫描处理62616964757a686964616fe58685e5aeb931333361323566字符串

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("<

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

{

System.out.println("非法符号\'#\'后的语句忽略!");

optr.add(new Word("#", 27));

break;

}

}

//清空扫描串

strToken.setLength(0);

}

return optr;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.根据状态转换图直接编程 编写一个词法分析程序,它从左到右逐个字符的对源程序进行扫描,产生一个个的单词的二元式,形成二元式(记号)流文件输出。在此,词法分析程序作为单独的一遍,如下图所示。 具体任务有: (1)组织源程序的输入 (2)识别单词的类别并记录类别编号和值,形成二元式输出,得到单词流文件 (3)删除注释、空格和无用符号 (4)发现并定位词法错误,需要输出错误的位置在源程序中的第几行。将错误信息输出到屏幕上。 (5)对于普通标识符和常量,分别建立标识符表和常量表(使用线性表存储),当遇到一个标识符或常量时,查找标识符表或常量表,若存在,则返回位置,否则返回0并且填写符号表或常量表。 标识符表结构:变量名,类型(整型、实型、字符型),分配的数据区地址 注:词法分析阶段只填写变量名,其它部分在语法分析、语义分析、代码生成等阶段逐步填入。 常量表结构:常量名,常量值 单词的构词规则: 字母=[A-Z a-z] 数字=[0-9] 标识符=(字母|_)(字母|数字|_)* 数字=数字(数字)*( .数字+|) 2.S语言表达式和语句说明 1.算术表达式:+、-、*、/、% 2.关系运算符:>、>=、<、<=、==、!= 3.赋值运算符:=,+=、-=、*=、/=、%= 4.变量说明:类型标识符 变量名表; 5.类型标识符:int char float 6.If语句:if 表达式then 语句 [else 语句] 7.For语句:for(表达式1;表达式2;表达式3) 语句 8.While语句:while 表达式 do 语句 9.S语言程序:由函数构成,函数不能嵌套定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值