编译原理实验-词法分析

* 编译原理词法分析:控制台输入方式
 * @author fei
 *
 */
/*
    保留字(1):if,int,for,while,do,return,break,continue
    单词标识(2):a arr
    整型常数(3):    1,2
    运算符(4):+,-,*,/,=,>,<,>=,<=,!=
    分隔符(5):{},()
    测试代码:
        main(){
            int a,b;
            a=10;
            b=a+20;
        }
 */
public class Test2 {
    public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         sc.useDelimiter("\n");
         String tr=sc.next();
         char[] ch=tr.toCharArray();
         compile cp=new compile();
         char s=0;
         StringBuffer token=new StringBuffer();//存放形参的单词
         for(int i=0;i<ch.length;i++){
             if(ch[i]>='a'&&ch[i]<='z'){
                 token.append(ch[i]);
             }
             if(ch[i]>=0&&ch[i]<=9){
                 token.append(ch[i]);
             }
             if((!cp.IsLetter(ch[i])&&!cp.IsNumber(ch[i]))||ch[i]==' '){
                    if(token.length()>0){
                        //cp.Retrat();
                        System.out.print("('1','"+token.toString()+"')");
                        token.delete(0, token.length());
                    }
             }
             //单目运算符
             if(ch[i]=='+'){
                 System.out.println("('4','"+ch[i]+"')");
             }
             if(ch[i]=='-'){
                 System.out.println("('4','"+ch[i]+"')");        
             }
             if(ch[i]=='*'){
                 System.out.println("('4','"+ch[i]+"')");
             }
             if(ch[i]=='/'){
                 System.out.println("('4','"+ch[i]+"')");
             }  
             //双目运算符
             if(ch[i]=='>'||ch[i]=='<'||ch[i]=='!'){
                    s=ch[i];
             }
             if(ch[i]=='='){
                 if(s!=0&&(s=='>'||s=='<'||s=='!')){
                     System.out.println("('4','"+s+ch[i]+"')");
                     s=0;
                 }else{
                     System.out.println("('4','"+ch[i]+"')");
                 }
             }
             //分隔符
             if(ch[i]=='('){
                 System.out.println("('5' '"+ch[i]+"')");
             }
             if(ch[i]==')'){
                 System.out.println("('5' '"+ch[i]+"')");    
             }
             if(ch[i]=='{'){
                 System.out.println("('5' '"+ch[i]+"')");
             }
             if(ch[i]=='}'){
                 System.out.println("('5' '"+ch[i]+"')");
             }
             if(ch[i]==';'){
                 System.out.println("('5' '"+ch[i]+"')");
             }
             if(ch[i]==','){
                 System.out.println("('5' '"+ch[i]+"')"); 
             }

         }


    }
}
class compile{
    int code;//返回的状态码
    String[] keys={"if","int","for","while","do","return","break","continue"};//保留字
    StringBuffer token=new StringBuffer();//存放形参的单词
    //连接字符
    public void concat(char ch){
        token.append(ch);
    }
    //检查连续的字符形成的字符串是否为保留字
    public int Reserve(){
        //为保留字时为1
        for(int i=0;i<keys.length;i++){
            if(token.toString().equals(keys[i]))
                return 1;
        }
        //为数字时返回3
        if(token.length()!=0){
            if(token.charAt(0)>='0'&&token.charAt(0)<='9'){
                return 3;
            }
        }
        //为变量名或函数名
        return 2;   
    }
    //根据状态码输出,并清空token
    public void Retrat(){
        code=Reserve();
        if(code==1){
            System.out.println("('"+1+"','"+token+"')");
        }else if(code==2){
            System.out.println("('"+2+"','"+token.toString()+"')");
        }else if(code==3){
            System.out.println("('"+3+"','"+token+"')");
        }
        token.delete(0, token.length());
    }
    public boolean IsLetter(char ch){
        if(ch>='a'&&ch<='z'){
            return true;
        }
        return false;
    }
    public boolean  IsNumber(char ch){
        if(ch>='0'&&ch<='9'){
            return true;
        }
        return false;
    }
}
* 编译原理词法分析:读取文件方式
 * @author fei
 *
 */
