?? syntaxanalyizerjava.txt
字號:
package testExperiment
//Demonstrate Lex Analyzer
//The purpose of this experiment is to make us familiar with Lex-like Algorithem
//first let me make a introduction:
//This Analyzer is programmed by Java languange
//By useing 11 methods (including main method) ,it is concise and easy for reading
import java.io.*;
import java.util.StringTokenizer;
//Class name
public class Compiler_Simulate_Lex
{
//Main method
public static void main(String[] args)
{
//Set a counter
int counter = 0;
//It has 98 keywords
String[] Keyword = {
"absolute","end","inline","procedure","type","and","external","interface","program","unit","array","file","interrupt","record","until","begin","for","label","repeat","uses","case","forward","mod","set","var","const","function","nil","shl","while","div","goto","not","shr","with","do","if","of","string","xor","downto","implementation","in","then","else","or","packed","to","ABSOLUTE","END","INLINE","PROCEDURE","TYPE","AND","EXTERNAL","INTERFACE","PROGRAM","UNIT","ARRAY","FILE","INTERRUPT","RECORD","UNTIL","BEGIN","FOR","LABEL","REPEAT","USES","CASE","FORWARD","MOD","SET","VAR","CONST","FUNCTION","NIL","SHL","WHILE","DIV","GOTO","NOT","SHR","WITH","DO","IF","OF","STRING","XOR","DOWNTO","IMPLEMENTATION","IN","THEN","ELSE","OR","PACKED","TO","WRITE","write"
};
//It has 52 alphabetas
String Alphabeta = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//It has 10 numbers in all
String Digit = "0123456789";
//It has 13 identities
String Identity = "\"\'\\<=>:+-();/";
//Give a pascal program as a test
//The total number of words is 96
String TestPascalProgram = "program tripme ( input , output ) ; " +
"var " +
"x , y , c : integer ; " +
"function add ( int a , int b ) : int " +
"begin add = a + b ; end ; " +
"function subtract ( int a , int b ) : int " +
"begin subtract = a - b ; end ; " +
"begin x = 6 ; y = 2 ; c := subtract ( a , b ) ; " +
"write ( c ) ; c := add ( a , b ) ; " +
"write ( c ) ; end .";
//show the TestProgram
System.out.println("Here is a Pascal program for testing the Lex analyzer:");
System.out.println("***********************************************************");
System.out.println("program tripme (input , output);");
System.out.println("var ");
System.out.println("x , y , c : integer ; ");
System.out.println("function add (int a , int b) : int ");
System.out.println("begin add = a + b ; end ; ");
System.out.println("function subtract (int a , int b) : int ");
System.out.println("begin subtract = a - b ; end ; ");
System.out.println("begin x = 6 ; y = 2 ; c := subtract (a , b) ; ");
System.out.println("write (c) ; c := add (a , b) ; " );
System.out.println("write (c) ; end .");
//Remind that the analyze begins.
System.out.println("***********************************************************");
System.out.println("");
System.out.println("Now begin analyzing:");
System.out.println('\r');
//Create String[] to store TestProgram
String[] Tokens = (String[])tokenIntoArray(TestPascalProgram).clone();
//Use loop to identify program
for(counter = 0;counter <= numberOfTokens(TestPascalProgram);counter++)
{
//first check whether it is a keyword and then print the result out
System.out.print(Tokens[counter]);
if(isKeyword(Tokens[counter],Keyword))
{
System.out.println('\b'+": Is a keyword"+"and is located at"+
locateKeyword(Tokens[counter],Keyword));
}
//Then Check whether it is a Alphabeta
else if(isAlphabeta(Tokens[counter],Alphabeta))
{
//The process below is worthless,should be deleted
if(locateAlphabeta(Tokens[counter],Alphabeta) > 0)
{
System.out.print('\b'+": Is a Alphabeta"+"and is located at"
+locateAlphabeta(Tokens[counter],Alphabeta)+95);
}
else{}
}
//And then check whether it is a Digit
else if(isDigit(Tokens[counter],Digit))
{
System.out.println('\b'+": Is a digit"+"and is located at"+
locateDigit(Tokens[counter],Digit)+95+52);
}
//Finally check whether it is a Identity
else if(isIdentity(Tokens[counter],Identity))
{
System.out.println('\b'+": Is a identity and is located at"+
locateIdentity(Tokens[counter],Identity)+95+52+10);
}
}
System.out.println("Analysis completed successfully");
}
//Give a method to create a way,analyzing the Array of Tokens
//that whether it is a keyword
public static boolean isKeyword(String Token, String[] keyword)
{
int i = 0;
while(keyword[i] != Token)
{
i=i+1;
}
if(i>97) return false;
else return true;
}
//A method to analyze Tokens wheteher it is a Alphabeta.
public static boolean isAlphabeta(String Token, String alphabeta)
{
int i = 0;
while(Token.charAt(0) != alphabeta.charAt(i))
{
i++;
}
if(i>51) return false;
else return true;
}
//A method to analyze Tokens wheteher it is a digit.
public static boolean isDigit(String Token, String digit)
{
int i = 0;
while(Token.charAt(0) != digit.charAt(i))
{
i++;
}
if(i>9) return false;
else return true;
}
//A method to analyze Tokens wheteher it is a identity
public static boolean isIdentity(String Token, String identity)
{
int i = 0;
while(Token.charAt(0) != identity.charAt(i))
{
i++;
}
if(i>12) return false;
else return true;
}
//Make the testprogram into tokens and then store in an array
public static String[] tokenIntoArray(String s)
{
//make a counter to limit the process of copy
int counter = 0;
//Make an Array to store Tokens.
String[] Tokens = new String[98];
StringTokenizer Words = new StringTokenizer(s);
//Clone Tokens to Array
while(Words.hasMoreElements())
{
Tokens[counter] = Words.nextToken();
counter++;
}
return Tokens;
}
//Count the number of token
public static int numberOfTokens(String s)
{
StringTokenizer Words = new StringTokenizer(s);
return Words.countTokens();
}
//get the number in string
public static int locateKeyword(String Token, String[] keyword)
{
int i = 0;
while(keyword[i] != Token)
{
i++;
}
if (i <=95) return i;
else return -1;
}
//get the number in string
public static int locateAlphabeta(String Token, String alphabeta)
{
int i = 0;
while(Token.charAt(0)!= alphabeta.charAt(i))
{
i++;
}
if(i<=51) return i;
else return -1;
}
//get the number in string
public static int locateDigit(String Token, String digit)
{
int i = 0;
while(Token.charAt(0) != digit.charAt(i))
{
i++;
}
if(i<=9) return i;
else return -1;
}
//get the number in string
public static int locateIdentity(String Token, String identity)
{
int i = 0;
while(Token.charAt(0) != identity.charAt(i))
{
i++;
}
if(i<=12) return i;
else return -1;
}
}
class MyInput
{
public static String readString()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in),1);
String string = " ";
try
{
string = br.readLine();
}
catch(IOException ex)
{
System.out.println(ex);
}
return string;
}
public static int readInt()
{
return Integer.parseInt(readString());
}
public static double readDouble()
{
return Double.parseDouble(readString());
}
}
? 運行結果:
Here is a Pascal program for testing the Lex analyzer:
***********************************************************
program tripme (input , output);
var
x , y , c : integer ;
function add (int a , int b) : int
begin add = a + b ; end ;
function subtract (int a , int b) : int
begin subtract = a - b ; end ;
begin x = 6 ; y = 2 ; c := subtract (a , b) ;
write (c) ; c := add (a , b) ;
write (c) ; end .
***********************************************************
Now begin analyzing:
program : Is a keyword and is located at 1
tripme : Is a Alphabeta and is located at 8
(: Is a identity and is located at 19
input : Is a Alphabeta and is located at 9
, : Is a identity and is located at 20
output : Is a Alphabeta and is located at 10
) : Is a identity and is located at 21
; : Is a identity and is located at22
var : Is a keyword and is located at 2
x : Is a Alphabeta and is located at 11
, : Is a identity and is located at 20
y : Is a Alphabeta and is located at 12
, : Is a identity and is located at 20
c : Is a Alphabeta and is located at 13
: : Is a identity and is located at 23
integer : Is a keyword and is located at 3
; : Is a identity and is located at 22
function : Is a keyword and is located at 4
add : Is a Alphabeta and is located at 14
(: Is a identity and is located at 19
int : Is a Alphabeta and is located at 15
a : Is a Alphabeta and is located at 16
, : Is a identity and is located at 20
int : Is a Alphabeta and is located at 15
b : Is a Alphabeta and is located at 24
) : Is a identity and is located at 21
: : Is a identity and is located at 23
int : Is a Alphabeta and is located 15
begin : Is a keyword and is located at 5
add : Is a Alphabeta and is located at 14
=: Is a identity and is located at 24
a : Is a Alphabeta and is located at 16
+: Is a identity and is located at 25
b : Is a Alphabeta and is located at 17
; : Is a identity and is located at 22
end : Is a keyword and is located at 6
; : Is a identity and is located at 22
function : Is a keyword and is located at 4
subtract : Is a Alphabeta and is located at 18
(: Is a identity and is located at 19
int : Is a Alphabeta and is located at 15
a : Is a Alphabeta and is located at 16
, : Is a identity and is located at 20
int : Is a Alphabeta and is located at 15
b : Is a Alphabeta and is located at 17
) : Is a identity and is located at 21
: : Is a identity and is located at 23
int : Is a Alphabeta and is located at 15
begin : Is a keyword and is located at 5
subtract : Is a Alphabeta and is located at 18
=: Is a identity and is located at 24
a : Is a Alphabeta and is located at 16
-: Is a identity and is located at 25
b : Is a Alphabeta and is located at 17
; : Is a identity and is located at 22
end : Is a keyword and is located at 6
; : Is a identity and is located at 22
begin : Is a keyword and is located at 5
x : Is a Alphabeta and is located at 11
=: Is a identity and is located at 24
6 : Is a digit and is located at 19
; : Is a identity and is located at 22
y : Is a Alphabeta and is located at 12
=: Is a identity and is located at 24
2 : Is a digit and is located at 20
; : Is a identity and is located at 22
c : Is a Alphabeta and is located at 13
:= : Is a identity and is located at 26
subtract : Is a Alphabeta and is located at 18
(: Is a identity and is located at 19
a : Is a Alphabeta and is located at 16
, : Is a identity and is located at 20
b : Is a Alphabeta and is located at 17
) : Is a identity and is located at 21
; : Is a identity and is located at 22
write : Is a keyword and is located at 7
(: Is a identity and is located at 19
c : Is a Alphabeta and is located at 13
) : Is a identity and is located at 21
; : Is a identity and is located at 22
c : Is a Alphabeta and is located at 13
:= : Is a identity and is located at 26
Add : Is a Alphabeta and is located at 14
(: Is a identity and is located at 19
a : Is a Alphabeta and is located at 16
, : Is a identity and is located at 20
b : Is a Alphabeta and is located at 17
) : Is a identity and is located at 21
; : Is a identity and is located at 22
write : Is a keyword and is located at 7
(: Is a identity and is located at 19
c : Is a Alphabeta and is located at 13
) : Is a identity and is located at 21
; : Is a identity and is located at 22
end : Is a keyword and is located at 6
. : Is a identity and is located at 27
Analysis completed successfully
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -