2013-02-28 5 views
-1

(x == y), (t != s), (a = b + c)과 같은 문자열이 있습니다. 이제 왼쪽 글자 x, t, a 및 오른쪽 글자 y, s, b, c을 가져와야합니다. 이를 위해 Java에서 문자열을 올바르게 분할 할 수 없습니다.

나는 다음을 수행 오전 :

while(//{condition}){ 
    eqn = stmnt.split("="); 
    getVars(eqn[0])); // first while round should return x, then t, and so on. 
    getVars(eqn[0])); // first while round should return y, then s, then b, c. 
} 

private static ArrayList<String> getVars(String str){ 
    ArrayList<String> variables = new ArrayList<String>(); 
    Pattern pattern = Pattern.compile("([a-zA-Z]+)"); 
     Matcher matcher = pattern.matcher(str); 
     while (matcher.find()) 
     { 
      variables.add(matcher.group()); 
     } 
     return variables; 
} 

자,이 코드는 (A = B + C)와 같은 식을 위해 잘 노력하고 있습니다. 앞의 두 방정식에서는 왼손 변수 (x, t) 만 반환하지만 오른손 변수 (y, s)는 반환하지 않습니다.

내가 뭘 잘못하고 있니?

도움이 될 것입니다! 우선 들어

+2

이 컴파일러의 일종해야합니까? 그때 또 다른 접근법을 제안 할 것입니다 ... – Sebastian

+0

답장을 보내 주셔서 감사합니다. 예, 입력 프로그램 코드를 기본 블록으로 나누고 변수의 활성을 분석하는 코드를 작성하려고합니다. 어떤 접근 방식을 제안하는지 알려주십시오. – kajarigd

+0

글쎄, 컴파일러에 대해 뭔가 배웁니다. 가장 쉬운 방법은 Java 기반 컴파일러 생성자 인 AntLR을 사용하는 것입니다. 문법 만 지정하면됩니다. 자동으로 AST를 생성합니다. – Sebastian

답변

0

: - 두 번째 getVars(eqn[0]));getVars(eqn[1]));

while(//{condition}){ 
    eqn = stmnt.split("="); 
    getVars(eqn[0])); // first while round should return x, then t, and so on. 
    getVars(eqn[1])); // first while round should return y, then s, then b, c. 
} 
+0

네, 맞아요! 감사! – kajarigd

관련 문제