1 from .DLV2Lexer
import DLV2Lexer
2 from .DLV2Parser
import DLV2Parser
3 from .DLV2ParserVisitor
import DLV2ParserVisitor
4 from antlr4
import PredictionMode
5 from antlr4.CommonTokenStream
import CommonTokenStream
6 from antlr4.error.ErrorListener
import ConsoleErrorListener
7 from antlr4.error.Errors
import RecognitionException
8 from antlr4.error.ErrorStrategy
import BailErrorStrategy, DefaultErrorStrategy
9 from antlr4.InputStream
import InputStream
13 def __init__(self, answerSets):
17 def visitAnswer_set(self, ctx):
20 if ctx.cost()
is not None and not ctx.cost().isEmpty():
22 firstCost = ctx.cost().COST_LABEL(
23 ).getText().split(
' ')[1].split(
'@')
25 self.
_costs[firstCost[1]] = firstCost[0]
27 if self.
_costs is not None:
28 for level, cost
in self.
_costs.items():
31 return self.visitChildren(ctx)
33 def visitLevel(self, ctx):
34 level = ctx.INTEGER(1).getText()
35 cost = ctx.INTEGER(0).getText()
40 def visitPredicate_atom(self, ctx):
44 def parse(answerSets, dlv2Output, two_stageParsing):
45 tokens = CommonTokenStream(
DLV2Lexer(InputStream(dlv2Output)))
49 if not two_stageParsing:
50 visitor.visit(parser.output())
54 parser._interp.predictionMode = PredictionMode.SLL
55 parser.removeErrorListeners()
56 parser._errHandler = BailErrorStrategy()
59 visitor.visit(parser.output())
60 except RuntimeError
as exception:
61 if isinstance(exception, RecognitionException):
63 parser.addErrorListener(ConsoleErrorListener.INSTANCE)
64 parser._errHandler = DefaultErrorStrategy()
65 parser._interp.predictionMode = PredictionMode.LL
66 visitor.visit(parser.output())