/*
    保留字(1):if,int,for,while,do,return,break,continue
    单词标识(2):a arr
    整型常数(3):    1,2
    运算符(4):+,-,*,/,=,>,<,>=,<=,!=
    分隔符(5):{},();
    测试代码:
        main(){
            int a,b;
            a=10;
            b=a+20;
        }
 */
public class Test4 {
    //存放构成单词符号的字符串:可能为keys\单词\数字
    public StringBuffer token = new StringBuffer(); 
    //保留字
    String[] keys={"if","int","for","while","do","return","break","continue","main"};
    //判断是否为字符
    public boolean IsLetter(int ch){
        if((ch>=65 && ch <= 90) || (ch >= 97 && ch <=122)){
            return true;
        }
        return false;
    }
    //是否为数字
    public boolean  IsNumber(int ch){
        if(ch>=48 && ch <= 57){
            return true;
        }
        return false;
    }
    //判断是否是空格
    public boolean IsEmpty(int ch){
        if(ch == 32){
            return true;
        }
        return false;
    }

    //连接字符
    public void concat(char ch){
        token.append(ch);
    }
    //检查连续的字符形成的字符串是否为保留字
    public int Reserve(){
        //为保留字时为1
        for(int i=0;i<keys.length;i++){
            if(token.toString().equals(keys[i]))
                return 1;
        }
        //为数字时返回3
        if(token.length()!=0){
            if(token.charAt(0)>='0'&&token.charAt(0)<='9'){
                return 3;
            }
        }
        //为变量名或函数名
        return 2;   
    }
    //根据状态码输出,并清空token
    public void Retrat(int code){
        code=Reserve();
        if(code==1){
            System.out.println("('"+1+"','"+token+"')");
        }else if(code==2){
            System.out.println("('"+2+"','"+token.toString()+"')");
        }else if(code==3){
            System.out.println("('"+3+"','"+token+"')");
        }
        token.delete(0, token.length());
    }
    public void scanner() throws IOException{
        BufferedReader  br = new BufferedReader(new FileReader("tests.txt"));
        //保存每次读取的字符
        int ch;
        //保存双目运算符的第一位
        char s1=0;
        while((ch =br.read()) != -1){
             char s=(char)ch;
             //如果为字符加入
             if(IsLetter(s)){
                 concat(s);
             }
             //如果为数字加入
             if(IsNumber(s)){
                 concat(s);
             }
             if((!IsLetter(s)&&!IsNumber(s))||IsEmpty(s)){
                    if(token.length()>0){
                        //单词,是否为关键字
                        //否则为数字
                        //System.out.println( token.length());
                        Retrat(Reserve());
                    }
             }
             //单目运算符
             if(s=='+'){
                 System.out.println("('4','"+s+"')");
             }
             if(s=='-'){
                 System.out.println("('4','"+s+"')");        
             }
             if(s=='*'){
                 System.out.println("('4','"+s+"')");
             }
             if(s=='/'){
                 System.out.println("('4','"+s+"')");
             }  
             //双目运算符,其中s1保存双目运算符的第一位
             if(s=='>'||s=='<'||s=='!'){
                    s1=s;
             }
             if(s=='='){
                 if(s1!=0&&(s1=='>'||s1=='<'||s1=='!')){
                     System.out.println("('4','"+s1+s+"')");
                     s1=0;
                 }else{
                     System.out.println("('4','"+s+"')");
                 }
             }
             //分隔符
             if(s=='('){
                 System.out.println("('5' '"+s+"')");
             }
             if(s==')'){
                 System.out.println("('5' '"+s+"')");    
             }
             if(s=='{'){
                 System.out.println("('5' '"+s+"')");
             }
             if(s=='}'){
                 System.out.println("('5' '"+s+"')");
             }
             if(s==';'){
                 System.out.println("('5' '"+s+"')");
             }
             if(s==','){
                 System.out.println("('5' '"+s+"')"); 
             }
        }
    }
    //测试
    public static void main(String[] args) throws IOException {
        Test4 t=new Test4();
        t.scanner();
    }
}

猜你喜欢

转载自blog.csdn.net/linkingfei/article/details/81569172