您的位置:首页 > 编程语言

Hao语言--词法分析程序

2014-05-09 16:03 405 查看
 新型汉语编程语言 设计开发中............源代码公开,希望有能力的朋友多提意见

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 "";
}
}

}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息