Hao语言--词法分析程序

 新型汉语编程语言  设计开发中............源代码公开,希望有能力的朋友多提意见
package com.hao.haocompiler.morphology;

public class Context {
protected static WordState spaceState;
protected static WordState signState;
protected static WordState numberState;
protected static WordState identifyingState;
protected static WordState floatState;

private WordState wordState;
public Context() {
// TODO Auto-generated constructor stub
spaceState=new SpaceState(this);
signState=new SignState(this);
numberState=new NumberState(this);
floatState=new FloatState(this);
identifyingState=new IdentifyingState(this);
initContext();
}
public void initContext() {
wordState=spaceState;
}
public String getOneWordByLine(String line, int index) {

return wordState.getOneWordByLine(line, index);
}

public WordState getWordState() {
return wordState;
}

public void setWordState(WordState wordState) {
this.wordState = wordState;
}
}
 package com.hao.haocompiler.morphology;

import com.hao.haocompiler.utill.FileUtill;

public class FileContext {
private FileUtill fileUtill;
private Context context;
private String currLine="";
private int index;
private String word;
public FileContext(String path) {
// TODO Auto-generated constructor stub
fileUtill=new FileUtill(path);
context=new Context();
if(fileUtill.isHaveNextLine()){
   currLine=fileUtill.getOneLineBySourceFile();
}
}
public boolean nextWord(){
        context.initContext();
    if(index>=currLine.length()){
     if(fileUtill.isHaveNextLine()){
     currLine=fileUtill.getOneLineBySourceFile();
     index=0;
     }
      }
    try {
word=context.getOneWordByLine(currLine, index);
index+=word.length();
return true;
} catch (Exception e) {
return false;
}
}
public String getOneWordByLine() {
return word;
}

}
package com.hao.haocompiler.morphology;

public class FloatState extends NumberState{

public FloatState(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public String getOneWordByLine(String line, int index) {
return super.getOneWordByLine(line, index);
}

}

package com.hao.haocompiler.morphology;

public class IdentifyingState extends WordState{

public IdentifyingState(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public String getOneWordByLine(String line, int index) {
try {
char currChar=line.charAt(index);
if(currChar>='a'&&currChar<='z'||currChar>='A'&&currChar<='Z'||
currChar=='_'||currChar>='0'&&currChar<='9'){
return currChar+getOneWordByLine(line, ++index);
}
} catch (Exception e) {
}
return "";
}

}
package com.hao.haocompiler.morphology;

public class NumberState extends WordState{

public NumberState(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public String getOneWordByLine(String line, int index) {
// TODO Auto-generated method stub
char currChar=line.charAt(index);
if(currChar>='0'&&currChar<='9'){
return currChar+getOneWordByLine(line, ++index);
}else if(currChar=='.'){
super.context.setWordState(Context.numberState);
return "."+super.context.getOneWordByLine(line, ++index);
}
return "";
}

}
package com.hao.haocompiler.morphology;

public class SignState extends WordState{

public SignState(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public String getOneWordByLine(String line, int index) {
try {
char currChar=line.charAt(index);
if(currChar=='+'||currChar=='-'||currChar=='*'||
currChar=='('||currChar==')'||currChar=='{'||currChar=='}'||
currChar=='['||currChar==']'||currChar=='?'||currChar==':'){
index++;
}else if(currChar=='>'||currChar=='<'||currChar=='='||currChar=='!'){
index++;
char nextChar=line.charAt(index);
if(nextChar=='='){
index++;
return currChar+""+nextChar+"";
}
}else if(currChar=='/'){
index++;
char nextChar=line.charAt(index);
if(nextChar=='/'){
index=line.length();
return "";
}
}
return currChar+"";
} catch (Exception e) {
return "";
}
}
}
package com.hao.haocompiler.morphology;

public class SpaceState extends WordState{

public SpaceState(Context context) {
super(context);
}

@Override
public String getOneWordByLine(String line, int index) {
char currChar=line.charAt(index);

if(currChar==' '){
return currChar+"";
}else if(currChar>='0'&&currChar<='9'){
return currChar+super.execWordStateGetOneWordByLine(Context.numberState,line, ++index);
}else if(currChar>='a'&&currChar<='z'||currChar>='A'&&currChar<='Z'||
currChar=='_'){
return currChar+super.execWordStateGetOneWordByLine(Context.identifyingState,line, ++index);
}else{
return super.execWordStateGetOneWordByLine(Context.signState, line, index);
}
}
}
package com.hao.haocompiler.morphology;

public abstract class WordState {
protected Context context;
public WordState(Context context) {
this.context = context;
}
public abstract String getOneWordByLine(String line,int index);
     

protected String execWordStateGetOneWordByLine(WordState wordState,String line, int index) {
context.setWordState(wordState);
return context.getOneWordByLine(line, index);
}
protected String getElseOneWordByLine(String line, int index){
try {
char currChar=line.charAt(index);
if(currChar==' '){
return execWordStateGetOneWordByLine(Context.spaceState, line, index);
}else{
return execWordStateGetOneWordByLine(Context.signState, line, index);
}
} catch (Exception e) {
return "";
}
}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值