词汇分析器:汇编到java

rwqw0loc  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(156)

我目前正在做一个小项目,它需要一个词汇分析器来组装成java。我很难找出为每个标记分配类型的情况。注解很简单,因为它们以“;”开头一直走到队伍的尽头,但是对于其他人,我有很大的困难。
有5个基本的类别,我想做,助记名称,标签,地址,偏移量,和评论。我尝试使用switch cases来实现这一点,我将第一个case设置为comments,因为我认为这是最容易处理的。我的计划是用尽所有的可能性,留下默认情况下的记忆,然而,当试图拿出一个标签的情况下,我不知所措。我知道标签总是一行的第一列,而且我知道一旦我在箱子里,我只会从标签的开始直到一个空白处取这行的子字符串,但是我的问题是它的箱子。你认为在这种情况下,其他的陈述会更好吗?请给我你的建议如何正确地执行这一点。事先谢谢大家的帮助,非常感谢。请找到下面的代码,以及一个输入示例。

import java.util.List;
import java.util.ArrayList; 

public class Lexer 
{
    public static enum Type
    { 
        //This Assembly code has 5 token types 
        MNUMONIC_NAMES, LABELS, ADDRESSES, OFFSETS, COMMENTS;
    } 

    public static class Token
    { 
        public final Type t; 
        public final String c; 

        //constructor (set type)
        public Token(Type t, String c) 
        { 
            this.t = t; 
            this.c = c;
        } 

        //toString
        public String toString() 
        { 
            if(t == Type.MNUMONIC_NAMES) 
            { 
                return "MNUMONIC_NAMES<" + c + ">";
            } 

            if(t == Type.LABELS) 
            { 
                return "LABELS<" + c + ">";
            }  

            if(t == Type.ADDRESSES) 
            { 
                return "ADDRESSES<" + c + ">";
            } 

            if(t == Type.OFFSETS) 
            { 
                return "OFFSETS<" + c + ">";
            } 

            if(t == Type.COMMENTS) 
            { 
                return "COMMENTS<" + c + ">";
            } 

            return t.toString();
        }
    } 

    //Given a String and an index, get the mnumonic starting at index
    public static String getMNUMONIC_NAMES(String s, int i) 
    { 
        int j = i; 
        for(;j < s.length(); ) 
        { 
            //while character is a letter, continue
            if(Character.isLetter(s.charAt(j)) ) 
            { 
                j++;
            } 
            else 
            { 
                return s.substring(i, j);
            }
        } 
        return s.substring(i,j);
    }  

    //Given a String and an index, get the comment starting at index
        public static String getComment(String s, int i) 
        { 
            int j = i; 
            for(;j < s.length(); ) 
            { 
                //continues until end of line
                if(s.charAt(j) != '\n') 
                { 
                    j++;
                } 
                else 
                { 
                    return s.substring(i, j);
                }
            } 
            return s.substring(i,j);
        }  

        //Given a String and an index, get the label starting at index
                public static String getLabel(String s, int i) 
                { 
                    int j = i; 
                    for(;j < s.length(); ) 
                    { 
                        //while character is a letter, continue
                        if(Character.isLetter(s.charAt(j)) )  
                        { 
                            j++;
                        } 
                        else 
                        { 
                            return s.substring(i, j);
                        }
                    } 
                    return s.substring(i,j);
                } 

    public static List<Token> lex(String input)
    {
        List<Token> result = new ArrayList<Token>(); 
        for(int i = 0;i < input.length();) 
        { 
            switch(input.charAt(i)) 
            {  
                //case comment
                case ';': 
                    String comment = getComment(input, i); 
                    i+=comment.length();
                    result.add(new Token(Type.COMMENTS, comment)); 
                case '
            }
        }
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题