사용자 입력이 정의 된 규칙과 일치하지 않을 때 사용자 정의 오류를보고해야합니다. 여기 antlr 3.0에서 오류가 발생했습니다.
내 코드입니다 :grammar second1;
@lexer::members {
@Override
public void reportError(RecognitionException e) {
System.out.println("Throwing Exception: "+ e.getMessage());
throw new IllegalArgumentException(e);
}
}
@parser::members {
private boolean inbounds(Token t, int min, int max, String methodName) {
int n = Integer.parseInt(t.getText());
if(n >= min && n <= max) {
return true;
}
else {
System.out.println("The range for value accepted by " + methodName+" is "+min +"-" + max);
return false;
}
}
}
expr : SET attribute EOF;
attribute : Value1 int1:integer1["Value1"] { System.out.println("Accepted"); }
| Value2 integer2 ["Value2"] { System.out.println("Accepted"); }
;
exception[int1]:
catch[Exception e] {System.out.println("Error Reported for int1");}
exception:
catch[Exception e] {System.out.println("General error Reported");}
integer1 [String methodName] : Int { inbounds($Int,0,1000,methodName) }? ;
integer2 [String methodName] : Int { inbounds($Int,0,10000,methodName) }? ;
Int : '0'..'9'+;
SET : 'set';
Value1 : 'value';
Value2 : 'value2';
fragment WS
: (' ' | '\t')
;
하지만이 코드를 컴파일하는 동안 다음과 같은 오류가 점점 오전 :
error(100): second1.g:26:22: syntax error: antlr: second1.g:26:22: unexpected token: int1
error(100): second1.g:29:17: syntax error: antlr: second1.g:29:17: unexpected token: :
error(100): second1.g:32:10: syntax error: antlr: second1.g:32:10: unexpected token: catch
error(100): second1.g:0:0: syntax error: assign.types: <AST>:0:0: unexpected AST node: <end-of-block>
error(100): second1.g:0:0: syntax error: assign.types: <AST>:0:0: unexpected end of subtree
error(100): second1.g:0:0: syntax error: define: <AST>:0:0: unexpected AST node: <end-of-block>
error(100): second1.g:0:0: syntax error: define: <AST>:0:0: unexpected AST node: <end-of-block>
error(100): second1.g:0:0: syntax error: define: <AST>:0:0: unexpected end of subtree
error(106): second1.g:26:27: reference to undefined rule: integer1
error(106): second1.g:27:22: reference to undefined rule: integer2
warning(105): second1.g:27:15: no lexer rule corresponding to token: Value2
warning(105): second1.g:26:15: no lexer rule corresponding to token: Value1
warning(105): second1.g:24:15: no lexer rule corresponding to token: SET
어떻게해야합니까를? :(내가 인터넷에서 확인
, 이것은 우리가
이 저를 도와주세요 :(
왜 그때 내 경우에는 작동하지 않습니다 ANTLR 3.x의에서 예외를 처리하는 방법입니다.