まず、次のように分析するコードセグメントを見てみましょう。
出力の結果は次のとおりです。
出力結果(a).png
出力結果(b).png
出力結果(c).png
ブラケットにはバイナリ式があります:(単語カテゴリエンコーディング、単語の位置番号)
コードは次のとおりです。
パッケージyue.lexicalanalyzer; Import java.io。*;/** Main Program*/public class main {public static void main(string [] args)throws ioexception {lexer lexer = new lexer(); lexer.printtoken(); lexer.printsymbolstable(); }}パッケージyue.lexicalanalyzer; Import java.io。*; import java.util。*;/**lexical analysion and output*/public class lexer {/*録音ライン番号*/public static int line = 1; /*最新の読み取り文字を保存*/ char文字= ''; /*予約単語*/ hashtable <string、keyword> keywords = new hashtable <string、keyword>(); /*トークンシーケンス*/ private arraylist <token> tokens = new ArrayList <Token>(); /*シンボルテーブル*/ private arrayList <symbol> symtable = new ArrayList <Symbol>(); /*ファイル変数の読み取り*/ bufferedreader reader = null; /*ファイルの最後が現在読み取られているかどうかを保存*/ private boolean isend = false; /*ファイルの終了が読み取られているかどうか*/ public boolean getReaderstate(){return this.isend; }/*tokens Sequence*/public void printtoken()throws ioexception {filewriter writer = new filewriter( "e://lex.txt"); system.out.println( "語彙分析結果は次のとおりです。"); System.out.print( "du yue-2015220201031/r/n/n"); writer.write( "du yue-2015220202031/r/n/r/n"); while(getReaderstate()== false){token tok = scan(); string str = "line" + tok.line + "/t(" + tok.tag + "、" + tok.pos + ")/t/t" + tok.name + ":" + tok.tostring() + "/r/n"; writer.write(str); System.out.print(str); } writer.flush(); }/*印刷記号テーブル*/public void printsymbolstable()throws ioexception {filewriter writer = new filewriter( "e://symtab1.txt"); System.out.print( "/r/n/r/nシンボルテーブル/r/n"); System.out.print( "number/t line number/t name/r/n"); writer.write( "シンボルテーブル/r/n"); writer.write( "number" + "/t line number" + "/t name/r/n"); iterator <symbol> e = symtable.iterator(); while(e.hasnext()){シンボルシンボル= e.next(); string desc = symbol.pos + "/t" + symbol.line + "/t" + symbol.toString(); System.out.print(desc + "/r/n"); writer.write(desc + "/r/n"); } writer.flush(); }/*print error*/public void printerror(token tok)throws ioexception {filewriter writer = new filewriter( "e://error.txt"); System.out.print( "/r/n/r/nエラー辞書編成は次のとおりです。/r/n"); writer.write( "エラーレキシグラフィは次のとおりです: /r /n"); string str = "line" + tok.line + "/t(" + tok.tag + "、" + tok.pos + ")/t/t" + tok.name + ":" + tok.tostring() + "/r/n"; writer.write(str); } /*予約済みの単語の追加* / voidリザーブ(キーワードw){keywords.put(w.lexme、w); } public lexer(){/*読み取りファイル変数を初期化*/try {reader = new fileReader( "e://input.txt")); } catch(ioException e){system.out.print(e); } /*予約済みの単語を追加* / this.reserve(keyword.begin); this.reserve(keyword.end); this.reserve(keyword.integer); this.reserve(keyword.function); this.reserve(keyword.read); this.reserve(keyword.write); this.reserve(keyword.aif); this.reserve(keyword.athen); this.reserve(keyword.aelse); } /*文字で読み取る* / public void readch()throws ioexception {charality =(char)reader.read(); if((int)character == 0xffff){this.isend = true; }} /*それが一致するかどうかを判断* / public boolean readch(char ch)throws ioexception {readch(); if(this.character!= ch){return false; } this.character = ''; trueを返します。 } /*数字の認識* / public boolean isdigit()throws ioexception {if(character.isdigit(character)){int value = 0; while(character.isdigit(character)){value = 10 * value + chacel.digit(character、10); readch(); } num n = new num(value); n.line = line; tokens.add(n); trueを返します。 } else falseを返します。 } /*予約された単語と識別子の認識* / public boolean isletter()throws ioexception {if(character.isletter(character)){stringbuffer sb = new StringBuffer(); /*最初に分割全体を取得します*/ while(character.isletterordigit(character)){sb.append(character); readch(); } /*それが予約済みの単語であるか識別子であるかを判断* /文字列s = sb.toString();キーワードw = keywords.get(s); /*予約済みの単語の場合、wは空にしないでください*/ if(w!= null){w.line = line; tokens.add(w); } else { /*それは識別子です。ここには、識別子番号を記録する追加のステートメントがあります* / symbol sy = new Symbol(s);シンボルマーク= sy; //既存の識別子boolean isrepeat = falseをマークするために使用されます。 sy.line = line; for(シンボルi:symtable){if(sy.tosttring()。equals(i.tostring())){mark = i; isrepeat = true; }} if(!isrepeat){sy.pos = symtable.size() + 1; symtable.add(sy); } else if(isrepeat){sy.pos = mark.pos; } tokens.add(sy); } trueを返します。 } else falseを返します。 } /*シンボル認識* / public boolean issign()throws ioexception {switch(character){case '#':readch(); Allend.allend.line = line; tokens.add(allend.allend); trueを返します。 case '/r':if(readch( '/n')){readch(); lineend.lined.line = line; tokens.add(lineend.lineend); line ++; trueを返します。 } case '(':readch(); delimiter.lpar.line = line; tokens.add(delimiter.lpar); return true; case ')':readch(); delimiter.rpar.line = line; tokens.add(delimiter.rpar); trueを返します。 case ';':readch(); delimiter.sem.line = line; tokens.add(delimiter.sem); trueを返します。 case '+':readch(); calcword.add.line = line; tokens.add(calcword.add); trueを返します。ケース ' - ':readch(); calcword.sub.line = line; tokens.add(calcword.sub); trueを返します。 case '*':readch(); calcword.mul.line = line; tokens.add(calcword.mul); trueを返します。 case '/':readch(); calcword.div.line = line; tokens.add(calcword.div); trueを返します。 case ':':if(readch( '=')){readch(); calcword.assign.line = line; tokens.add(calcword.Assign); trueを返します。 } 壊す; case '>':if(readch( '=')){readch(); calcword.ge.line = line; tokens.add(calcword.ge); trueを返します。 } 壊す; case '<':if(readch( '=')){readch(); calcword.le.line = line; tokens.add(calcword.le); trueを返します。 } 壊す; case '!':if(readch( '=')){readch(); calcword.ne.line = line; tokens.add(calcword.ne); trueを返します。 } 壊す; } falseを返します。 } /*以下は、キーワード、識別子、その他の情報を分割するために開始します* / public token scan()throws ioException {token tok; while(character == '')readch(); if(isdigit()|| sissign()|| isletter()){tok = tokens.get(tokens.size() - 1); } else {tok = new Token(Character); Printerror(Tok); } tokを返します。 }}パッケージyue.lexicalanalyzer;/ * * token parent class */public class token {public final int tag; public int line = 1; public string name = ""; public int pos = 0; public token(int t){this.tag = t; } public string toString(){return "" +(char)タグ; }}パッケージyue.lexicalanalyzer;/**単語カテゴリの割り当て*/public classタグ{public final static int begin = 1、//予約済みの単語integer = 3、//予約済みの単語関数= 4、//予約単語read = 5、//予約済みの単語word write = 6、// reserved単語= 7、 11、識別子定数= 12、//定数add = 13、// operator "+" sub = 14、// operator " - " mul = 15、// operator "*" div = 16、/"le = 18、// operator" <= "ge = 19、// operator"> = "ne = 20、//オペレーター" // operator "(" rpar = 25、// operator ")" sem = 26、// operator ";" line_end = 27、// operator all_end = 28; //オペレーター "#"}パッケージyue.lexicalanalyzer;/***予約済みの単語*/public classキーワード拡張トークン{public string lexme = ""; publicキーワード(string s、int t){super(t); this.lexme = s; this.name = "予約単語"; } public string toString(){return this.lexme; } public static final keyword begin = new keyword( "begin"、tag.begin)、end = new keyword( "end"、tag.end)、integer = new keyword( "integer"、tag.integer)、function = new keyword( "function"、tag.function)、read = new keyword( "read"、tag.read)、write = new keyd "キーワード( "if"、tag.if)、athen = new keyword( "then"、tag.then)、aelse = new keyword( "else"、tag.else);}パッケージyue.lexicalanalyzer;/**識別子*/public classシンボルextends token {public string lexme = "";パブリックシンボル(string s){super(tag.symbol); this.lexme = s; this.name = "識別子"; } public string toString(){return this.lexme; }}パッケージyue.lexicalanalyzer;/*** operator*/public class calcword extends token {public string lexme = ""; public calcword(string s、int t){super(t); this.lexme = s; this.name = "operator"; } public string toString(){return this.lexme; } public static final calcword add = new calcword( "+"、tag.add)、sub = new calcword( " - "、tag.sub)、mul = new calcword( "*"、tag.mul)、new calcword( "/"、tag.div)、le = new calcword( "<="、tag.le)、ge = new calcword( " tag.ne)、assign = new calcword( ":="、tag.assign);}パッケージyue.lexicalanalyzer;/***境界シンボル*/public class delimiter extends token {public string lexme = ""; public delimiter(string s、int t){super(t); this.lexme = s; this.name = "Bounding Symbol"; } public string toString(){return this.lexme; } public static final delimiter lpar = new delimiter( "("、tag.lpar)、rpar = new delimiter( ")"、tag.rpar)、sem = new delimiter( ";"、tag.sem);}パッケージyue.lexicalanalyzer;/** constant*/public class num extends token {public final int value; public num(int v){super(tag.constant); this.value = v; this.name = "constant"; } public string toString(){return "" + value; }}パッケージyue.lexicalanalyzer;/***行の文字の終わり*/public class lineend extends token {public string lexme = ""; public lineend(string s){super(tag.line_end); this.lexme = s; this.name = "end of line文字"; } public string toString(){return this.lexme; } public static final lineEnd = new LineEnd( "/r/n");}パッケージyue.lexicalanalyzer;/***終了文字*/パブリッククラスAllend extends token {public string lexme = ""; public Allend(string s){super(tag.all_end); this.lexme = s; this.name = "end文字"; } public string toString(){return this.lexme; } public static final allend allend = new Allend( "#");}要約します
この記事のコンテンツ全体で寝ます。この記事の内容があなたの研究や仕事に役立つことを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。