/** 词法构造器
* class WordsAnalyse: 词法分析类的主体
* class WordsConstrucor: 用于保存输出的<tag,word>类型
* class Constants: 用来保存用于最后进行tag选择的类
* @author xulanshan
* */
import java.util.* ;
public class WordsAnalyse {
Scanner input = new Scanner(System.in) ; //控制台输出
StringBuffer word = new StringBuffer() ; //保存输出的内容
String[] keys = Constants.keys ; //关键字数组
int index ; //当前指向的缓冲区内容
char ch ; //当前判断的字符
char[] data = {} ; //当前IO读入的字符串
public static void main(String[] args){
WordsAnalyse analyse = new WordsAnalyse() ;
if(analyse.input.hasNext()){
String data = analyse.input.next() ;
//System.out.println(data) ;
analyse.index = 0 ;
analyse.data = data.toCharArray() ;
analyse.specialCh(analyse.getCh()) ;
}
}
//获取字符
char getCh(){
ch = ' ' ;
if(this.index < this.data.length){
//System.out.print("getCh()1 ch = " +ch);
ch = this.data[index] ;
index++ ;
}else{
this.index = 0 ;
this.data = this.input.nextLine().toCharArray() ;
if(data.length > 0){
//System.out.print("getCh()2" + "-----length= " +data.length +" >< index= " +index);
ch = this.data[index] ;
index ++ ;
}
}
//System.out.println( ch + " length--" +this.data.length +this.input.hasNext()) ;
return ch ;
}
//是否字符,数字或者其他
void specialCh(char ch){
if(ch >= 'A' && ch <= 'z'){ //字符串
//System.out.println("Strings----") ;
word.append(ch) ;
this.specialCh(getCh()) ;
}else if(ch >= '0' && ch <= '9'){ //数字
//System.out.println("Numbers----") ;
word.append(ch) ;
this.specialCh(getCh()) ;
}else{ //其他情况
//System.out.println("Others----") ;
//System.out.println(word.toString()) ;
if(this.word.length() > 0){
if(word.charAt(0) < '0' || word.charAt(0) > '9'){ //字符串
int tag = ifKey(word.toString()) ;
if( tag >= 0){
System.out.println(new WordsConstructor(tag, word.toString() ) ) ;
}else{
System.out.println(new WordsConstructor(Constants.WORDS, word.toString() ) ) ;
}
}else
System.out.println(new WordsConstructor(Constants.NUMBERS, word.toString() ) ) ;
word.delete(0, word.length()) ;
}
process(ch) ;
this.specialCh(getCh()) ;
}
}
//判断是否关键字
int ifKey(String word){
for(int i=0;i<6;i++){ //判断是否是关键字
if(word.toString().equals(keys[i])){
return i ;
}
}
return -1 ;
}
//进行其他字符判断
void process(char ch){
switch(ch){
case '+':
System.out.println(new WordsConstructor(Constants.ADD, "+" ) ) ;
break ;
case '-':
System.out.println(new WordsConstructor(Constants.MINUS, "-" ) ) ;
break ;
case '*':
if( getCh() == '/')
System.out.println(new WordsConstructor(Constants.RIGHT_ANNOTATION, "!=" ) ) ;
System.out.println(new WordsConstructor(Constants.STAR, "*" ) ) ;
break ;
case '/':
if( getCh() == '*')
System.out.println(new WordsConstructor(Constants.LEFT_ANNOTATION, "/*" ) ) ;
System.out.println(new WordsConstructor(Constants.BACKSLASH, "/" ) ) ;
break ;
case '=':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.DOUBLE_EQUAL, "==" ) ) ;
System.out.println(new WordsConstructor(Constants.EQUAL, "=" ) ) ;
break ;
case '!':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.NOT_EQUAL, "!=" ) ) ;
break ;
case ',':
System.out.println(new WordsConstructor(Constants.COMMA, "," ) ) ;
break ;
case ';':
System.out.println(new WordsConstructor(Constants.SEMICCOLON, ";" ) ) ;
break ;
case '#':
System.out.println(new WordsConstructor(Constants.POUND_SIGN, "#" ) ) ;
break ;
case '>':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.LARGER_AND_EQUAL, ">=" ) ) ;
System.out.println(new WordsConstructor(Constants.LARGER, ">" ) ) ;
break ;
case '<':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.LOWER_AND_EQUAL, "<=" ) ) ;
System.out.println(new WordsConstructor(Constants.LOWER, "<" ) ) ;
break ;
case '(':
System.out.println(new WordsConstructor(Constants.OPEN_PARENTHESIS, "(" ) ) ;
break ;
case ')':
System.out.println(new WordsConstructor(Constants.CLOSED_PARENTHESIS, ")" ) ) ;
break ;
case '[':
System.out.println(new WordsConstructor(Constants.LEFT_TITLE, "[" ) ) ;
break ;
case ']':
System.out.println(new WordsConstructor(Constants.RIGHT_TITLE, "]" ) ) ;
break ;
case '{':
System.out.println(new WordsConstructor(Constants.OPEN_BRACKET, "{" ) ) ;
break ;
case '}':
System.out.println(new WordsConstructor(Constants.CLOSED_BRACKET, "}" ) ) ;
break ;
default:
//System.out.println(ch);
}
}
}
/*输出类型*/
class WordsConstructor{
private int tag ;
private String word ;
public WordsConstructor(int tag,String word){
this.word = word ;
this.tag = tag ;
}
public String toString(){
return "<" +this.tag +"," +this.word +">" ;
}
}
/*常量*/
class Constants{
public static final String[] keys = {"else", "if", "int", "return", "void", "while"};
public static int KEY_1 = 0 ; // 'else'
public static int KEY_2 = 1 ; // 'if'
public static int KEY_3 = 2 ; // 'int'
public static int KEY_4 = 3 ; // 'return'
public static int KEY_5 = 4 ; // 'void'
public static int KEY_6 = 5 ; // 'while'
public static int WORDS = 7 ; // 'strings'
public static int NUMBERS = 8 ; // 'number'
public static int STAR = 13 ; // '*'
public static int BACKSLASH = 14 ; // '/'
public static int ADD = 15 ; // '+'
public static int MINUS = 16 ; // '-'
public static int DOUBLE_EQUAL = 17 ; // '=='
public static int NOT_EQUAL = 18 ; // '!='
public static int COMMA = 19 ; // ','
public static int LARGER = 20 ; // '>'
public static int OPEN_BRACKET = 21 ; // '{'
public static int LOWER_AND_EQUAL = 22 ; // '<='
public static int LOWER = 23 ; // '<'
public static int LARGER_AND_EQUAL= 24 ; // '>='
public static int EQUAL = 25 ; // '='
public static int SEMICCOLON = 26 ; // ';'
public static int OPEN_PARENTHESIS = 27 ; // '('
public static int CLOSED_PARENTHESIS = 28 ; // ')'
public static int POUND_SIGN = 29 ; // '#'
public static int CLOSED_BRACKET = 30 ; // '}'
public static int LEFT_ANNOTATION = 31 ; // '/*'
public static int RIGHT_ANNOTATION = 32 ; // '*/'
public static int LEFT_TITLE = 33 ; // '['
public static int RIGHT_TITLE = 34 ; // ']'
}
* class WordsAnalyse: 词法分析类的主体
* class WordsConstrucor: 用于保存输出的<tag,word>类型
* class Constants: 用来保存用于最后进行tag选择的类
* @author xulanshan
* */
import java.util.* ;
public class WordsAnalyse {
Scanner input = new Scanner(System.in) ; //控制台输出
StringBuffer word = new StringBuffer() ; //保存输出的内容
String[] keys = Constants.keys ; //关键字数组
int index ; //当前指向的缓冲区内容
char ch ; //当前判断的字符
char[] data = {} ; //当前IO读入的字符串
public static void main(String[] args){
WordsAnalyse analyse = new WordsAnalyse() ;
if(analyse.input.hasNext()){
String data = analyse.input.next() ;
//System.out.println(data) ;
analyse.index = 0 ;
analyse.data = data.toCharArray() ;
analyse.specialCh(analyse.getCh()) ;
}
}
//获取字符
char getCh(){
ch = ' ' ;
if(this.index < this.data.length){
//System.out.print("getCh()1 ch = " +ch);
ch = this.data[index] ;
index++ ;
}else{
this.index = 0 ;
this.data = this.input.nextLine().toCharArray() ;
if(data.length > 0){
//System.out.print("getCh()2" + "-----length= " +data.length +" >< index= " +index);
ch = this.data[index] ;
index ++ ;
}
}
//System.out.println( ch + " length--" +this.data.length +this.input.hasNext()) ;
return ch ;
}
//是否字符,数字或者其他
void specialCh(char ch){
if(ch >= 'A' && ch <= 'z'){ //字符串
//System.out.println("Strings----") ;
word.append(ch) ;
this.specialCh(getCh()) ;
}else if(ch >= '0' && ch <= '9'){ //数字
//System.out.println("Numbers----") ;
word.append(ch) ;
this.specialCh(getCh()) ;
}else{ //其他情况
//System.out.println("Others----") ;
//System.out.println(word.toString()) ;
if(this.word.length() > 0){
if(word.charAt(0) < '0' || word.charAt(0) > '9'){ //字符串
int tag = ifKey(word.toString()) ;
if( tag >= 0){
System.out.println(new WordsConstructor(tag, word.toString() ) ) ;
}else{
System.out.println(new WordsConstructor(Constants.WORDS, word.toString() ) ) ;
}
}else
System.out.println(new WordsConstructor(Constants.NUMBERS, word.toString() ) ) ;
word.delete(0, word.length()) ;
}
process(ch) ;
this.specialCh(getCh()) ;
}
}
//判断是否关键字
int ifKey(String word){
for(int i=0;i<6;i++){ //判断是否是关键字
if(word.toString().equals(keys[i])){
return i ;
}
}
return -1 ;
}
//进行其他字符判断
void process(char ch){
switch(ch){
case '+':
System.out.println(new WordsConstructor(Constants.ADD, "+" ) ) ;
break ;
case '-':
System.out.println(new WordsConstructor(Constants.MINUS, "-" ) ) ;
break ;
case '*':
if( getCh() == '/')
System.out.println(new WordsConstructor(Constants.RIGHT_ANNOTATION, "!=" ) ) ;
System.out.println(new WordsConstructor(Constants.STAR, "*" ) ) ;
break ;
case '/':
if( getCh() == '*')
System.out.println(new WordsConstructor(Constants.LEFT_ANNOTATION, "/*" ) ) ;
System.out.println(new WordsConstructor(Constants.BACKSLASH, "/" ) ) ;
break ;
case '=':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.DOUBLE_EQUAL, "==" ) ) ;
System.out.println(new WordsConstructor(Constants.EQUAL, "=" ) ) ;
break ;
case '!':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.NOT_EQUAL, "!=" ) ) ;
break ;
case ',':
System.out.println(new WordsConstructor(Constants.COMMA, "," ) ) ;
break ;
case ';':
System.out.println(new WordsConstructor(Constants.SEMICCOLON, ";" ) ) ;
break ;
case '#':
System.out.println(new WordsConstructor(Constants.POUND_SIGN, "#" ) ) ;
break ;
case '>':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.LARGER_AND_EQUAL, ">=" ) ) ;
System.out.println(new WordsConstructor(Constants.LARGER, ">" ) ) ;
break ;
case '<':
if( getCh() == '=')
System.out.println(new WordsConstructor(Constants.LOWER_AND_EQUAL, "<=" ) ) ;
System.out.println(new WordsConstructor(Constants.LOWER, "<" ) ) ;
break ;
case '(':
System.out.println(new WordsConstructor(Constants.OPEN_PARENTHESIS, "(" ) ) ;
break ;
case ')':
System.out.println(new WordsConstructor(Constants.CLOSED_PARENTHESIS, ")" ) ) ;
break ;
case '[':
System.out.println(new WordsConstructor(Constants.LEFT_TITLE, "[" ) ) ;
break ;
case ']':
System.out.println(new WordsConstructor(Constants.RIGHT_TITLE, "]" ) ) ;
break ;
case '{':
System.out.println(new WordsConstructor(Constants.OPEN_BRACKET, "{" ) ) ;
break ;
case '}':
System.out.println(new WordsConstructor(Constants.CLOSED_BRACKET, "}" ) ) ;
break ;
default:
//System.out.println(ch);
}
}
}
/*输出类型*/
class WordsConstructor{
private int tag ;
private String word ;
public WordsConstructor(int tag,String word){
this.word = word ;
this.tag = tag ;
}
public String toString(){
return "<" +this.tag +"," +this.word +">" ;
}
}
/*常量*/
class Constants{
public static final String[] keys = {"else", "if", "int", "return", "void", "while"};
public static int KEY_1 = 0 ; // 'else'
public static int KEY_2 = 1 ; // 'if'
public static int KEY_3 = 2 ; // 'int'
public static int KEY_4 = 3 ; // 'return'
public static int KEY_5 = 4 ; // 'void'
public static int KEY_6 = 5 ; // 'while'
public static int WORDS = 7 ; // 'strings'
public static int NUMBERS = 8 ; // 'number'
public static int STAR = 13 ; // '*'
public static int BACKSLASH = 14 ; // '/'
public static int ADD = 15 ; // '+'
public static int MINUS = 16 ; // '-'
public static int DOUBLE_EQUAL = 17 ; // '=='
public static int NOT_EQUAL = 18 ; // '!='
public static int COMMA = 19 ; // ','
public static int LARGER = 20 ; // '>'
public static int OPEN_BRACKET = 21 ; // '{'
public static int LOWER_AND_EQUAL = 22 ; // '<='
public static int LOWER = 23 ; // '<'
public static int LARGER_AND_EQUAL= 24 ; // '>='
public static int EQUAL = 25 ; // '='
public static int SEMICCOLON = 26 ; // ';'
public static int OPEN_PARENTHESIS = 27 ; // '('
public static int CLOSED_PARENTHESIS = 28 ; // ')'
public static int POUND_SIGN = 29 ; // '#'
public static int CLOSED_BRACKET = 30 ; // '}'
public static int LEFT_ANNOTATION = 31 ; // '/*'
public static int RIGHT_ANNOTATION = 32 ; // '*/'
public static int LEFT_TITLE = 33 ; // '['
public static int RIGHT_TITLE = 34 ; // ']'
}
测试程序:
int gcd (int u, int v)
{
if (v == 0)
return u ;
else
return gcd(v,u-u/v*v);
/* u-u/v*v == u mod v */
}
void main(void)
{
int x;
int y;
x = input();
y = input();
output(gcd(x,y));
}