java词法分析代码_跪求,用java编写词法分析的原理及源代码

展开全部

//楼上傻

package a;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class fenxi extends JFrame implements ActionListener

{

private JTextArea ta1;

private JTextArea ta2;

private JButton jb=new JButton("词法分32313133353236313431303231363533e78988e69d8331333337613931析");

private JButton jb1=new JButton("清空文本区");

private JLabel jl1=new JLabel("输入源代码:");

private JLabel jl2=new JLabel("分析结果:");

//保留字

String gj[]={"else","if","int","return","void","while"};

//运算符

String fh[]={"+","-","*","/","",">=","==","!=","=",";",",","(",")","[","]","{","}"};

//初始化界面

public fenxi()

{

super("词法分析器");

this.setSize(600,448);

this.setLocation(200,150);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setLayout(null);

JPanel jp =new JPanel();

JPanel jp1 =new JPanel();

jp.setSize(300, 400);

jp1.setSize(300, 400);

jp.setLocation(0, 0);

jp1.setLocation(300, 0);

jp.setLayout(null);

jp1.setLayout(null);

ta1=new JTextArea(20,30);

ta2=new JTextArea(20,30);

ta2.setEditable(false);

JScrollPane scrol1=new JScrollPane(ta1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

JScrollPane scrol2=new JScrollPane(ta2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scrol1.setSize(260, 310);

scrol1.setLocation(18, 40);

scrol2.setSize(260, 310);

scrol2.setLocation(18, 40);

jp.add(scrol1);

jp1.add(scrol2);

jb.setBounds(80, 370, 100, 25);

jb1.setBounds(80, 370, 100, 25);

jl1.setFont(new Font("楷书",Font.BOLD,12));

jl1.setSize(140, 25);

jl1.setLocation(0, 0);

jl2.setFont(new Font("楷书",Font.BOLD,12));

jl2.setSize(140, 25);

jl2.setLocation(0, 0);

jp.add(jb1);

jp.add(jl1);

jp1.add(jb);

jp1.add(jl2);

this.add(jp);

this.add(jp1);

jb.addActionListener(this);

jb1.addActionListener(this);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

//点击了“清空文本”区按钮

if(e.getSource()==jb1)

{

int a=JOptionPane.showConfirmDialog(null, "确定清空吗?","提示!",JOptionPane.YES_NO_OPTION);

//执行清空操作

if( a==JOptionPane.YES_OPTION)

{

ta1.setText("");

ta2.setText("");

}

}

//点击了“词法分析”分析按钮

if(e.getSource()==jb)

{

int m=0;//控制数组b的下标

int n=0;

float f;

String str[]=new String[20];

int c[]=new int[20];

for(int i=0;i<20;i++)

{

c[i]=i;

}

String str1 = new String();

//把你输入的软代码赋值给字符串变量a

String a=ta1.getText();

//把a中的字符一个一个放入字符数组b中

char[] b=new char[a.length()];

for(int i=0;i

b[i]=a.charAt(i);

while(m

{

//已过时。由 isWhitespace(char) 取代。确定指定字符依据 Java 标准是否为空白字符。

if(Character.isSpace(b[m]))

{

m++;

}

//确定指定字符是否为数字

else if(Character.isDigit(b[m]))

{

boolean flag=false;

int mn=0;

int xy=0;

int ab=0;

while(m'&& b[m]!='

{

str1=str1+str1.valueOf(b[m]);

m++;

}

//判断是否是一个数字

for(int i=0;i

{

if(str1.charAt(i)!='.'&& !Character.isDigit(str1.charAt(i)))

mn=1;

}

//mn为0则为数字(浮点或整数 分别翻译成十六进制 和 二进制)

if(mn==0)

{

for(int k=0;k

{

if(str1.charAt(k)=='.')

flag=true;

}

if(flag)

{

f=Float.valueOf(str1);

ta2.setText(ta2.getText()+str1+"\t"+Float.toHexString(f)+"\n");

}

else

{

int x =Integer.valueOf(str1);

ta2.setText(ta2.getText()+x+"\t"+Integer.toBinaryString(x)+"\n");

}

}

//科学计算法 或是 非法字符

else

{

for(int i=0;i

if(Character.isLetter(str1.charAt(i))||str1.charAt(i)=='-')

{

xy++;

ab=i;

}

if(xy==1 && str1.charAt(ab)=='e')

ta2.setText(ta2.getText()+str1+"\t"+"e"+str1+"\n");

else if(xy==2 && str1.charAt(ab)=='-' && str1.charAt(ab-1)=='e')

{

ta2.setText(ta2.getText()+str1+"\t"+"-e"+str1+"\n");

}

else

ta2.setText(ta2.getText()+str1+"\t"+"非法标志符"+"\n");

}

str1="";

}

//确定指定字符是否为字母。

else if(Character.isLetter(b[m]))

{

//不是结束符则继续(改正了定义数组时字符后面跟“[”)

while(m'&& b[m]!='

{

str1=str1+str1.valueOf(b[m]);

m++;

}

//gj是保留字

if(str1.equals(gj[0]))

ta2.setText(ta2.getText()+str1+"\t"+"41"+"\n");

else if(str1.equals(gj[1]))

ta2.setText(ta2.getText()+str1+"\t"+"42"+"\n");

else if(str1.equals(gj[2]))

ta2.setText(ta2.getText()+str1+"\t"+"43"+"\n");

else if(str1.equals(gj[3]))

ta2.setText(ta2.getText()+str1+"\t"+"44"+"\n");

else if(str1.equals(gj[4]))

ta2.setText(ta2.getText()+str1+"\t"+"45"+"\n");

else if(str1.equals(gj[5]))

ta2.setText(ta2.getText()+str1+"\t"+"46"+"\n");

//

else

{

int p=1;

for(int i=0;i

{

if(!Character.isLetter(str1.charAt(i))&&!Character.isDigit(str1.charAt(i))&&str1.charAt(i)!='_')

p=0;//字符串不是字符或数字的话 就是非法字符(更正用下划线定义变量 但不能识别的错误)

}

if(p==1)

{

for(int j=0;n>0 && j

{

if( str1.equals(str[j]))

{

ta2.setText(ta2.getText()+str1+"\t"+c[j]+"\n");

str1="";

}

}

if(str1!="")

{

str[n]=str1;

ta2.setText(ta2.getText()+str1+"\t"+c[n]+"\n");

n++;

}

str1="";

}

else

{

ta2.setText(ta2.getText()+str1+"\t"+"非法标志符"+"\n");

}

}

str1="";

}

//

else if(b[m]=='+')

{

m++;

if(m

{

while((m

{

str1=str1+str1.valueOf(b[m]);

m++;

}

ta2.setText(ta2.getText()+"+"+str1+"\t"+"00"+str1+"\n");

}

else

{

if(m

{

ta2.setText(ta2.getText()+"++"+"\t"+"47"+"\n");

m++;

}

else

{

m--;

ta2.setText(ta2.getText()+b[m]+"\t"+"21"+"\n");

m++;

}

}

str1="";

}

//

else if(b[m]=='-')

{

m++;

if(m

{

while((m

{

str1=str1+str1.valueOf(b[m]);

m++;

}

ta2.setText(ta2.getText()+"-"+str1+"\t"+"11"+str1+"\n");

}

else

{

if(m

{

ta2.setText(ta2.getText()+"--"+"\t"+"48"+"\n");

m++;

}

else

{

m--;

ta2.setText(ta2.getText()+b[m]+"\t"+"22"+"\n");

m++;

}

}

str1="";

}

//

else if(b[m]=='*')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"23"+"\n");

m++;

}

//

else if(b[m]=='

{

str1=str1+str1.valueOf(b[m]);

m++;

if(b[m]=='=')

{

str1=str1+str1.valueOf(b[m]);

ta2.setText(ta2.getText()+str1+"\t"+"25"+"\n");

m++;

}

else

ta2.setText(ta2.getText()+str1+"\t"+"24"+"\n");

str1="";

}

//

else if(b[m]=='>')

{

str1=str1+str1.valueOf(b[m]);

m++;

if(b[m]=='=')

{str1=str1+str1.valueOf(b[m]);

ta2.setText(ta2.getText()+str1+"\t"+"27"+"\n");

m++;

}

else ta2.setText(ta2.getText()+str1+"\t"+"26"+"\n");

str1="";

}

//

else if(b[m]=='=')

{

str1=str1+str1.valueOf(b[m]);

m++;

if(b[m]=='=')

{str1=str1+str1.valueOf(b[m]);

ta2.setText(ta2.getText()+str1+"\t"+"29"+"\n");

m++;

}

else ta2.setText(ta2.getText()+str1+"\t"+"28"+"\n");

str1="";

}

//30,"=" 31,"!="

else if(b[m]=='!')

{

str1=str1+str1.valueOf(b[m]);

m++;

if(b[m]=='=')

{

str1=str1+str1.valueOf(b[m]);

ta2.setText(ta2.getText()+str1+"\t"+"31"+"\n");

m++;

}

else ta2.setText(ta2.getText()+str1+"\t"+"30"+"\n");

str1="";

}

//32

else if(b[m]==';')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"32"+"\n");

m++;

}

//33

else if(b[m]==',')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"33"+"\n");

m++;

}

//34

else if(b[m]=='(')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"34"+"\n");

m++;

}

//35

else if(b[m]==')')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"35"+"\n");

m++;

}

//36

else if(b[m]=='{')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"36"+"\n");

m++;

}

//37

else if(b[m]=='}')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"37"+"\n");

m++;

}

//38

else if(b[m]=='[')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"38"+"\n");

m++;

}

//39

else if(b[m]==']')

{

ta2.setText(ta2.getText()+b[m]+"\t"+"39"+"\n");

m++;

}

//40

else if(b[m]=='/')

{

m++;

if(m

{

m++;

inner:

for(;true;)

{

label:

for(;b[m]!='*';)

{

m++;

for(;b[m]=='*';)

{

m++;

for(;b[m]!='/';)

{

continue label;

}

if(b[m]=='/')

m++;

break inner;

}

}

}

}

else

{

m--;

ta2.setText(ta2.getText()+b[m++]+"\t"+"40"+"\n");

}

}

//

else

{

ta2.setText(ta2.getText()+b[m]+"\t"+"非法字符"+"\n");

m++;

}

//全部检查完毕,一while循环

}

}

}

public static void main(String args[])

{

new fenxi();

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值