JAVA实现的C-词法构造器(编译原理)

/** 词法构造器
 * 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));

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.根据状态转换图直接编程 编写一个词法分析程序,它从左到右逐个字符的对源程序进行扫描,产生一个个的单词的二元式,形成二元式(记号)流文件输出。在此,词法分析程序作为单独的一遍,如下图所示。 具体任务有: (1)组织源程序的输入 (2)识别单词的类别并记录类别编号和值,形成二元式输出,得到单词流文件 (3)删除注释、空格和无用符号 (4)发现并定位词法错误,需要输出错误的位置在源程序中的第几行。将错误信息输出到屏幕上。 (5)对于普通标识符和常量,分别建立标识符表和常量表(使用线性表存储),当遇到一个标识符或常量时,查找标识符表或常量表,若存在,则返回位置,否则返回0并且填写符号表或常量表。 标识符表结构:变量名,类型(整型、实型、字符型),分配的数据区地址 注:词法分析阶段只填写变量名,其它部分在语法分析、语义分析、代码生成等阶段逐步填入。 常量表结构:常量名,常量值 单词的构词规则: 字母=[A-Z a-z] 数字=[0-9] 标识符=(字母|_)(字母|数字|_)* 数字=数字(数字)*( .数字+|) 2.S语言表达式和语句说明 1.算术表达式:+、-、*、/、% 2.关系运算符:>、>=、<、<=、==、!= 3.赋值运算符:=,+=、-=、*=、/=、%= 4.变量说明:类型标识符 变量名表; 5.类型标识符:int char float 6.If语句:if 表达式then 语句 [else 语句] 7.For语句:for(表达式1;表达式2;表达式3) 语句 8.While语句:while 表达式 do 语句 9.S语言程序:由函数构成,函数不能嵌套定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值