Package com.seakt.example;
Import java.io.*;
Import java.lang.String;
public class J_scanner {
Public String infile;
Public String outfile;
Public String []key = new string[33];
FileOutputStream out = null;
Public J_scanner (String infile,string outfile) {
J_Scanner.this.infile = infile;
J_Scanner.this.outfile = outfile;
String[] Key_temp = {"", "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", "do", "if", "while", "Static"};
key = Key_temp;
try {
out = new FileOutputStream (new File (outfile));
}
catch (IOException e) {
E.printstacktrace ();
}
}
Output keywords
public void Print_key () {
for (int i=0;i<j_scanner.this.key.length;i++) {
System.out.printf ("%s\r\n", J_scanner.this.key[i]);
}
}
Read the file
public void ReadFile () {
File File = new file (J_Scanner.this.infile);
BufferedReader reader = null;
try {
reader = new BufferedReader (new FileReader (file));
String tempstring = null;
while ((tempstring = Reader.readline ()) = null) {
System.out.println (Tempstring.length ());
GetToken (tempstring);
}
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (reader! = null) {
try {
Reader.close ();
} catch (IOException E1) {
}
}
}
}
Determine if the character
Private boolean isletter (char ch) {
if ((ch>=65&&ch<=90) | | | (ch>=97&&ch<=122) | | ch==35| | CH==46)
return true;
Else
return false;
}
Judging whether the number
Private boolean isdigit (char ch) {
if (ch>=48&&ch<=57)
return true;
Else
return false;
}
Find keywords
private int Reserve (String s) {
for (int i=1;i<33;i++)
if (S==key[i])
return i;
return 0;
}
private void GetToken (String s) throws ioexception{
String Str_write=null;
int I=0,code;
Char ch= ';
String temp= "";
if (S.length ()!=0) {
Ch=s.charat (i);
}
while (I<s.length ()) {
//If NULL skipped
while (I<s.length () &&ch== ") {
i++;
Ch=s.charat (i);
}
//Is the letter
if (Isletter (ch)) {
while ((Isletter (ch) | | | IsDigit (CH)) &&i<s.length ()) {
temp+=ch;
i++;
if (I<s.length ()-1) {
Ch=s.charat (i);
} else{
Ch= ";
}
}
i--;
Code=reserve (temp);
if (code==0) {
Str_write = temp+ "\ T" + "identifier" + "\ r \ n";
Out.write (Str_write.getbytes ());
temp= ""; str_write= "";
}else{
Str_write = temp+ "\ t" + "keyword" + "\ r \ n";
Out.write (Str_write.getbytes ());
temp= ""; str_write= "";
}
}else if (isdigit (ch)) {
while (isdigit (ch)) {
temp+=ch;
i++;
if (I<s.length ()-1) {
Ch=s.charat ( i);
}else{
Ch= ";
}
}
i--;
Str_write = temp+ "\ t" + "constant" + "\ r \ n";
Out.write (Str_write.getbytes ());
temp= ""; str_write= "";
}else if (ch== ' = ') {
i++;
Ch=s.charat (i);
if (ch== ' = ') {
Str_write = "= =" + "\ T" + "judgment equal" + "\ r \ n";
Out.write (Str_write.getbytes ());
temp= ""; str_write= "" ;
}
else{
i--;
Str_write = "=" + "\ T" + "assignment" + "\ r \ n";
Out.write (Str_write.getbytes ());
temp= ""; str_ Write= "";
}
}
Else if (ch== ' + ') {
i++;
Ch=s.charat (i);
if (ch== ' + ')
Out.write (("+ +" + ' \ T ' + "plus 1" + "\ r \ n"). GetBytes ());
else{
i--;
Out.write ("+ +" + ' \ T ' + "plus" + "\ r \ n"). GetBytes ());
}
}
Else if (ch== ' & ') {
i++;
Ch=s.charat (i);
if (ch== ' & ')
Out.write ("&&" + ' \ T ' + ' and ' + ' \ r \ n '). GetBytes ());
else{
i--;
Out.write (("&" + ' \ T ' + "bitwise +" + "\ r \ n"). GetBytes ());
}
}
Else if (ch== ' | ') {
i++;
Ch=s.charat (i);
if (ch== ' | ')
Out.write ("| | |" + ' \ t ' + ' or ' + ' \ r \ n '). GetBytes ());
else{
i--;
Out.write (("|" + ' \ t ' + ' bitwise or ' + ' \ r \ n '). GetBytes ());
}
}
else if (ch== '-')
Out.write ((char) ch+ ' \ t ' + "minus" + "\ r \ n"). GetBytes ());
else if (ch== '; ')
Out.write ((char) ch+ ' \ t ' + "semicolon" + "\ r \ n"). GetBytes ());
else if (ch== ' (')
Out.write ((char) ch+ ' \ t ' + "left parenthesis" + "\ r \ n"). GetBytes ());
else if (ch== ') ')
Out.write ((char) ch+ ' \ t ' + "right parenthesis" + "\ r \ n"). GetBytes ());
else if (ch== ' {')
Out.write ((char) ch+ ' \ t ' + "left curly brace" + "\ r \ n"). GetBytes ());
else if (ch== '} ')
Out.write ((char) ch+ ' \ t ' + "right curly brace" + "\ r \ n"). GetBytes ());
else if (ch== ' * ') {
i++;
Ch=s.charat (i);
if (ch== ' * ')
Out.write (("* *" + ' \ t ' + "operator" + "\ r \ n"). GetBytes ());
else{
i--;
Out.write ("*" + ' \ t ' + "multiplication sign" + "\ r \ n"). GetBytes ());
}
}
else if (ch== ' < ') {
i++;
Ch=s.charat (i);
if (ch== ' = ')
Out.write ("<=" + ' \ t ' + "less than equals" + "\ r \ n"). GetBytes ());
else{
i--;
Out.write ("<" + ' \ t ' + "less than" + "\ r \ n"). GetBytes ());
}
}
else if (ch== ' > ') {
i++;
if (I<s.length ()-1) {
Ch=s.charat (i);
}else{
Ch= ";
}
if (ch== ' = ')
Out.write (">=" + ' \ T ' + "is greater than or equal to" + \ r \ n "). GetBytes ());
else{
i--;
Out.write ("<" + ' \ T ' + "greater than" + "\ r \ n"). GetBytes ());
}
}
Else
Return
i++;
if (I<s.length ()) {
Ch=s.charat (i);
}
}
}
Turn off the output stream
public void Close_outstream () throws ioexception{
J_Scanner.this.out.close ();
}
}
Simple lexical analysis of Java