2012-03-11 3 views
0

다음은 문제입니다. TI 프로그래밍 언어에서 유효한 토큰 인 토큰 목록이 있습니다. 전적으로 16 진수의 데이터로 구성된 TI-83/84 프로그램에 유효한 토큰을 포함하는 텍스트를 컴파일하는 프로그램을 작성하려고합니다. 파일에 데이터를 쓰는 방법과 파일에서 데이터를 가져 오는 방법을 알고 있습니다. 프로그램을 검색하고 유효한 토큰을 식별하고 프로그램의 순서를 유지할 수있는 프로그램을 작성해야합니다. 저는 이미 토큰을 키워드로 사용하고 텍스트를 검색하는 방법을 생각했습니다. 그러나 처음 선택한 토큰이 항상 텍스트의 첫 번째 항목이 될 수는 없으므로 순서가 손실됩니다.Java - TI-84 토큰을 16 진 코드로 구문 분석

난 내 자신의 토큰 오브젝트를 작성했습니다 그리고 내 파서에서 다음 변수를 사용하고 있습니다 :

  • ArrayList를 토큰 - 토큰 오브젝트의 ArrayList를 (TI-BASIC의 모든 유효한 토큰)
  • 의 DataInputStream의 창피 - 또한 텍스트 파일
  • String 데이터 - loadData 방법을 사용하여 파일에서 프로그램 추출물 (문제가 아니라, 그래서 작동하지 중요한 방법) 텍스트 데이터
  • DataOutputStream 정렬 도스 - .8xp 파일 기입 대상 :

토큰 클래스 :

public class Token { 

    private String tokenText; 
    int[] hexValue; 
    boolean isTwoByte; 

    /** 
    * The default constructor. 
    * Assigns empty values to all instance variables. 
    */ 
    public Token() { 
    tokenText = ""; 
    hexValue = new int[0]; 
    isTwoByte = false; 
    } 

    /** 
    * Constructs a Token with <tt>text</tt> text and an empty int[] for <tt>hexValue</tt> 
    * @param text the text to be assigned to the Token 
    */ 
    public Token(String text) { 
    tokenText = text; 
    hexValue = new int[0]; 
    isTwoByte = false; 
    } 

    /** 
    * Constructs a Token with an empty String for <tt>text</tt> and <tt>hexValue</tt> value 
    * This is a one-byte Token 
    * @param value the value to be assigned to the Token 
    */ 
    public Token(int value) { 
    tokenText = ""; 
    hexValue = new int[1]; 
    hexValue[0] = value; 
    isTwoByte = false; 
    } 

    /** 
    * Constructs a Token with an empty String for <tt>text</tt> and <tt>hexValue></tt> v1v2 
    * This is a two-byte Token 
    * @param v1 the first byte of the two-byte value that will be assigned to the Token 
    * @param v2 the second byte of the two-byte value 
    */ 
    public Token(int v1, int v2) { 
    tokenText = ""; 
    hexValue = new int[2]; 
    hexValue[0] = v1; hexValue[1] = v2; 
    isTwoByte = true;   
    } 

    /** 
    * Constructs a Token with <tt>text</tt> text and <tt>hexValue</tt> value 
    * This is a one-byte Token 
    * @param text the text to be assigned to the Token 
    * @param value the value to be assigned to the one-byte Token 
    */ 
    public Token(String text, int value) { 
    tokenText = text; 
    hexValue = new int[1]; 
    hexValue[0] = value; 
    isTwoByte = false; 
    } 

    /** 
    * Constructs a Token with tokenText <tt>text</tt> text and <tt>hexValue</tt> v1v2 
    * This is a two-byte Token 
    * @param text the text to be assigned to the Token 
    * @param v1 the first byte of the two-byte value that will be assigned to the Token 
    * @param v2 the second byte of the two-byte value 
    */ 
    public Token(String text, int v1, int v2) { 
    tokenText = text; 
    hexValue = new int[2]; 
    hexValue[0] = v1; hexValue[1] = v2; 
    isTwoByte = true; 
    } 

    /** 
    * Returns the text that is assigned to the particular Token 
    * @return the text that is assigned to the particular Token 
    */ 
    public String getText() { 
    return tokenText; 
    } 

    /** 
    * Returns the byte value of the Token 
    * @return the byte value of the Token 
    */ 
    public int[] getValue() { 
    return hexValue; 
    } 

