2017-04-07 1 views
0

여러 토큰을 구분하고 싶습니다.
내 코드를 살펴보십시오.사용 지침 javacc 토큰

TOKEN : 
{ 
    < LOOPS : 
    <BEAT> 
    | <BASS> 
    | <MELODY> 
    > 
| < #BEAT : "beat" > 
| < #BASS : "bass" > 
| < #MELODY : "melody" > 
} 

void findType(): 
{Token loops;} 
{ 
loops = <LOOPS> 
{ String type = loops.image; } 

유형을 찾으려면 findType() 함수를 사용하고 싶습니다.
입력이 "박자"일 때 올바른 출력을 반환하려면 어떻게해야합니까? 당신이 원하는 무엇

답변

1

는 다음과 같이 return 문을 추가하는 것입니다

String findType(): 
{Token loops;} 
{ 
    loops = <LOOPS> 
    { 
     String type = loops.image; 
     return type; 
    } 
} 

void에서 String에, 방법에 반환 값의 정의를 변경 염두에두고.

그런 다음 주부터 :

Reading from standard input... 
Enter loop:bass 
Type is: bass 
Reading from standard input... 
Enter loop:beat 
Type is: beat 
+0

미안 해요 :

ExampleGrammar parser = new ExampleGrammar(System.in); while (true) { System.out.println("Reading from standard input..."); System.out.print("Enter loop:"); try { String type = ExampleGrammar.findType(); System.out.println("Type is: " + type); } catch (Exception e) { System.out.println("NOK."); System.out.println(e.getMessage()); ExampleGrammar.ReInit(System.in); } catch (Error e) { System.out.println("Oops."); System.out.println(e.getMessage()); break; } } 

그것은 같은 출력을 생성합니다. 간단히 쓰려고 했으므로 잘못 쓰려고했습니다. 함수의 반환 값은 이미 문자열입니다. 내가 알고 싶은 것은 토큰의 정의입니다. 위와 같이 'LOOPS'를 정의하면 반환 값이 나오지 않습니다. –