أولاً ، دعونا نلقي نظرة على قطاع الكود الذي نريد تحليله على النحو التالي:
نتيجة الإخراج على النحو التالي:
نتيجة الإخراج (أ) .png
نتيجة الإخراج (ب) .PNG
نتيجة الإخراج (ج) .PNG
هناك تعبير ثنائي بين قوسين: (ترميز فئة الكلمات ، رقم موضع الكلمة)
الرمز كما يلي:
package yue.lexicalanalyzer ؛ import java.io.* ؛/** البرنامج الرئيسي*/الفئة العامة الرئيسية {public static void main (string [] args) رمي ioException {lexer lexer = new lexer () ؛ lexer.printtoken () ؛ lexer.printsymbolstable () ؛ }} package yue.lexicalanalyzer ؛ import java.io.*؛ import java.util.*؛/**analysis and outpray*/public class kexer {/*record number*/public static int line = 1 ؛ /*تخزين أحدث أحرف قراءة*/ char charach = '' ؛ /*كلمات الاحتياط*/ hashtable <string ، الكلمة الرئيسية> الكلمات الرئيسية = new hashtable <string ، keyword> () ؛ /*sequence token*/ private ArrayList <loken> tokens = new ArrayList <loken> () ؛ /*جدول الرمز*/ arraylist الخاص <symbor> symtable = جديد arraylist <mashy> () ؛ /*قراءة متغير الملف*/ BufferedReader Reader = null ؛ /*حفظ ما إذا كانت نهاية الملف تقرأ حاليًا*/ private boolean isend = false ؛ /*ما إذا كانت نهاية الملف يتم قراءة*/ public boolean getReaderState () {return this.isend ؛ }/*Print Tokens Sequence*/public void printtoken () يلقي ioException {filewRiter writer = new filewRiter ("e: //lex.txt") ؛ System.out.println ("نتائج التحليل المعجمي هي كما يلي:") ؛ system.out.print ("du Yue-20152202031/r/n/n") ؛ Writer.Write ("du Yue-20152202031/r/n/r/n") ؛ بينما (getReaderState () == false) {token tok = scan () ؛ String str = "line" + tok.line + "/t (" + tok.tag + "،" + tok.pos + ")/t/t" + tok.name + ":" + tok.toString () + "/r/n" ؛ الكاتب. write (str) ؛ system.out.print (str) ؛ } constr.flush () ؛ }/*طباعة جدول رمز*/public void printsymbolstable () يلقي ioException {filewRiter writer = new filewRiter ("e: //symtab1.txt") ؛ system.out.print ("/r/n/r/n symbol table/r/n") ؛ System.out.print ("رقم السطر/T/اسم T/R/N") ؛ Writer.write ("جدول الرمز/r/n") ؛ Writer.write ("Number" + "/T Number" + "/t name/r/n") ؛ iterator <mashy> e = symtable.iterator () ؛ بينما (e.hasnext ()) {symbol = e.next () ؛ String desc = symbor.pos + "/t" + symbor.line + "/t" + symbor.toString () ؛ system.out.print (desc + "/r/n") ؛ Writer.write (desc + "/r/n") ؛ } constr.flush () ؛ }/*خطأ طباعة*/printerror public void (Token Tok) يلقي ioException {filewRiter Writer = new filewRiter ("e: //error.txt") ؛ System.out.print ("/r/n/r/n error Lerratography كما يلي:/r/n") ؛ Writer.write ("خطأ معجم هو كما يلي: /r /n") ؛ String str = "line" + tok.line + "/t (" + tok.tag + "،" + tok.pos + ")/t/t" + tok.name + ":" + tok.toString () + "/r/n" ؛ الكاتب. write (str) ؛ } /*إضافة كلمات محفوظة* / void Reserve (الكلمة الرئيسية W) {Keywords.put (W.lexme ، W) ؛ } public lexer () {/*تهيئة متغير ملف القراءة*/try {reader = new BufferEdReader (new fileReader ("e: //input.txt")) ؛ } catch (ioException e) {system.out.print (e) ؛ } /*إضافة كلمات محفوظة* / this.reserve (الكلمة الرئيسية. 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 () rems IoException {character = (char) reader.read () ؛ if ((int) حرف == 0xffff) {this.isend = true ؛ }} /*ادعى ما إذا كان يتطابق مع* / public boolean readch (char ch) يلقي ioException {readch () ؛ if (this.character! = ch) {return false ؛ } this.character = '' ؛ العودة صحيح. } /*التعرف على الأرقام* / public boolean iSdigit () يلقي ioException {if (character.isdigit (حرف)) {int value = 0 ؛ بينما (character.isdigit (حرف)) {value = 10 * value + forme.digit (الحرف ، 10) ؛ readch () ؛ } num n = new num (value) ؛ n.line = خط ؛ Tokens.Add (n) ؛ العودة صحيح. } مرة أخرى عودة خطأ ؛ } /*التعرف على الكلمات والمعرفات المحفوظة* / iSletter () Public Boolean Isletter () IoException {if (character.isletter (character)) {StringBuffer sb = new StringBuffer () ؛ /*احصل أولاً على الانقسام الكامل*/ بينما (character.isletterordigit (حرف)) {sb.append (حرف) ؛ readch () ؛ } /*ادعى ما إذا كانت كلمة محفوظة أو معرف* / string s = sb.toString () ؛ الكلمة الرئيسية w = الكلمات الرئيسية. get (s) ؛ /*إذا كانت كلمة محفوظة ، يجب ألا تكون W فارغة*/ if (w! = null) {w.line = line ؛ Tokens.Add (W) ؛ } آخر { /* وإلا فهو معرف ، وهنا هناك عبارات إضافية تسجل رقم المعرف* / symbol sy = رمز (s) الجديد ؛ علامة الرمز = SY ؛ // المستخدمة لتمييز المعرف الحالي المنطقي isrepeat = false ؛ sy.line = خط ؛ لـ (الرمز i: symtable) {if (sy.toString (). equals (i.toString ())) {mark = i ؛ isrepeat = صحيح ؛ }} if (! isRepeat) {sy.pos = symtable.size () + 1 ؛ symtable.add (sy) ؛ } آخر إذا (isRepeat) {sy.pos = mark.pos ؛ } Tokens.Add (sy) ؛ } إعادة صواب ؛ } مرة أخرى عودة خطأ ؛ } /*التعرف على الرمز* / public boolean issign () يلقي ioException {switch (حرف) {case '#': readch () ؛ Allend.allend.line = line ؛ Tokens.Add (Allend.allend) ؛ العودة صحيح. الحالة '/r': if (readch ('/n')) {readch () ؛ lineend.lineend.line = line ؛ Tokens.add (lineend.lineend) ؛ Line ++ ؛ العودة صحيح. } case '(': readch () ؛ delimiter.lpar.line = line ؛ tokens.add (delimiter.lpar) ؛ return true ؛ case ')': readch () ؛ delimiter.rpar.line = line ؛ Tokens.add (delimiter.rpar) ؛ العودة صحيح. حالة '؛': readch () ؛ selimiter.sem.line = line ؛ Tokens.Add (SELIMITER.SEM) ؛ العودة صحيح. Case '+': readch () ؛ calcword.add.line = line ؛ Tokens.Add (calcword.add) ؛ العودة صحيح. القضية "-': readch () ؛ calcword.sub.line = line ؛ Tokens.add (calcword.sub) ؛ العودة صحيح. حالة '*': readch () ؛ calcword.mul.line = line ؛ Tokens.Add (calcword.mul) ؛ العودة صحيح. حالة '/': readch () ؛ calcword.div.line = line ؛ Tokens.Add (calcword.div) ؛ العودة صحيح. الحالة ':': if (readch ('=')) {readch () ؛ calcword.assign.line = line ؛ Tokens.Add (calcword.assign) ؛ العودة صحيح. } استراحة؛ Case '>': if (readch ('=')) {readch () ؛ calcword.ge.line = line ؛ Tokens.add (calcword.ge) ؛ العودة صحيح. } استراحة؛ Case '<': if (readch ('=')) {readch () ؛ calcword.le.line = line ؛ Tokens.Add (calcword.le) ؛ العودة صحيح. } استراحة؛ Case '!': if (readch ('=')) {readch () ؛ calcword.ne.line = line ؛ Tokens.Add (calcword.ne) ؛ العودة صحيح. } استراحة؛ } إرجاع خطأ ؛ } /*يبدأ التالي في تقسيم الكلمات الرئيسية والمعرفات والمعرفات الأخرى* / الرمز المميز العام () يلقي iOexception {token tok ؛ بينما (الحرف == '') readch () ؛ if (iSdigit () || issign () || isletter ()) {tok = tokens.get (tokens.size () - 1) ؛ } آخر {tok = رمز جديد (حرف) ؛ الطابعة (TOK) ؛ } إرجاع tok ؛ }} حزمة yue.lexicalanalyzer ؛/ * * token parent class */public class token {public final int tag ؛ خط int العام = 1 ؛ اسم السلسلة العامة = "" ؛ POS POS العام = 0 ؛ الرمز المميز العام (int t) {this.tag = t ؛ } السلسلة العامة toString () {return "" + (char) tag ؛ }} package yue.lexicalanalyzer ؛/*** واجهة فئة الكلمات*/علامة الفئة العامة {public final static int begin = 1 ، // word end end = 2 ، // محفوظة الكلمات integer = 3 ، // وظيفة الكلمات المحفوظة = 4 ، // كلمة محفوظة القراءة = 5 ، // محفوظة كلمة الكتابة = 6 ، كلمة محفوظة إذا كانت كلمة محفوظة. // المعرف ثابت = 12 ، // ثابت إضافة = 13 ، // المشغل "+" sub = 14 ، // المشغل "-" mul = 15 ، // operator "*" div = 16 ، // operator "/" le = 18 ، // operator "<=" ge = 19 ، // complator "> =" ne = 20 ، // operator "! RPAR = 25 ، // operator ")" SEM = 26 ، // operator "؛" line_end = 27 ، // dorpator all_end = 28 ؛ // عامل "#"} حزمة yue.lexicalanalyzer ؛/*** الكلمات المحجوزة*/الكلمة الرئيسية الفئة العامة تمتد الرمز المميز {public string lexme = "" ؛ الكلمة الرئيسية العامة (السلسلة S ، int t) {super (t) ؛ this.lexme = s ؛ this.name = "الكلمات المحجوزة" ؛ } السلسلة العامة tostring () {return this.lexme ؛ } الكلمة الرئيسية النهائية الثابتة العامة تبدأ = كلمة رئيسية جديدة ("start" ، tag.begin) ، end = new kwerke ("end" ، tag.end) ، integer = new kwork integer "، tag.integer) ، function = new keyword (" function "، tag.function) ، read = new Keyword (" read "، tag.read) الكلمة الرئيسية ("if" ، tag.if) ، athen = الكلمة الرئيسية الجديدة ("ثم" ، tag.then) ، aelse = الكلمة الرئيسية الجديدة ("else" ، tag.else) ؛} حزمة yue.lexicalanalyzer ؛/** editerifier*/رمز الفئة العامة يمتد الرمز المميز {public string lexme = "" ؛ الرمز العام (سلسلة S) {super (tag.symbol) ؛ this.lexme = s ؛ this.name = "Identifier" ؛ } السلسلة العامة tostring () {return this.lexme ؛ }} package yue.lexicalanalyzer ؛/*** عامل التشغيل*/calcword الفئة العامة يمتد الرمز المميز {public string lexme = "" ؛ calcword العامة (السلسلة S ، int t) {super (t) ؛ this.lexme = s ؛ this.name = "Operator" ؛ } السلسلة العامة tostring () {return this.lexme ؛ ) tag.ne) ، issis = calcword جديد (": =" ، tag.assign) ؛} package yue.lexicalanalyzer ؛/*** Symbol*/class public class يمتد الرمز المميز {public string lexme = "" ؛ المحدد العام (سلسلة S ، int t) {super (t) ؛ this.lexme = s ؛ this.name = "رمز السحب" ؛ } السلسلة العامة tostring () {return this.lexme ؛ ) حزمة yue.lexicalanalyzer ؛/** ثابت*/فئة عامة num يمتد الرمز المميز {public final int value ؛ Num (int v) {super (tag.constant) ؛ this.value = v ؛ this.name = "ثابت" ؛ } السلسلة العامة tostring () {return "" + value ؛ }} package yue.lexicalanalyzer ؛/*** نهاية أحرف الخط*/خط سطر الفئة العامة يمتد الرمز المميز {public string lexme = "" ؛ خط خط عام (سلسلة S) {super (tag.line_end) ؛ this.lexme = s ؛ this.name = "نهاية أحرف الخط" ؛ } السلسلة العامة tostring () {return this.lexme ؛ } خط الخط النهائي الثابت العام = خط جديد ("/r/n") ؛} package yue.lexicalanalyzer ؛/*** ending character*/allend الفئة العامة يمتد الرمز المميز {public string lexme = "" ؛ Public Allend (سلسلة S) {super (tag.all_end) ؛ this.lexme = s ؛ this.name = "End Character" ؛ } السلسلة العامة tostring () {return this.lexme ؛ )لخص
سوف أنام مع المحتوى الكامل لهذه المقالة. آمل أن يكون محتوى هذه المقالة من بعض المساعدة في دراستك أو عملك. إذا كان لديك أي أسئلة ، فيمكنك ترك رسالة للتواصل.