ASP Parser einführen

This commit is contained in:
JanUlrich 2018-06-15 06:15:36 +02:00
parent fe72936737
commit ba63da2860
7 changed files with 614 additions and 8 deletions

View File

@ -6,9 +6,7 @@ import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.ASPStringConverter;
import de.dhbwstuttgart.sat.asp.model.ASPRule;
import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultBaseListener;
import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultLexer;
import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultParser;
import de.dhbwstuttgart.sat.asp.parser.antlr.*;
import de.dhbwstuttgart.sat.asp.parser.model.ParsedType;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.typeinference.result.*;
@ -27,14 +25,11 @@ import java.util.stream.Collectors;
* TODO: Überlegen welche Informationen noch nach der Unifizierung gebraucht werden
* -> Eigentlich nur die korrekten Namen der Typen und TPHs
*/
public class ASPParser extends UnifyResultBaseListener {
public class ASPParser extends ASPResultBaseListener {
private Collection<TypePlaceholder> originalTPHs;
private ResultSet resultSet;
private Map<String, ParsedType> types = new HashMap<>();
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> types = new HashMap();
private Set<String> tphs = new HashSet<>();
private Map<String, ParameterListNode> parameterLists = new HashMap<>();
private Set<Relation> unifierRelations = new HashSet<>();
private Set<Relation> smallerRelations = new HashSet<>();
/**
* Parst clingo output welcher als JSON (option --outf=2) ausgibt
@ -135,6 +130,15 @@ public class ASPParser extends UnifyResultBaseListener {
}
}
*/
private void getTPHs(ASPResultParser.AnswerContext answer){
for(ASPResultParser.ResultSetRuleContext e : answer.resultSetRule()){
if(e.NAME().getText().equals(ASPRule.ASP_TYPE_VAR)){
tphs.add(e.parameterList().getChild(0).getText());
}
}
}
private ASPParser(String toParse, Collection<TypePlaceholder> oldPlaceholders){
//System.out.println(toParse);
this.originalTPHs = oldPlaceholders;

View File

@ -0,0 +1,13 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
NAME=6
WS=7
LINE_COMMENT=8
'ANSWER'=1
'.'=2
'('=3
','=4
')'=5

View File

@ -0,0 +1,87 @@
// Generated from ASPResult.g4 by ANTLR 4.7
package de.dhbwstuttgart.sat.asp.parser.antlr;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;
/**
* This class provides an empty implementation of {@link ASPResultListener},
* which can be extended to create a listener which only needs to handle a subset
* of the available methods.
*/
public class ASPResultBaseListener implements ASPResultListener {
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterAnswer(ASPResultParser.AnswerContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitAnswer(ASPResultParser.AnswerContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterResultSetRule(ASPResultParser.ResultSetRuleContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitResultSetRule(ASPResultParser.ResultSetRuleContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterParameterList(ASPResultParser.ParameterListContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitParameterList(ASPResultParser.ParameterListContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterValue(ASPResultParser.ValueContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitValue(ASPResultParser.ValueContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterEveryRule(ParserRuleContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitEveryRule(ParserRuleContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void visitTerminal(TerminalNode node) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void visitErrorNode(ErrorNode node) { }
}

View File

@ -0,0 +1,121 @@
// Generated from ASPResult.g4 by ANTLR 4.7
package de.dhbwstuttgart.sat.asp.parser.antlr;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class ASPResultLexer extends Lexer {
static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache();
public static final int
T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, NAME=6, WS=7, LINE_COMMENT=8;
public static String[] channelNames = {
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
};
public static String[] modeNames = {
"DEFAULT_MODE"
};
public static final String[] ruleNames = {
"T__0", "T__1", "T__2", "T__3", "T__4", "NAME", "WS", "LINE_COMMENT"
};
private static final String[] _LITERAL_NAMES = {
null, "'ANSWER'", "'.'", "'('", "','", "')'"
};
private static final String[] _SYMBOLIC_NAMES = {
null, null, null, null, null, null, "NAME", "WS", "LINE_COMMENT"
};
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
static {
tokenNames = new String[_SYMBOLIC_NAMES.length];
for (int i = 0; i < tokenNames.length; i++) {
tokenNames[i] = VOCABULARY.getLiteralName(i);
if (tokenNames[i] == null) {
tokenNames[i] = VOCABULARY.getSymbolicName(i);
}
if (tokenNames[i] == null) {
tokenNames[i] = "<INVALID>";
}
}
}
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
}
@Override
public Vocabulary getVocabulary() {
return VOCABULARY;
}
public ASPResultLexer(CharStream input) {
super(input);
_interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
@Override
public String getGrammarFileName() { return "ASPResult.g4"; }
@Override
public String[] getRuleNames() { return ruleNames; }
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public String[] getChannelNames() { return channelNames; }
@Override
public String[] getModeNames() { return modeNames; }
@Override
public ATN getATN() { return _ATN; }
public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\n\67\b\1\4\2\t\2"+
"\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\3\2\3\2\3\2\3"+
"\2\3\2\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\7\6\7$\n\7\r\7\16\7%"+
"\3\b\6\b)\n\b\r\b\16\b*\3\b\3\b\3\t\3\t\7\t\61\n\t\f\t\16\t\64\13\t\3"+
"\t\3\t\2\2\n\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\3\2\5\6\2\62;C\\aac|\5"+
"\2\13\f\16\17\"\"\4\2\f\f\17\17\29\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2"+
"\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\3\23\3"+
"\2\2\2\5\32\3\2\2\2\7\34\3\2\2\2\t\36\3\2\2\2\13 \3\2\2\2\r#\3\2\2\2\17"+
"(\3\2\2\2\21.\3\2\2\2\23\24\7C\2\2\24\25\7P\2\2\25\26\7U\2\2\26\27\7Y"+
"\2\2\27\30\7G\2\2\30\31\7T\2\2\31\4\3\2\2\2\32\33\7\60\2\2\33\6\3\2\2"+
"\2\34\35\7*\2\2\35\b\3\2\2\2\36\37\7.\2\2\37\n\3\2\2\2 !\7+\2\2!\f\3\2"+
"\2\2\"$\t\2\2\2#\"\3\2\2\2$%\3\2\2\2%#\3\2\2\2%&\3\2\2\2&\16\3\2\2\2\'"+
")\t\3\2\2(\'\3\2\2\2)*\3\2\2\2*(\3\2\2\2*+\3\2\2\2+,\3\2\2\2,-\b\b\2\2"+
"-\20\3\2\2\2.\62\7\'\2\2/\61\n\4\2\2\60/\3\2\2\2\61\64\3\2\2\2\62\60\3"+
"\2\2\2\62\63\3\2\2\2\63\65\3\2\2\2\64\62\3\2\2\2\65\66\b\t\2\2\66\22\3"+
"\2\2\2\6\2%*\62\3\b\2\2";
public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static {
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}

View File

@ -0,0 +1,13 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
NAME=6
WS=7
LINE_COMMENT=8
'ANSWER'=1
'.'=2
'('=3
','=4
')'=5

View File

@ -0,0 +1,50 @@
// Generated from ASPResult.g4 by ANTLR 4.7
package de.dhbwstuttgart.sat.asp.parser.antlr;
import org.antlr.v4.runtime.tree.ParseTreeListener;
/**
* This interface defines a complete listener for a parse tree produced by
* {@link ASPResultParser}.
*/
public interface ASPResultListener extends ParseTreeListener {
/**
* Enter a parse tree produced by {@link ASPResultParser#answer}.
* @param ctx the parse tree
*/
void enterAnswer(ASPResultParser.AnswerContext ctx);
/**
* Exit a parse tree produced by {@link ASPResultParser#answer}.
* @param ctx the parse tree
*/
void exitAnswer(ASPResultParser.AnswerContext ctx);
/**
* Enter a parse tree produced by {@link ASPResultParser#resultSetRule}.
* @param ctx the parse tree
*/
void enterResultSetRule(ASPResultParser.ResultSetRuleContext ctx);
/**
* Exit a parse tree produced by {@link ASPResultParser#resultSetRule}.
* @param ctx the parse tree
*/
void exitResultSetRule(ASPResultParser.ResultSetRuleContext ctx);
/**
* Enter a parse tree produced by {@link ASPResultParser#parameterList}.
* @param ctx the parse tree
*/
void enterParameterList(ASPResultParser.ParameterListContext ctx);
/**
* Exit a parse tree produced by {@link ASPResultParser#parameterList}.
* @param ctx the parse tree
*/
void exitParameterList(ASPResultParser.ParameterListContext ctx);
/**
* Enter a parse tree produced by {@link ASPResultParser#value}.
* @param ctx the parse tree
*/
void enterValue(ASPResultParser.ValueContext ctx);
/**
* Exit a parse tree produced by {@link ASPResultParser#value}.
* @param ctx the parse tree
*/
void exitValue(ASPResultParser.ValueContext ctx);
}

View File

@ -0,0 +1,318 @@
// Generated from ASPResult.g4 by ANTLR 4.7
package de.dhbwstuttgart.sat.asp.parser.antlr;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.misc.*;
import org.antlr.v4.runtime.tree.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class ASPResultParser extends Parser {
static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
new PredictionContextCache();
public static final int
T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, NAME=6, WS=7, LINE_COMMENT=8;
public static final int
RULE_answer = 0, RULE_resultSetRule = 1, RULE_parameterList = 2, RULE_value = 3;
public static final String[] ruleNames = {
"answer", "resultSetRule", "parameterList", "value"
};
private static final String[] _LITERAL_NAMES = {
null, "'ANSWER'", "'.'", "'('", "','", "')'"
};
private static final String[] _SYMBOLIC_NAMES = {
null, null, null, null, null, null, "NAME", "WS", "LINE_COMMENT"
};
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
@Deprecated
public static final String[] tokenNames;
static {
tokenNames = new String[_SYMBOLIC_NAMES.length];
for (int i = 0; i < tokenNames.length; i++) {
tokenNames[i] = VOCABULARY.getLiteralName(i);
if (tokenNames[i] == null) {
tokenNames[i] = VOCABULARY.getSymbolicName(i);
}
if (tokenNames[i] == null) {
tokenNames[i] = "<INVALID>";
}
}
}
@Override
@Deprecated
public String[] getTokenNames() {
return tokenNames;
}
@Override
public Vocabulary getVocabulary() {
return VOCABULARY;
}
@Override
public String getGrammarFileName() { return "ASPResult.g4"; }
@Override
public String[] getRuleNames() { return ruleNames; }
@Override
public String getSerializedATN() { return _serializedATN; }
@Override
public ATN getATN() { return _ATN; }
public ASPResultParser(TokenStream input) {
super(input);
_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
}
public static class AnswerContext extends ParserRuleContext {
public List<ResultSetRuleContext> resultSetRule() {
return getRuleContexts(ResultSetRuleContext.class);
}
public ResultSetRuleContext resultSetRule(int i) {
return getRuleContext(ResultSetRuleContext.class,i);
}
public AnswerContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_answer; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).enterAnswer(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).exitAnswer(this);
}
}
public final AnswerContext answer() throws RecognitionException {
AnswerContext _localctx = new AnswerContext(_ctx, getState());
enterRule(_localctx, 0, RULE_answer);
int _la;
try {
enterOuterAlt(_localctx, 1);
{
setState(8);
match(T__0);
setState(14);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==NAME) {
{
{
setState(9);
resultSetRule();
setState(10);
match(T__1);
}
}
setState(16);
_errHandler.sync(this);
_la = _input.LA(1);
}
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ResultSetRuleContext extends ParserRuleContext {
public TerminalNode NAME() { return getToken(ASPResultParser.NAME, 0); }
public ParameterListContext parameterList() {
return getRuleContext(ParameterListContext.class,0);
}
public ResultSetRuleContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_resultSetRule; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).enterResultSetRule(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).exitResultSetRule(this);
}
}
public final ResultSetRuleContext resultSetRule() throws RecognitionException {
ResultSetRuleContext _localctx = new ResultSetRuleContext(_ctx, getState());
enterRule(_localctx, 2, RULE_resultSetRule);
try {
enterOuterAlt(_localctx, 1);
{
setState(17);
match(NAME);
setState(18);
parameterList();
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ParameterListContext extends ParserRuleContext {
public List<ValueContext> value() {
return getRuleContexts(ValueContext.class);
}
public ValueContext value(int i) {
return getRuleContext(ValueContext.class,i);
}
public ParameterListContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_parameterList; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).enterParameterList(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).exitParameterList(this);
}
}
public final ParameterListContext parameterList() throws RecognitionException {
ParameterListContext _localctx = new ParameterListContext(_ctx, getState());
enterRule(_localctx, 4, RULE_parameterList);
int _la;
try {
enterOuterAlt(_localctx, 1);
{
setState(20);
match(T__2);
setState(21);
value();
setState(26);
_errHandler.sync(this);
_la = _input.LA(1);
while (_la==T__3) {
{
{
setState(22);
match(T__3);
setState(23);
value();
}
}
setState(28);
_errHandler.sync(this);
_la = _input.LA(1);
}
setState(29);
match(T__4);
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static class ValueContext extends ParserRuleContext {
public TerminalNode NAME() { return getToken(ASPResultParser.NAME, 0); }
public ResultSetRuleContext resultSetRule() {
return getRuleContext(ResultSetRuleContext.class,0);
}
public ValueContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_value; }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).enterValue(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof ASPResultListener ) ((ASPResultListener)listener).exitValue(this);
}
}
public final ValueContext value() throws RecognitionException {
ValueContext _localctx = new ValueContext(_ctx, getState());
enterRule(_localctx, 6, RULE_value);
try {
setState(33);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,2,_ctx) ) {
case 1:
enterOuterAlt(_localctx, 1);
{
setState(31);
match(NAME);
}
break;
case 2:
enterOuterAlt(_localctx, 2);
{
setState(32);
resultSetRule();
}
break;
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
public static final String _serializedATN =
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\n&\4\2\t\2\4\3\t"+
"\3\4\4\t\4\4\5\t\5\3\2\3\2\3\2\3\2\7\2\17\n\2\f\2\16\2\22\13\2\3\3\3\3"+
"\3\3\3\4\3\4\3\4\3\4\7\4\33\n\4\f\4\16\4\36\13\4\3\4\3\4\3\5\3\5\5\5$"+
"\n\5\3\5\2\2\6\2\4\6\b\2\2\2$\2\n\3\2\2\2\4\23\3\2\2\2\6\26\3\2\2\2\b"+
"#\3\2\2\2\n\20\7\3\2\2\13\f\5\4\3\2\f\r\7\4\2\2\r\17\3\2\2\2\16\13\3\2"+
"\2\2\17\22\3\2\2\2\20\16\3\2\2\2\20\21\3\2\2\2\21\3\3\2\2\2\22\20\3\2"+
"\2\2\23\24\7\b\2\2\24\25\5\6\4\2\25\5\3\2\2\2\26\27\7\5\2\2\27\34\5\b"+
"\5\2\30\31\7\6\2\2\31\33\5\b\5\2\32\30\3\2\2\2\33\36\3\2\2\2\34\32\3\2"+
"\2\2\34\35\3\2\2\2\35\37\3\2\2\2\36\34\3\2\2\2\37 \7\7\2\2 \7\3\2\2\2"+
"!$\7\b\2\2\"$\5\4\3\2#!\3\2\2\2#\"\3\2\2\2$\t\3\2\2\2\5\20\34#";
public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static {
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
}
}
}