EmbASP-Python
dlv_parser_visitor_implementation.py
1 from .DLVLexer import DLVLexer
2 from .DLVParser import DLVParser
3 from .DLVParserVisitor import DLVParserVisitor
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
10 
11 
13  def __init__(self, answerSets):
14  self._answerSets = answerSets
15 
16  def visitSimpleModel(self, ctx):
17  self._answerSets.add_answer_set()
18 
19  return self.visitChildren(ctx)
20 
21  def visitWeightedModel(self, ctx):
22  self._answerSets.add_answer_set()
23 
24  return self.visitChildren(ctx)
25 
26  def visitWitness(self, ctx):
27  self._answerSets.add_answer_set()
28 
29  return self.visitChildren(ctx)
30 
31  def visitCost_level(self, ctx):
32  self._answerSets.store_cost(
33  ctx.INTEGER_CONSTANT(1).getText(),
34  ctx.INTEGER_CONSTANT(0).getText())
35 
36  def visitPredicate(self, ctx):
37  self._answerSets.store_atom(ctx.getText())
38 
39  @staticmethod
40  def parse(answerSets, dlvOutput, two_stageParsing):
41  tokens = CommonTokenStream(DLVLexer(InputStream(dlvOutput)))
42  parser = DLVParser(tokens)
43  visitor = DLVParserVisitorImplementation(answerSets)
44 
45  if not two_stageParsing:
46  visitor.visit(parser.output())
47 
48  return
49 
50  parser._interp.predictionMode = PredictionMode.SLL
51  parser.removeErrorListeners()
52  parser._errHandler = BailErrorStrategy()
53 
54  try:
55  visitor.visit(parser.output())
56  except RuntimeError as exception:
57  if isinstance(exception, RecognitionException):
58  tokens.seek(0)
59  parser.addErrorListener(ConsoleErrorListener.INSTANCE)
60  parser._errHandler = DefaultErrorStrategy()
61  parser._interp.predictionMode = PredictionMode.LL
62  visitor.visit(parser.output())
parsers.asp.dlv.DLVParser.DLVParser
Definition: DLVParser.py:53
parsers.asp.dlv.DLVParserVisitor.DLVParserVisitor
Definition: DLVParserVisitor.py:6
parsers.asp.dlv.dlv_parser_visitor_implementation.DLVParserVisitorImplementation
Definition: dlv_parser_visitor_implementation.py:12
parsers.asp.dlv.DLVLexer.DLVLexer
Definition: DLVLexer.py:99
parsers.asp.dlv.dlv_parser_visitor_implementation.DLVParserVisitorImplementation._answerSets
_answerSets
Definition: dlv_parser_visitor_implementation.py:14