Java simple implementation of compiler principle lexical analysis

Source: Internet
Author: User
Tags switch case

Package Com.csray;import Java.io.bufferedreader;import Java.io.file;import java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;public class Lexicalanalysis {static String id;public static void Main ( String[] args) throws ioexception{lexicalanalysis la = new Lexicalanalysis (); String context =//la. bufferedreaderdemo//("C:" +system.getproperty ("file.separator") + "Users" +system.getproperty ("file.separator") + " Administrator "+system.getproperty (" file.separator ") +" Desktop "+system.getproperty (" file.separator ") +" Lexical "); La. Bufferedreaderdemo ("c:\\users\\administrator\\desktop\\lexical\\lexical.c"); SYSTEM.OUT.PRINTLN (context); for (int i = 0; i < context.length ();) {char NOWC = Context.charat (i);//system.out.println (NOWC); if (NOWC = = "| | nowc = = ' \ n ' | | nowc = = ' \ t ') {id =" illegalcharacter "; ++i;} else if (Isalpha (NOWC)) {i = alphaprocess (context, NOWC, i);} else if (IsDigit (NOWC)) {i = digitprocess (context, NOWC, i);//i ++;} else {i = otherprocess (context, NOWC, i);//i++;}}} Read the lexical.c file to Stringpublic string Bufferedreaderdemo (String path) throws Ioexception{file file = new file (p ATH); if (!file.exists () | | | file.isdirectory ()) throw new FileNotFoundException (); BufferedReader br = new BufferedReader (new FileReader (file)); String tmp = NULL; StringBuffer Sbuff = new StringBuffer (); tmp = Br.readline (); while (tmp! = null) {Sbuff.append (tmp+ ""); tmp = Br.readline (); }return sbuff.tostring ();} alphaprocesspublic static int alphaprocess (String context, char c, int i) {StringBuffer word = new StringBuffer (); while ( Isalpha (c)) | | (IsDigit (c)) | | c = = ' _ ') {Word.append (c); c = Context.charat (++i);} System.out.println (Word.tostring ());//check this word if or not a keywordif (Checkkeyword (word.tostring ())) {id = " Iskeyword "; System.out.println ("(" + word.tostring () + "," + ID + ")"); else {id = "Iscommonword"; System.out.println ("(" + word.tostring () + "," + ID + ")"); return i;} Checkkeywordpublic Static Boolean Checkkeyword (string word) {string keyword = ' Auto double int struct break else long ' switch case enum register typedef char ' + ' extern return Union const float Short Unsigned continue for signed void default goto "+" sizeof volatile does if while static Scnaf printf "; string[] KeyWords = Keyword.split (""); for (int i = 0; i < keywords.length; ++i) {if (Word.equals (Keywords[i])) {return true;}} return false;} digitprocesspublic static int digitprocess (String context, char c, int i) {stringbuffer digit = new StringBuffer (); while (IsDigit (c)) {digit.append (c); c = Context.charat (++i);} id = "IsDigit"; System.out.println ("(" + digit.tostring () + "," + ID + ")"); return i;} otherprocesspublic static int otherprocess (String context, char c, int i) {//+/() {} + =/= & "" StringBuffer O Perator = new StringBuffer (); StringBuffer delimiter = new StringBuffer (), if (c = = ' + ') {operator.append (c); id = "Isoperator"; c = Context.charat (++i); if (c = = ' = ') {operator.append (c); System.out.println ("(" + operator + "," + ID + ")"); return ++i;} else if (c = = ' + ') {operator.append (c); System.out.println ("(" + operator + "," + ID + ")"); return ++i;} else {System.out.println ("+ operator +", "+ ID +") "); return---------;} else if (c = = ' (') {delimiter.append (c); id = "Isdelimiter"; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = ') ') {delimiter.append (c); id = "Isdelimiter"; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = ' {') {delimiter.append (c); id = "Isdelimiter"; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = '} ') {delimiter.append (c); id = "Isdelimiter"; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = '; ') {delimiter.append (c); id = "Isdelimiter"; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = ' = ') {operator.append (c); id = "Isoperator"; c = Context.charat (++i); if (c = = ' = ') {operator.append (c); System.out.println ("(" + operator + "," + ID + ")"); return ++i;} else {System.out.println ("(" + OPerator + "," + ID + ")"); return i;}} else if (c = = '/') {operator.append (c); c = Context.charat (++i); if (c = = ' = ') {operator.append (c); id = "Isoperator"; System.out.println ("(" + operator + "," + ID + ")"); return ++i;} else if (c = = '/') {operator.append (c); id = "Isannotation"; c = Context.charat (++i); while (c! = ") {operator.append (c); C = Co Ntext.charat (++i);} System.out.println ("(" + operator + "," + ID + ")"); return i;}} else if (c = = ' & ') {operator.append (c); id = "Isoperator"; System.out.println ("(" + operator + "," + ID + ")"); return ++i;} else if (c = = ' "') {delimiter.append (c); id =" Isdelimiter "; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = ', ') {delimiter.append (c); id = "Isdelimiter"; System.out.println ("(" + delimiter + "," + ID + ")"); return ++i;} else if (c = = '% ') {operator.append (c); id = "Isoperator"; c = Context.charat (++i); if (c = = ' d ') {operator.append (c); System.out.println ("(" + operator + "," + ID + ")"); return ++i;} Return-----; return i;} IsdIgitpublic Static Boolean isdigit (char c) {if (c >= ' 0 ' && C <= ' 9 ') return True;return false;} Isalphapublic Static Boolean isalpha (char c) {if (C >= ' a ' && C <= ' Z ') return True;else if (c >= ' a ' & & c <= ' z ') return True;return false;}}

Instance:

Testint Main () {float Nu0_m;int num = 100;num++; scanf ("%d", &num);p rintf ("%d", num); return 0;}

Results:

Test int main () {  float nu0_m; int num = num++  ; scanf ("%d", &num); printf ("%d", num); return 0; } (//test, isannotation) (int, Iskeyword) (Main, Iscommonword) ((, Isdelimiter) (), Isdelimiter) ({, isdelimiter) (Flo At, Iskeyword) (Nu0_m, Iscommonword) (;, isdelimiter) (int, iskeyword) (num, iscommonword) (=, isoperator) (ISDI) git) (;, isdelimiter) (num, Iscommonword) (+ +, isoperator) (;, Isdelimiter) (scanf, Iscommonword) ((, Isdelimiter) (", Isdelimiter) (%d, Isoperator) (", Isdelimiter) (,, Isdelimiter) (&, isoperator) (num, Iscommonword) (), Isdelimiter) (;, Isdelimiter) (printf, Iskeyword) ((, Isdelimiter) (", Isdelimiter) (%d, Isoperator) (", Isdelimiter) (,, isdelimiter) (num, Iscommonword) (), Isdelimiter ) (;, Isdelimiter) (Return, Iskeyword) (0, IsDigit) (;, Isdelimiter) (}, Isdelimiter)

  

Java simple implementation of compiler principle lexical analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.