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 "";
}
}
}
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 "";
}
}
}
相关文章推荐
- Micro语言的词法分析程序 Java代码
- 桌面端数据分析程序语言——润乾
- 小C语言--词法分析程序
- 一个小语言的词法分析程序原理及其实现(2)
- GEP程序(C#语言描述)分析(一)
- 【程序语言】C++ 构造函数 拷贝构造函数 =操作符重载 析构函数 详细分析
- 小C语言--词法分析程序
- 小C语言--词法分析程序
- 小C语言--词法分析程序
- C程序语言strstr函数分析和使用
- 通过汇编语言分析程序执行
- PL/0语言编译程序分析
- GEP程序(C#语言描述)分析(二)
- 3666 sdut 小C语言--词法分析程序
- 80x86汇编语言程序错误分析
- PL/0语言编译程序分析
- 汇编语言编写方法及程序分析
- 汇编语言---王爽--实验8-分析一个奇怪的程序
- GEP程序(C#语言描述)分析(一)
- 汇编语言32位控制台读取用户输入字符程序代码分析(17)