Package org.tweetyproject.lp.asp.parser
Class ASPParser
java.lang.Object
org.tweetyproject.lp.asp.parser.ASPParser
- All Implemented Interfaces:
ASPParserConstants,ASPParserTreeConstants
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
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
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.
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
-
Field Summary
FieldsModifier and TypeFieldDescriptionNext token.Current token.Generated Token Manager.Fields inherited from interface org.tweetyproject.lp.asp.parser.ASPParserConstants
AGGREGATE_COUNT, AGGREGATE_MAX, AGGREGATE_MIN, AGGREGATE_SUM, AGGREGATE_SUM_PLUS, AGGREGATE_TIMES, ANONYMOUS_VARIABLE, AT, CLINGO_ID, CLINGO_SHOW, COLON, COMMA, COMMENT, CONS, CURLY_CLOSE, CURLY_OPEN, DEFAULT, DIV, DLV_ARITHMETIC_ID, DLV_ID, DOT, EOF, EQUAL, GREATER, GREATER_OR_EQ, ID, LESS, LESS_OR_EQ, MAXIMIZE, META_CONST, META_MAXINT, META_OTHER, MINIMIZE, MINUS, MODULO, MULTI_LINE_COMMENT, NAF, NUMBER, OR, PAREN_CLOSE, PAREN_OPEN, PLUS, QUERY_MARK, SEMICOLON, SQUARE_CLOSE, SQUARE_OPEN, STRING, TIMES, tokenImage, UNEQUAL, VARIABLE, WCONSFields inherited from interface org.tweetyproject.lp.asp.parser.ASPParserTreeConstants
JJTAGGREGATE, JJTAGGRELEMENT, JJTAGGRELEMENTLIST, JJTAGGRFUNC, JJTANSWERSET, JJTARITERM, JJTARITHOP, JJTBINOP, JJTBODY, JJTBODYLIST, JJTBUILTINATOM, JJTCHOICE, JJTCHOICEELEMENT, JJTCHOICEELEMENTLIST, JJTCLINGOID, JJTCLINGOMETA, JJTDLVARITHMETICID, JJTDLVID, JJTHEAD, JJTHEADELEMENTSLIST, JJTID, JJTLITERAL, JJTNAFLITERAL, JJTNAFLITERALLIST, jjtNodeName, JJTNUMBER, JJTOPT, JJTOPTELEMENT, JJTOPTELEMENTLIST, JJTOPTFUNC, JJTPROGRAM, JJTQUERY, JJTRULE, JJTRULELIST, JJTSTRING, JJTTERM, JJTTERMLIST, JJTVAR, JJTWEIGHT -
Constructor Summary
ConstructorsConstructorDescriptionASPParser(InputStream stream) Constructor with InputStreamASPParser(InputStream stream, String encoding) Constructor with InputStream and supplied encodingConstructorConstructor with generated Token Manager -
Method Summary
Modifier and TypeMethodDescriptionfinal ASTAggregateParses an aggregate elementfinal ASTAggrElementParses a single aggregate elementfinal ASTAggrElementListParses a list of aggregate elementsfinal StringParses an aggregate function namefinal ASTAnswerSetConstructorfinal ASTAriTermAriTerm()Parses an arithmetic term expressionfinal StringArithop()Parses an arithmetic operatorfinal StringBinop()Parses a binary operatorfinal ASTBodyListBody()Parses the body of a rule as a list of atomsfinal ASTBodyBody1()Parses a single body atomfinal ASTBuiltInAtomParses a built-in atomfinal ASTChoiceChoice()Parses a choice elementfinal ASTChoiceElementParses a single choice elementfinal ASTChoiceElementListParses a list of choice elementsfinal ASTLiteralParses a classical literalfinal voidParses a Clingo-specific identifierfinal voidDisable tracing.final ASTHeadElementsListParses a disjunction of head elementsfinal voidParses a DLV arithmetic predicatefinal voidParses a DLV-specific identifierfinal voidEnable tracing.Generates a parse exceptionfinal TokenGets the next tokenfinal TokengetToken(int index) Gets a token at the specified indexfinal ASTHeadHead()Parses the head of a rulefinal voidParses an identifierfinal voidParses a clingo meta statement.final ASTNAFLiteralParses a NAF literal (negation as failure)final ASTNAFLiteralListParses a list of NAF literalsfinal voidNumber()Parses a numeric valuefinal ASTOptOptimize()Parses a clingo meta statementfinal ASTOptElementParses a single optimization elementfinal ASTOptElementListParses a list of optimization elementsfinal StringParses an optimization functionstatic AnswerSetparseAnswerSet(Reader reader) Parse parseAnswerSetstatic AnswerSetparseAnswerSet(String answerSet) Parse AnswerSetstatic ProgramparseProgram(Reader reader) parse the programstatic ProgramparseProgram(String program) Parse the programmstatic ASPRuleParse the rulestatic ASPRuleParse the rulefinal ASTProgramProgram()Constructorfinal ASTQueryQuery()Parses a queryvoidReInit(InputStream stream) Reinitializes the parser with an input stream.voidReInit(InputStream stream, String encoding) Reinitialize the parser with an input stream and encodingvoidReinitialize the parser with a readervoidReinitialize with token managerfinal ASTRuleParses a single statementfinal ASTRuleListParses a list of statementsfinal voidString()Parses a string literalfinal ASTTermTerm()Parses a single termfinal ASTTermListTerms()Parses a list of termsfinal voidVariable()Parses a variablefinal ASTWeightParses a weight at level expression
-
Field Details
-
token_source
Generated Token Manager. -
token
Current token. -
jj_nt
Next token.
-
-
Constructor Details
-
ASPParser
Constructor with InputStream- Parameters:
stream- InputStream
-
ASPParser
Constructor with InputStream and supplied encoding- Parameters:
stream- encodingencoding- encoding
-
ASPParser
Constructor- Parameters:
stream- java.io.Reader
-
ASPParser
Constructor with generated Token Manager- Parameters:
tm- ASPParser TokenManager
-
-
Method Details
-
parseProgram
Parse the programm- Parameters:
program- StringReader- Returns:
- StringReader
- Throws:
ParseException- StringReader
-
parseProgram
parse the program- Parameters:
reader- Reader- Returns:
- Reader
- Throws:
ParseException- Reader
-
parseRule
Parse the rule- Parameters:
rule- ASPRule- Returns:
- ASPRule
- Throws:
ParseException- ASPRule
-
parseRule
Parse the rule- Parameters:
reader- ASPRule- Returns:
- ASPRule
- Throws:
ParseException- ASPRule
-
parseAnswerSet
Parse AnswerSet- Parameters:
answerSet- AnswerSet- Returns:
- AnswerSet
- Throws:
ParseException- AnswerSet
-
parseAnswerSet
Parse parseAnswerSet- Parameters:
reader- AnswerSet- Returns:
- AnswerSet
- Throws:
ParseException- AnswerSet
-
AnswerSet
Constructor- Returns:
- AnswerSet
- Throws:
ParseException- AnwerSet
-
Program
Constructor- Returns:
- Program
- Throws:
ParseException- Program
-
Statements
Parses a list of statements- Returns:
- RuleList
- Throws:
ParseException- RuleList
-
Query
Parses a query- Returns:
- Query
- Throws:
ParseException- Query
-
Statement
Parses a single statement- Returns:
- Rule
- Throws:
ParseException- Rule
-
Head
Parses the head of a rule- Returns:
- Head
- Throws:
ParseException- Head
-
Body
Parses the body of a rule as a list of atoms- Returns:
- BodyList
- Throws:
ParseException- BodyList
-
Body1
Parses a single body atom- Returns:
- Body
- Throws:
ParseException- Body
-
Disjunction
Parses a disjunction of head elements- Returns:
- HeadElementsList
- Throws:
ParseException- HeadElementsList
-
Choice
Parses a choice element- Returns:
- Choice
- Throws:
ParseException- Choise
-
ChoiceElements
Parses a list of choice elements- Returns:
- ChoiceElementList
- Throws:
ParseException- ChoiseElementList
-
ChoiceElement
Parses a single choice element- Returns:
- ChoiceElement
- Throws:
ParseException- ChoiceElement
-
Aggregate
Parses an aggregate element- Returns:
- Aggregate
- Throws:
ParseException- Aggregate
-
AggregateElements
Parses a list of aggregate elements- Returns:
- AggrElementList
- Throws:
ParseException- AggrElementList
-
AggregateElement
Parses a single aggregate element- Returns:
- AggrElement
- Throws:
ParseException- AggrElement
-
AggregateFunction
Parses an aggregate function name- Returns:
- AggrFunc
- Throws:
ParseException- AggrFunc
-
MetaStatement
Parses a clingo meta statement.- Throws:
ParseException- if parsing fails
-
Optimize
Parses a clingo meta statement- Returns:
- Opt
- Throws:
ParseException- Opt
-
OptimizeElements
Parses a list of optimization elements- Returns:
- OptElementList
- Throws:
ParseException- OptElementList
-
OptimizeElement
Parses a single optimization element- Returns:
- OptElement
- Throws:
ParseException- OptElement
-
OptimizeFunction
Parses an optimization function- Returns:
- OptFunc
- Throws:
ParseException- OptFunc
-
WeightAtLevel
Parses a weight at level expression- Returns:
- Weight
- Throws:
ParseException- Weight
-
NAFLiterals
Parses a list of NAF literals- Returns:
- NAFLiteralList
- Throws:
ParseException- NAFLiteralList
-
NAFLiteral
Parses a NAF literal (negation as failure)- Returns:
- ASTNAFLiteral
- Throws:
ParseException- ASTNAFLiteral
-
ClassicalLiteral
Parses a classical literal- Returns:
- ASTLiteral
- Throws:
ParseException- ASTListeral
-
BuiltinAtom
Parses a built-in atom- Returns:
- ASTBuiltInAtom
- Throws:
ParseException- ASTBuiltInAtom
-
Binop
Parses a binary operator- Returns:
- Binop
- Throws:
ParseException- Binop
-
Terms
Parses a list of terms- Returns:
- ASTTermList
- Throws:
ParseException- ASTTermList
-
Term
Parses a single term- Returns:
- ASTTerm
- Throws:
ParseException- ASTTerm
-
AriTerm
Parses an arithmetic term expression- Returns:
- Ariterm
- Throws:
ParseException- Ariterm
-
Arithop
Parses an arithmetic operator- Returns:
- Arithop
- Throws:
ParseException- Arithop
-
Number
Parses a numeric value- Throws:
ParseException- ParseException
-
Variable
Parses a variable- Throws:
ParseException- ParseException
-
Identifier
Parses an identifier- Throws:
ParseException- Identifier
-
DlvIdentifier
Parses a DLV-specific identifier- Throws:
ParseException- DlvIdentifier
-
ClingoIdentifier
Parses a Clingo-specific identifier- Throws:
ParseException- Parse Exception
-
DlvArithmeticPredicate
Parses a DLV arithmetic predicate- Throws:
ParseException- ParseException
-
String
Parses a string literal- Throws:
ParseException- ParseException
-
ReInit
Reinitializes the parser with an input stream.- Parameters:
stream- input stream to parse
-
ReInit
Reinitialize the parser with an input stream and encoding- Parameters:
stream- java.io.InputStreamencoding- java.io.InputStream
-
ReInit
Reinitialize the parser with a reader- Parameters:
stream- java.io.Reader
-
ReInit
Reinitialize with token manager- Parameters:
tm- ReInit
-
getNextToken
Gets the next token- Returns:
- getNextToken
-
getToken
Gets a token at the specified index- Parameters:
index- getToken- Returns:
- getToken
-
generateParseException
Generates a parse exception- Returns:
- generateParseException
-
enable_tracing
public final void enable_tracing()Enable tracing. -
disable_tracing
public final void disable_tracing()Disable tracing.
-