मैंने एएसटी पेड़ का उपयोग करके एंटर 3 का उपयोग करके एक जटिल व्याकरण विकसित किया है। एएनटीएलआर लेक्सर और पार्सर उत्पन्न करता है। समस्या यह है कि जब उपयोगकर्ता एक वाक्यविन्यास में प्रवेश करता है जो उदाहरण के लिए मान्य नहीं है, तो व्याकरण ';' की अपेक्षा कर रहा है। उपयोगकर्ता मैं निम्नलिखित अपवाद मिल मेरी ग्रहण आईडीई में तो यह प्रवेश नहीं करता,:एंटरल हैंडलिंग अपवाद
line 1:24 mismatched input '<EOF>' expecting ';'
कैसे इस अपवाद संभाला जा सकता है क्योंकि मैं इस अपवाद को पकड़ने की कोशिश, लेकिन अपवाद catched नहीं है। क्या यह एक अपवाद है? मुझे समझ में नहीं आता कि यह अपवाद क्यों पकड़ा नहीं गया है। मैंने पता लगाने की कोशिश की, हालांकि एंटरल वेबसाइट अब कुछ समय से नीचे प्रतीत होती है।
मैंने निम्न पर देखा: ANTLR exception handling with "$", Java और उस उदाहरण का पालन किया, लेकिन जब लेक्सर RuntimeException() जोड़कर कोड उत्पन्न करता है, तो मुझे पहुंचने योग्य कोड नहीं मिलता है।
मुझे यकीन नहीं है कि क्या करना है।
जब मैं पार्सर यह प्रदर्शित करता है से वाक्यविन्यास त्रुटियों की संख्या प्राप्त करने की कोशिश 0.
संपादित करें:
मैं एक समाधान है कि को देखकर काम करता पाया है: ANTLR not throwing errors on invalid input
हालांकि, जब मैं अपवाद संदेश वापस पाने का प्रयास करता हूं, यह शून्य है। क्या मैंने सब ठीक से सेट किया है? कृपया नमूना व्याकरण देखें:
grammar i;
options {
output=AST;
}
@header {
package com.data;
}
@rulecatch {
catch(RecognitionException e) {
throw e;
}
}
// by having these below it makes no difference
/**@parser::members {
@Override
public void reportError(RecognitionException e) {
throw new RuntimeException("Exception : " + " " + e.getMessage());
}
}
@lexer::members {
@Override
public void reportError(RecognitionException e) {
throw new RuntimeException("Exception : " + " " + e.getMessage());
}
}*/
संपादित करें:
कृपया देखो क्या मैं अब तक है:
grammar i;
options {
output=AST;
}
@header {
package com.data;
}
@rulecatch {
// ANTLR does not generate its normal rule try/catch
catch(RecognitionException e) {
throw e;
}
}
@parser::members {
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
throw new RuntimeException(hdr + ":" + msg);
}
}
@lexer::members {
@Override
public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
throw new RuntimeException(hdr + ":" + msg);
}
}
operatorLogic : 'AND' | 'OR';
value : STRING;
query : (select)*;
select : 'SELECT'^ functions 'FROM table' filters?';';
operator : '=' | '!=' | '<' | '>' | '<=' | '>=';
filters : 'WHERE'^ conditions;
members : STRING operator value;
conditions : (members (operatorLogic members)*);
functions : '*';
STRING : ('a'..'z'|'A'..'Z')+;
WS : (' '|'\t'|'\f'|'\n'|'\r')+ {skip();}; // handle white space between keywords
public class Processor {
public Processor() {
}
/**
* This method builds the MQL Parser.
* @param args the args.
* @return the built IParser.
*/
private IParser buildMQLParser(String query) {
CharStream cs = new ANTLRStringStream(query);
// the input needs to be lexed
ILexer lexer = new ILexer(cs);
CommonTokenStream tokens = new CommonTokenStream();
IParser parser = new IParser(tokens);
tokens.setTokenSource(lexer);
// use the ASTTreeAdaptor so that the grammar is aware to build tree in AST format
parser.setTreeAdaptor((TreeAdaptor) new ASTTreeAdaptor().getASTTreeAdaptor());
return parser;
}
/**
* This method parses the MQL query.
* @param query the query.
*/
public void parseMQL(String query) {
IParser parser = buildMQLParser(query);
CommonTree commonTree = null;
try {
commonTree = (CommonTree) parser.query().getTree();
}
catch(Exception e) {
System.out.println("Exception :" + " " + e.getMessage());
}
}
}
public class ASTTreeAdaptor {
public ASTTreeAdaptor() {
}
/**
* This method is used to create a TreeAdaptor.
* @return a treeAdaptor.
*/
public Object getASTTreeAdaptor() {
TreeAdaptor treeAdaptor = new CommonTreeAdaptor() {
public Object create(Token payload) {
return new CommonTree(payload);
}
};
return treeAdaptor;
}
}
तो जब मैं यह डालें: बिना तालिका
से चुनें * ए ';'
catch(Exception e) {
System.out.println("Exception : " + " " e);
}
जब मैं कोशिश:
e.getMessage();
यह शून्य देता है मैं एक MismatchedTokenException मिलता है।
यह या तो काम नहीं करता है। जब मैं संदेश वापस पाने का प्रयास करता हूं, तो मैं सामान्य रूप से वापस वापस आ जाता हूं। – user1646481
कृपया मेरे पास अब जो कुछ है उसके ऊपर संपादित करें देखें। शायद मैं यहाँ कुछ गलत कर रहा हूँ। मैं अपवादों को ट्रैक नहीं करना चाहता हूं लेकिन वास्तव में उन्हें प्रिंट करता हूं। – user1646481
यह सुनिश्चित नहीं करता है कि यह मदद करता है: http://stackoverflow.com/questions/4627244/catching-errors-in-antlr-and-finding-parent – user1646481