[java]实现Brainfuc解析器

来源:互联网 发布:淘宝购物评级怎么提升 编辑:程序博客网 时间:2024/06/01 07:54
>什么是Brainfuck语言?

Brainfuck是一种极小化的计算机语言,它是由Urban Müller在1993年创建的.

Müller的目标是建立一种简单的、可以用最小的编译器来实现的、符合图灵完全思想的编程语言.

这种语言由八种状态构成,为Amiga机器编写的编译器(第二版)只有240个字节大小!就象它的名字所暗示的,brainfuck程序很难读懂.尽管如此,brainfuck图灵机一样可以完成任何计算任务.虽然brainfuck的计算方式如此与众不同,但它确实能够正确运行

wiki:https://en.wikipedia.org/wiki/Brainfuck


其只有八种状态:


他的代码看起来长得就像这样:



是不是非常有意思?



>解析器源码如下

import java.util.HashMap;import java.util.Stack;  public class BF_Prj {private static HashMap<Integer,Character> cs=new HashMap<Integer,Character>();//Code Segmentprivate static HashMap<Integer,Character> ds=new HashMap<Integer,Character>();//Data Segmentprivate static HashMap<Integer,Integer> sslr=new HashMap<Integer,Integer>();//Stack Segment i -> ]private static HashMap<Integer,Integer> ssrl=new HashMap<Integer,Integer>();//Stack Segment i -> [private static Stack<Integer> ssStack=new Stack<Integer>();//Stack Segment []private static Integer bp=0,readInbp=0,csbp=0;//Base Point | args[1] readIn Point | Code Segment Pointprivate static String codeSeg,readIn;//仅支持ASCII字符public static void  main(String[] args){codeSeg="+++++++[>++++++++++<-]>++.>++++++++++[>++++++++++<-]>+++++.";if(args.length==1){codeSeg=args[0];readInbp=-1;}else if(args.length==2){codeSeg=args[1];}else {System.out.println("【解析器】\n当前命令参数行数 : " + args.length+"\n运行结果:\n");}try{load();run();}catch (Exception e) {e.printStackTrace();}}//读入指令private static void load() throws BFException{int whileNum=0,left=0;for(Integer i=0;i<codeSeg.length();i++){char c=codeSeg.charAt(i);cs.put(i,c);if(c=='['){ssStack.push(i);whileNum++;}else if(c==']'){whileNum--;try{left=ssStack.pop();}catch (Exception e) {throw new BFException("BF异常 :括号匹配错误 : whileNum = "+whileNum);}sslr.put(left,i);ssrl.put(i,left);}}if(whileNum!=0)throw new BFException("BF异常 :括号匹配错误 : whileNum = "+whileNum);}//运行并解析private static void run(){while(csbp<cs.size()){try{//System.out.print(cs.get(csbp));vm(cs.get(csbp));}catch (Exception e) {e.printStackTrace();return ;}}}private static void vm(Character c)throws Exception{switch (c) {case '+':plus();csbp++;break;case '-':des();csbp++;break;case '.':output();csbp++;break;case ',':read();csbp++;break;case '>':right();csbp++;break;case '<':left();csbp++;break;case '[':whileLeft();break;case ']':whileRight();break;default:throw new BFException("BF异常 :输入错误 : 未知字符 = '"+cs.get(csbp)+"' at "+csbp+".");//break;}}//数据+1private static void plus(){if(ds.containsKey(bp)){int c=ds.get(bp)+1;if(c==256){ds.put(bp,(char)0);}else{ds.put(bp,(char)c);}}else{ds.put(bp,(char)1);}}//数据-1private static void des(){if(ds.containsKey(bp)){int c=ds.get(bp)-1;if(c==-1){ds.put(bp,(char)255);}else{ds.put(bp,(char)c);}}else{ds.put(bp,(char)255);}}//指针右移private static void right(){bp++;}//指针左移private static void left() throws BFException{bp--;if(bp==-1){throw new BFException("BF异常 :指针越界 : bp = -1"+".");}}//输出private static void output() throws BFException{if(ds.containsKey(bp)){System.out.print(ds.get(bp));}else{throw new BFException("BF异常 :指针越界 : 数据不存在  : bp = "+bp+".");}}//读入private static void read() throws BFException{if(readInbp==-1){throw new BFException("BF异常 :数据不存在越界 : 请先输入数据.");}else{try{ds.put(bp,readIn.charAt(readInbp++));}catch (Exception e) {throw new BFException("BF异常 :指针越界 : 输入数据长度不足 : readInbp = "+ readInbp+".");}}}//循环右括号private static void whileRight() throws BFException{if(!ds.containsKey(bp)){throw new BFException("BF异常 :指针越界 : 循环检测空值: bp = "+ bp+".");}else{if(ds.get(bp)!=0){csbp=ssrl.get(csbp);return ;}csbp++;}}//循环左括号private static void whileLeft() throws BFException{if(!ds.containsKey(bp)){throw new BFException("BF异常 :指针越界 : 循环检测空值: bp = "+ bp+".");}else{if(ds.get(bp)==0){csbp=sslr.get(csbp);return ;}csbp++;}}//自定义异常类@SuppressWarnings("serial")static class BFException extends Exception{public BFException(String msg){          super(msg);      } }//Create by http://blog.csdn.net/shenpibaipao//2017.6.25}



原创粉丝点击