0
我想與你分享我必須在ANTLR4中實施的島嶼解決方案。島嶼解決方案與ANTLR4
- 該語言的結構。我必須編寫語法的語言來自 帶有一些附加結構的PL/SQL。我不會在此討論更多細節,因爲這是脫離主題。
語言定義了一個特殊命令
PUT
具有以下結構:PUT [<SPECIALISED LANGUAGE>]
。
我的解決辦法是:
覆蓋詞法的
nextToken
方法:public Token nextToken() { if (f_current_idx != -1) { _input.seek(f_current_idx); f_current_idx = -1; } Token l_token = super.nextToken(); return l_token; }
添加一些代碼在詞法:
PUT : 'PUT' { f_current_idx = _input.index(); ((ANTLRStringStream) _input).rewind(); SRC_PUTLexer l_put_lexer = new SRC_PUTLexer(_input); UnbufferedTokenStream<Token> l_tokenStream = new UnbufferedTokenStream<Token>(l_put_lexer); if (l_tokenStream.LA(2) == SRC_PUTLexer.LBRACK) { new SRC_PUTParser(l_tokenStream).start_rule(); f_current_idx = _input.index(); } };
而且已在ANTLR 4不見了類ANTLRStringStream
必須定義:
public class ANTLRStringStream extends ANTLRInputStream {
protected int markDepth = 0;
protected int lastMarker;
protected ArrayList<Integer> markers;
public ANTLRStringStream() {
super();
}
public ANTLRStringStream(String input) {
super(input);
}
public int mark() {
if (markers==null) { markers = new ArrayList<Integer>(); }
markers.add(markDepth, index()); markDepth++; lastMarker = markDepth;
return markDepth;
}
public void rewind(int m) {
int state = (int) markers.get(m); seek(state); release(m);
}
public void rewind() { rewind(lastMarker); }
public void release(int marker) {
markDepth = marker; markDepth--;
}
}
任何反饋將是非常歡迎! 親切的問候,WolfgangHämmer