Implementation Code of a simple lexical analyzer (implemented in Java)

Source: Internet
Author: User

Main. Java

/* <Br/> * main <SPAN class = 'wp _ keywordlink '> Program </span> <br/> */<br/> Import Java. io. *; <br/> Import lexer. *; </P> <p> public class main {<br/> Public static void main (string [] ARGs) throws ioexception {<br/> lexer = new lexer (); </P> <p> while (lexer. getreaderstate () = false) {<br/> lexer. scan (); <br/>}</P> <p>/* save related information */<br/> lexer. savetokens (); <br/> lexer. savesymbolstable (); </P> <p >}< br/>

Lexer. Java

Package lexer; </P> <p> Import Java. io. *; <br/> Import Java. util. *; </P> <p> import symbols. *; </P> <p> public class lexer {<br/> Public static int line = 1;/* record row number */<br/> char peek = ''; /* The next read character */<br/> hashtable <string, word> words = <br/> New hashtable <string, word> (); <br/>/* symbol table */<br/> private hashtable <token, string> table = <br/> New hashtable <token, string> (); <br/>/* token sequence */<br/> private list <String> tokens = <br/> New Variable list <string> (); <br/>/* read file variable */<br/> bufferedreader reader = NULL; <br/>/* save whether the current file has been read to the end */<br/> private Boolean isend = false; </P> <p>/* Whether to read the end Of the file */<br/> Public Boolean getreaderstate () {<br/> return this. isend; <br/>}</P> <p>/* save it in table */<br/> Public void savesymbolstable () throws ioexception {<br/> filewriter writer = new filewriter ("symbol table .txt"); <br/> wri Ter. write ("[Symbol] [symbol type information] \ n"); <br/> writer. write ("\ r \ n"); </P> <p> enumeration <token> E = table. keys (); <br/> while (E. hasmoreelements () {<br/> token = (token) E. nextelement (); <br/> string DESC = table. get (token); </P> <p>/* Write File */<br/> writer. write (token + "\ t" + DESC + "\ r \ n"); <br/>}</P> <p> writer. flush (); <br/>}</P> <p>/* save tokens */<br/> Public void savetokens () throws ioexception {<br/> fil Ewriter writer = new filewriter ("tokens .txt"); <br/> writer. write ("[Symbol] \ n"); <br/> writer. write ("\ r \ n"); </P> <p> for (INT I = 0; I <tokens. size (); ++ I) {<br/> string Tok = (string) tokens. get (I); </P> <p>/* Write File */<br/> writer. write (Tok + "\ r \ n"); <br/>}</P> <p> writer. flush (); <br/>}</P> <p> void reserve (word w) {<br/> words. put (W. lexme, W ); <br/>}</P> <p>/* <br/> * add keywords and types to hashtable words in the constructor <br/> */< Br/> Public lexer () {<br/>/* initialize and read the file variable */<br/> try {<br/> reader = new bufferedreader (New filereader ("input .txt ")); <br/>}< br/> catch (ioexception e) {<br/> system. out. print (E); <br/>}</P> <p>/* keyword */<br/> This. reserve (New Word ("if", Tag. if); <br/> This. reserve (New Word ("then", Tag. then); <br/> This. reserve (New Word ("else", Tag. else); <br/> This. reserve (New Word ("while", Tag. while); <br/> This. reserv E (New Word ("do", Tag. do); </P> <p>/* type */<br/> This. reserve (word. true); <br/> This. reserve (word. false); <br/> This. reserve (type. INT); <br/> This. reserve (type. char); <br/> This. reserve (type. bool); <br/> This. reserve (type. float); <br/>}</P> <p> Public void readch () throws ioexception {<br/>/* Here it should be used */<br/> peek = (char) reader. read (); <br/> If (INT) peek = 0 xFFFF) {<br/> This. isend = true; <br/>}< br/> // Peek = (char) system. in. read (); <br/>}</P> <p> Public Boolean readch (char ch) throws ioexception {<br/> readch (); <br/> If (this. peek! = CH) {<br/> return false; <br/>}</P> <p> This. peek = ''; <br/> return true; <br/>}</P> <p> Public token scan () throws ioexception {<br/>/* clear blank */<br/> for (; readch ()) {<br/> If (PEEK = ''| peek = '\ t') <br/> continue; <br/> else if (PEEK = '\ n') <br/> line = line + 1; <br/> else <br/> break; <br/>}</P> <p>/* start to split keywords, identifiers, and other information. */<br/> switch (PEEK) {<br/>/* For =, >=, <= ,! = Implementation using the state machine */<br/> case '=': <br/> If (readch ('=') {<br/> tokens. add ("="); <br/> return word. eq; <br/>}< br/> else {<br/> tokens. add ("="); <br/> return new token ('='); <br/>}< br/> case '> ': <br/> If (readch ('=') {<br/> tokens. add ("> ="); <br/> return word. GE; <br/>}< br/> else {<br/> tokens. add (">"); <br/> return new token ('>'); <br/>}< br/> case '<': <br/> If (readch ('=') {<br/> tokens. add ("<="); <br /> Return word. le; <br/>}< br/> else {<br/> tokens. add ("<"); <br/> return new token ('<'); <br/>}< br/> case '! ': <Br/> If (readch (' = ') {<br/> tokens. Add ("! = "); <Br/> return word. Ne; <br/>}< br/> else {<br/> tokens. Add ("! "); <Br/> return new token ('! '); <Br/>}</P> <p>/* the following section identifies numbers. According to grammar, <br/> * The number can only be an integer. <br/> */<br/> If (character. isdigit (PEEK) {<br/> int value = 0; <br/> do {<br/> value = 10 * value + character. digit (Peek, 10); <br/> readch (); <br/>}while (character. isdigit (PEEK); </P> <p> num n = new num (value); <br/> tokens. add (N. tostring (); <br/> // table. put (n, "num"); <br/> return N; <br/>}</P> <p>/* <br/> * keyword or identifier recognition <br/> */<Br/> If (character. isletter (PEEK) {<br/> stringbuffer sb = new stringbuffer (); </P> <p>/* first obtain the entire split */<br/> do {<br/> Sb. append (PEEK); <br/> readch (); <br/>}while (character. isletterordigit (PEEK); </P> <p>/* determines whether it is a keyword or an identifier */<br/> string S = sb. tostring (); <br/> word w = (Word) words. get (s); </P> <p>/* if it is a keyword or type, W should not be empty */<br/> If (W! = NULL) {<br/> // table. put (W, "keyword or type"); <br/> tokens. add (W. tostring (); <br/> return W; /* Indicates a keyword or type name */<br/>}</P> <p>/* otherwise, it is an identifier ID */<br/> W = new word (s, tag. ID); <br/> tokens. add (W. tostring (); <br/> table. put (W, "ID"); <br/> words. put (S, W); </P> <p> return W; <br/>}</P> <p>/* any character in peek is considered as a lexical unit. */<br/> token Tok = new token (PEEK ); <br/> // table. put (Tok, "token or seprator"); <br/> If ((INT) peek! = 0 xFFFF) <br/> tokens. add (Tok. tostring (); <br/> peek = ''; </P> <p> return Tok; <br/>}< br/>

Num. Java

Package lexer; </P> <p> public class num extends token {<br/> Public final int value; </P> <p> Public num (int v) {<br/> super (tag. num); <br/> This. value = V; <br/>}</P> <p> Public String tostring () {<br/> return "" + value; <br/>}< br/>

Tag. Java

Package lexer; </P> <p> public class tag {<br/> Public final static int <br/> and = 256, <br/> basic = 257, <br/> Break = 258, <br/> DO = 259, <br/> else = 260, <br/> eq = 261, /* = */<br/> false = 262, <br/> Ge = 263, <br/> id = 264, <br/> If = 265, <br/> Index = 266, <br/> Le = 267, <br/> minus = 268, <br/> Ne = 269, <br/> num = 270, <br/> OR = 271, <br/> real = 272, <br/> temp = 273, <br/> true = 274, <br/> while = 275, <br/>/* Add */<br/> then = 276; <br/>}< br/>

Token. Java

Package lexer; </P> <p> public class token {<br/> Public final int tag; </P> <p> Public token (int t) {<br/> This. tag = T; <br/>}</P> <p> Public String tostring () {<br/> return "" + (char) Tag; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> token Tok = new token ('A '); <br/> system. out. println (Tok); <br/>}< br/>

Word. Java

/* <br/> * class word is used to manage reserved words, identifier and compound word elements like. <Br/> */<br/> package lexer; </P> <p> public class word extends token {<br/> Public String lexme = ""; </P> <p> Public word (string S, int t) {<br/> super (t); <br/> This. lexme = s; <br/>}</P> <p> Public String tostring () {<br/> return this. lexme; <br/>}</P> <p> Public static final word <br/> and = New Word ("&", Tag. and), <br/> OR = New Word ("|", Tag. or), <br/> eq = New Word ("=", Tag. EQ), <br/> Ne = new word ("! = ", Tag. ne), <br/> Le = New Word ("<=", Tag. le), <br/> Ge = New Word ("> =", Tag. ge), <br/> minus = New Word ("minus", Tag. minus), <br/> true = New Word ("true", Tag. true), <br/> false = New Word ("false", Tag. false), <br/> temp = New Word ("T", Tag. temp); <br/>}< br/>

Type. Java

/* <Br/> * description of the data type <br/> */<br/> package symbols; </P> <p> Import lexer. *; </P> <p> public class type extends word {<br/> Public type (string S, int tag) {<br/> super (S, tag ); <br/>}</P> <p> Public static final type <br/> Int = new type ("int", Tag. basic), <br/> float = new type ("float", Tag. basic), <br/> char = new type ("char", Tag. basic), <br/> bool = new type ("bool", Tag. basic); <br/>}< br/>

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.