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
Modifier 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, WCONS
Fields 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
ConstructorDescriptionASPParser
(InputStream stream) Constructor with InputStreamASPParser
(InputStream stream, String encoding) Constructor with InputStream and supplied encodingConstructorConstructor with generated Token Manager -
Method Summary
Modifier and TypeMethodDescriptionfinal ASTAggregate
Return Aggregatefinal ASTAggrElement
Return AggrElementfinal ASTAggrElementList
Return AggrElementListfinal String
Return AggrFuncfinal ASTAnswerSet
Constructorfinal ASTAriTerm
AriTerm()
final String
Arithop()
final String
Binop()
final ASTBodyList
Body()
Return BodyListfinal ASTBody
Body1()
Return Bodyfinal ASTBuiltInAtom
final ASTChoice
Choice()
Return Choicefinal ASTChoiceElement
Return ChoiceElementfinal ASTChoiceElementList
Return ChoiceElementListfinal ASTLiteral
final void
final void
Disable tracing.final ASTHeadElementsList
Return HeadElementsListfinal void
final void
final void
Enable tracing.final Token
final Token
getToken
(int index) final ASTHead
Head()
Return Headfinal void
final void
final ASTNAFLiteral
final ASTNAFLiteralList
final void
Number()
final ASTOpt
Optimize()
final ASTOptElement
final ASTOptElementList
final String
static AnswerSet
parseAnswerSet
(Reader reader) Parse parseAnswerSetstatic AnswerSet
parseAnswerSet
(String answerSet) Parse AnswerSetstatic Program
parseProgram
(Reader reader) parse the programstatic Program
parseProgram
(String program) Parse the programmstatic ASPRule
Parse the rulestatic ASPRule
Parse the rulefinal ASTProgram
Program()
Constructorfinal ASTQuery
Query()
Return Queryvoid
ReInit
(InputStream stream) void
ReInit
(InputStream stream, String encoding) void
void
final ASTRule
Return Rulefinal ASTRuleList
Return RuleListfinal void
String()
final ASTTerm
Term()
final ASTTermList
Terms()
final void
Variable()
final ASTWeight
-
Field Details
-
token_source
Generated Token Manager. -
token
Current token. -
jj_nt
Next token.
-
-
Constructor Details
-
ASPParser
-
ASPParser
Constructor with InputStream and supplied encoding- Parameters:
stream
- encodingencoding
- encoding
-
ASPParser
-
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
Return RuleList- Returns:
- RuleList
- Throws:
ParseException
- RuleList
-
Query
Return Query- Returns:
- Query
- Throws:
ParseException
- Query
-
Statement
Return Rule- Returns:
- Rule
- Throws:
ParseException
- Rule
-
Head
Return Head- Returns:
- Head
- Throws:
ParseException
- Head
-
Body
Return BodyList- Returns:
- BodyList
- Throws:
ParseException
- BodyList
-
Body1
Return Body- Returns:
- Body
- Throws:
ParseException
- Body
-
Disjunction
Return HeadElementsList- Returns:
- HeadElementsList
- Throws:
ParseException
- HeadElementsList
-
Choice
Return Choice- Returns:
- Choice
- Throws:
ParseException
- Choise
-
ChoiceElements
Return ChoiceElementList- Returns:
- ChoiceElementList
- Throws:
ParseException
- ChoiseElementList
-
ChoiceElement
Return ChoiceElement- Returns:
- ChoiceElement
- Throws:
ParseException
- ChoiceElement
-
Aggregate
Return Aggregate- Returns:
- Aggregate
- Throws:
ParseException
- Aggregate
-
AggregateElements
Return AggrElementList- Returns:
- AggrElementList
- Throws:
ParseException
- AggrElementList
-
AggregateElement
Return AggrElement- Returns:
- AggrElement
- Throws:
ParseException
- AggrElement
-
AggregateFunction
Return AggrFunc- Returns:
- AggrFunc
- Throws:
ParseException
- AggrFunc
-
MetaStatement
- Throws:
ParseException
- ClingoMeta
-
Optimize
- Returns:
- Opt
- Throws:
ParseException
- Opt
-
OptimizeElements
- Returns:
- OptElementList
- Throws:
ParseException
- OptElementList
-
OptimizeElement
- Returns:
- OptElement
- Throws:
ParseException
- OptElement
-
OptimizeFunction
- Returns:
- OptFunc
- Throws:
ParseException
- OptFunc
-
WeightAtLevel
- Returns:
- Weight
- Throws:
ParseException
- Weight
-
NAFLiterals
- Returns:
- NAFLiteralList
- Throws:
ParseException
- NAFLiteralList
-
NAFLiteral
- Returns:
- ASTNAFLiteral
- Throws:
ParseException
- ASTNAFLiteral
-
ClassicalLiteral
- Returns:
- ASTLiteral
- Throws:
ParseException
- ASTListeral
-
BuiltinAtom
- Returns:
- ASTBuiltInAtom
- Throws:
ParseException
- ASTBuiltInAtom
-
Binop
- Returns:
- Binop
- Throws:
ParseException
- Binop
-
Terms
- Returns:
- ASTTermList
- Throws:
ParseException
- ASTTermList
-
Term
- Returns:
- ASTTerm
- Throws:
ParseException
- ASTTerm
-
AriTerm
- Returns:
- Ariterm
- Throws:
ParseException
- Ariterm
-
Arithop
- Returns:
- Arithop
- Throws:
ParseException
- Arithop
-
Number
- Throws:
ParseException
- ParseException
-
Variable
- Throws:
ParseException
- ParseException
-
Identifier
- Throws:
ParseException
- Identifier
-
DlvIdentifier
- Throws:
ParseException
- DlvIdentifier
-
ClingoIdentifier
- Throws:
ParseException
- Parse Exception
-
DlvArithmeticPredicate
- Throws:
ParseException
- ParseException
-
String
- Throws:
ParseException
- ParseException
-
ReInit
- Parameters:
stream
- java.io.Input.Stream
-
ReInit
- Parameters:
stream
- java.io.InputStreamencoding
- java.io.InputStream
-
ReInit
- Parameters:
stream
- java.io.Reader
-
ReInit
- Parameters:
tm
- ReInit
-
getNextToken
- Returns:
- getNextToken
-
getToken
- Parameters:
index
- getToken- Returns:
- getToken
-
generateParseException
- Returns:
- generateParseException
-
enable_tracing
public final void enable_tracing()Enable tracing. -
disable_tracing
public final void disable_tracing()Disable tracing.
-