    /** 
    * Returns <tt>true</tt> if the Token is a two-byte Token, and <tt>false</tt> otherwise 
    * @return <tt>true</tt> if the Token is a two-byte Token, and <tt>false</tt> otherwise 
    */ 
    public boolean isTwoByte() { 
    return isTwoByte; 
    } 

    /** 
    * Sets the tokenText text of the Token to the String passed it. 
    * @param text the new text to be assigned to the Token 
    */ 
    public void setText(String text) { 
    tokenText = text; 
    } 

    /** 
    * Sets the byte value of the Token to the integer passed it. This sets the isTwoByte variable to <tt>false</tt>. 
    * @param value the new byte value to be assigned to the Token 
    */ 
    public void setValue(int value) { 
    hexValue = new int[1]; 
    hexValue[0] = value; 
    isTwoByte = false; 
    } 

    /** 
    * Sets the byte value of the Token to the integers passed it. This sets the isTwoByte variable to <tt>true</tt>. 
    * @param v1 value of the first byte of the two-byte Token 
    * @param v2 value of the second byte 
    */ 
    public void setValue(int v1, int v2) { 
    hexValue = new int[2]; 
    hexValue[0] = v1; hexValue[1] = v2; 
    isTwoByte = true; 
    } 
} 

나는 사람이 나를 도울 수 있기를 바랍니다. 더 이상 코드 같은 더 많은 정보가 필요하다면 문제가되지 않을 것입니다. 어쨌든 프로젝트는 오픈 소스가 될 것입니다. 이것에 대한 도움을 미리 감사드립니다.

+1

입력 데이터의 모양과 해당 데이터 집합의 출력 내용을 미리 볼 수 있습니까? 바라건대 각각에 대해 단지 몇 줄만 있으면됩니다. –

+0

입력 데이터는 UTF-8이 .txt 파일에 저장하고 다음과 같이 할 수있다 : 가 : TI-84 PROG 에 "Hello World"-> (PS)와 복수의 랜덤 DISP (PS)와 복수의 랜덤 액세스가 : 를 중지하고 최종 출력에 보관해야합니다 헤더를 포함하지 않는 다음 원시 16 진수 데이터를 포함해야하는 .8xp 파일 : 3E 54 49 71 38 34 29 50 BBC2 BBBF BBB6 3F 2A 48 BBB4 BBBC BBBC BBBF 29 57 BBBF BBC2 BBBC BBB3 2A 04 AA00 3F DE AA00 3E D9는 는 [:] [-] = 71 [] = 29 [-> = 는 [표시]가 DE 가 = [정지] = D9= 3E [이 "] 2A에게 를 =[Str1] = 토큰 값이있는 사전 결정된 변수 AA00 대문자는 일반 ASCII 인코딩과 동일합니다. 소문자는 BBB0에서 시작하는 2 바이트 토큰입니다. – user1260475

+0

위의 정보 형식이 좋지 않아 사과드립니다. 새로운 라인. "Prog"다음에 첫 번째 "Str1"뒤에 줄 바꿈이 있어야합니다. – user1260475

답변

0

직접 사용하지 않았지만 java.io.StreamTokenizer 클래스는 필요한 것을 수행합니까? 또는 전체 소스 파일을 읽을 수있는 경우 간단하게 StringTokenizer을 입력하십시오.

파일을 순서대로 검토하고 있으므로 순서 유지에 대해 걱정할 필요가 없습니다.

+0

StreamTokenizer가 유망 해 보입니다. BufferedReader를 사용하여 String 데이터를 가져옵니다. 그러나 저는 StreamTokenizer를 사용한 적이 없기 때문에 사전 구축 된 데이터베이스에서 특정 토큰을 검색하는 데 어떻게 사용합니까? – user1260475

+0

명확히하는 데 도움이된다면 선형 적으로 소스 코드를 파싱해야합니다.어떤 경우 든 토큰의 16 진수 값이 프로그램에서 어디로 가는지 추적해야합니다. * 내가하는 일은 정말로 중요하지 않습니다. 입력 해 주셔서 감사합니다. – user1260475

+0

이 질문 (http://stackoverflow.com/q/672577/732379)에는 컴파일러를 작성하면서 시작하기위한 몇 가지 사항이 있습니다. 그 질문에 링크가 많이 있습니다. – chooban