Class ASPParser

java.lang.Object
org.tweetyproject.lp.asp.parser.ASPParser
All Implemented Interfaces:
ASPParserConstants, ASPParserTreeConstants

public class ASPParser extends Object implements ASPParserTreeConstants, ASPParserConstants
Parser for ASP programs in the ASP-Core-2 syntax (https://www.mat.unical.it/aspcomp2013/files/ASP-CORE-2.03c.pdf). and DLV input syntaxes and provides additional methods for parsing answer sets given by the DLV and Clingo solvers.
See ASPParserExample for examples on how to use this parser.

Note for developers: This parser is generated by JavaCC. To make changes to the grammar, edit "ASPParser.jjt". Then invoke the JJTree preprocessor by calling "jjtree ASPParser.jjt" on the command line (this generates the .jj file). Then run "javacc ASPParser.jj" to generate the parser (i.e. the ASPParser class and the various AST classes). In many cases you will also have to edit the InstantiateVisitor class which defines how the AST nodes are parsed into TweetyProject's syntax classes.
See https://javacc.github.io/javacc/ for more information on JavaCC.

The EBNF grammar for this parser is given as follows:
PROGRAM ::== (STATEMENT)* (QUERY)?
QUERY ::== LITERAL "?"
STATEMENT ::== ":-" BODY_ELEMENTS "." | HEAD (":-" BODY_ELEMENTS)? "." | ":~" | BODY_ELEMENTS "." | OPTIMIZE "." | META
HEAD ::== (LITERAL ("|" | "v"))* LITERAL | CHOICE | AGGREGATE
BODY_ELEMENTS ::== (BODY_ELEMENTS ",")? BODY_ELEMENT
BODY_ELEMENT ::== NAF_LITERAL | ("not")? AGGREGATE
CHOICE ::== (TERM BINOP)? "{" CHOICE_ELEMENTS "}" (BINOP TERM)?
CHOICE_ELEMENTS ::== (CHOICE_ELEMENTS ";")? CHOICE_ELEMENT
CHOICE_ELEMENT ::== LITERAL (":" NAF_LITERALS)?
AGGREGATE ::== (TERM BINOP)? AGGREGATE_FUNCTION "{" AGGREGATE_ELEMENTS "}" (BINOP TERM)?
AGGREGATE_ELEMENTS ::== (AGGREGATE_ELEMENTS ";")? AGGREGATE_ELEMENT
AGGREGATE_ELEMENT ::== (Terms)? (":" NAF_LITERALS)?
AGGREGATE_FUNCTION ::== "#max" | "#min" | "#count" | "#sum" | "#sum+" | "#times"
OPTIMIZE ::== OPTIMIZE_FUNCTION "{" (OPTIMIZE_ELEMENTS)? "}"
OPTIMIZE_ELEMENTS ::== (OPTIMIZE_ELEMENTS ";")? OPTIMIZE_ELEMENT
OPTIMIZE_ELEMENT ::== WEIGHT_AT_LEVEL (":" NAF_LITERALS)?
OPTIMIZE_FUNCTION ::== "#maximize" | "#maximise" | "#minimize"| "#minimise"
META ::== "#show " ID "/" NUMBER | "#maxint" "=" "NUMBER | "#const" ID "=" ID WEIGHT_AT_LEVEL ::== TERM ("@" TERM)? ("," TERMS)?
NAF_LITERALS ::== (NAF_LITERALS ",")? NAF_LITERAL
NAF_LITERAL ::== ("not")? LITERAL | BUILTIN_ATOM
LITERAL ::== ("-")? (ID | DLV_ID | CLINGO_ID) ("(" Terms ")")?
BUILTIN_ATOM ::== TERM BINOP TERM | DLV_ARITHMETIC_ID "(" Terms ")" | ARITHOP "(" TERM "," TERM "," TERM ")"
BINOP ::== "=" | "!=" |"<>" | "<" | ">" | "<=" | ">="
TERMS ::== (TERMS ",")? TERM
TERM ::== ID ("(" TERMS ")")? | NUMBER | STRING | VARIABLE | "_" | "(" TERM ")" | "-" TERM | TERM ARITHOP TERM
ARITHOP ::== "+" | "-" | "*" | "/" | "\"
DLV_ARITHMETIC_ID ::== "#succ" | "#int" | "#prec" | "#mod"
DLV_ID ::== "#rand"|"#int"|"#absdiff"|"#append"|"#delnth"|"#flatten"|"#getnth"|"#head"|"#insLast"|"#insnth"|"#last"|"#length"|"#member"|"#reverse"|"#subList"|"#tail"
CLINGO_ID ::== "#true"|"#false"

where ID is a sequence of letters, numbers, and "_" that starts with a lowercase letter, VARIABLE is a sequence of letters, numbers, and "_" that starts with an uppercase letter, STRING is a sequence of arbitrary characters surrounded by quotation marks ("...") and NUMBER is a sequence of numbers.
Single-line comments (starting with "%") and multi-line-comments (starting with "%*", ending with "*%") are ignored by the parser.

The grammar given above is mostly equivalent to the ASP-Core-2 grammar. The main differences are that some variations in symbols are supported (such as DLV using "v" instead of "|") and that special constructs from Clingo and DLV are allowed:
- #show statements (Clingo): The whitelisted atoms are added to the program's outputPredicateWhitelist and can be used by ClingoSolver if enabled with ClingoSolver.toggleOutputWhitelist(true)
- #succ, #prec, #mod predicates, +(X,Y,Z), -(X,Y,Z), *(X,Y,Z), /(X,Y,Z) (DLV): They are converted into equivalent built-in atoms (clingo syntax)
- #maxint statements: They are used in place of the integerMaximum parameter when calling solvers
- #const constants: They are added to the program's additionalOptions and are automatically used by ClingoSolver and DLVSolver
- other arithmetic predicates (such as #int) and list predicates (such as #append) from DLV: They are parsed as special predicates that can only be used with the DLV solver but not with Clingo
Other Clingo/DLV meta-statements (such as #include, #external, #program, #script) are ignored.
Author:
Anna Gessler