词法分析器|编译原理|陈火旺
生活随笔
收集整理的這篇文章主要介紹了
词法分析器|编译原理|陈火旺
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*詞法分析器
* by qi
* */
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;public class javasc {public static void main(String[] args) {// TODO Auto-generated method stubanalysis ana=new analysis();ana.str="* * ** * 0 00 DIM IF STOP ENO where while else";ana.anStr();}}
class analysis{public char ch;//存放最新讀入的字符public String strToken="";//存放字符串public int op=-1;//搜索指示器public String str;//存放讀入的字符public Map<String,Integer> tain=new HashMap<>();//存放保留字public ArrayList<String> charTain=new ArrayList<>();//存放符號表public ArrayList<String> digitTain=new ArrayList<>();//存放常數表public analysis(){loadMap();//加載保留字}public void loadMap(){//存放保留字tain.put("DIM",1);tain.put("IF", 2);tain.put("DO",3);tain.put("STOP",4);tain.put("ENO",5);}public boolean GetChar(){//獲得下一個字符到ch中op++;if((op<=str.length()-1)){ch=str.charAt(op);/* if(ch<=0){return false;}*/return true;}return false;}public boolean GetBC(){//判斷ch是否是空格,如果是,則獲得下一個if(Character.isSpaceChar(ch)){GetChar();return true;}return false;}public void Concat(){//將ch連接到strToken之后strToken+=ch;}public boolean IsLetter(){//判斷是否為字母if(Character.isLetter(ch)){return true;}else{return false;}}public boolean IsDigit(){//判斷是否為數字if(Character.isDigit(ch)){return true;}else{return false;}}public int Reserve(){//從保留字表中查找strTokenInteger code=tain.get(strToken);if(code==null){return 0;}return code;}public void Retract(){//將搜索指示器回調一位op--;ch='\000';}public int InsertId(){//將strToken插入到標識符表中charTain.add(strToken);return charTain.indexOf(strToken);}public int InsertConst(){//將strToken插入到常量表中digitTain.add(strToken);return digitTain.indexOf(strToken);}public void an(){//分析主程序int code;int value;GetChar(); //獲得一個字符GetBC(); //判斷獲得的字符是否為空,如果為空重新獲得if(IsLetter()){ //判斷是否為字母while(IsLetter()||IsDigit()){boolean o=true;Concat();o=GetChar();if(o==false){break;}}Retract();code=Reserve();if (code==0){//如果不是保留字,則將標示符插入到標識符表中,并返回下標value=InsertId();System.out.println("標識符:$ID"+" "+value);}else{System.out.println("保留字:"+strToken+" "+code);}strToken="";//完成一次校驗,置空}else if(IsDigit()){while(IsDigit()){Concat();boolean o=GetChar();if(o==false){break;}}Retract();value=InsertConst();//插入到常量表中,返回下標System.out.println("$INT "+strToken+" "+value);}else if(ch=='='){System.out.println("="+"$ASSIGN");}else if(ch=='+'){System.out.println("+"+" "+"$PLUS");}else if(ch=='*'){boolean o=GetChar();if(ch=='*'&&o==true){System.out.println("**"+" "+"$POWER");}else {Retract();System.out.println("*" + " " + "$STAR");}}else if(ch==';'){System.out.println(";"+" "+"$SEMICOLON");}else if(ch=='('){System.out.println("("+" "+"$LPAR");}else if(ch==')'){System.out.println(")"+" "+"$RPAR");}else if(ch=='{'){System.out.println("{"+" "+"$LBRACE");}else if(ch=='}'){System.out.println("}"+" "+"$RBRACE");}else{System.out.println("error");System.out.println(strToken);}strToken="";}public void anStr(){while(op<str.length()-1){an();//System.out.println(op);}}
}
注意: 最后一個”前不能有空格?
總結
以上是生活随笔為你收集整理的词法分析器|编译原理|陈火旺的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows 重新启动 postgre
- 下一篇: R语言学习笔记