AbcDatalog includes a recursive-descent parser that converts Datalog source text into theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/HarvardPL/AbcDatalog/llms.txt
Use this file to discover all available pages before exploring further.
Clause-based AST described in the AST overview. The entry point is always a DatalogTokenizer wrapping a java.io.Reader, which is then passed to the static methods of DatalogParser. Parsing can throw DatalogParseException for any syntax error.
Datalog text conventions: identifiers consist of letters, digits, and underscores. An identifier that starts with an uppercase letter or _ is parsed as a variable; all others are parsed as constants or predicate symbols. The % character begins a single-line comment. Clauses end with . and queries end with ?.
DatalogTokenizer
DatalogTokenizer wraps a java.io.Reader and produces a stream of string tokens consumed by DatalogParser.
Constructor
Any
java.io.Reader — typically a StringReader for in-memory text or a FileReader / BufferedReader for files.DatalogTokenizer is stateful and advances linearly through the input. Once tokens have been consumed by a parser call, they cannot be re-read. Create a new tokenizer if you need to parse the same input again.Token-level methods
| Method | Description |
|---|---|
boolean hasNext() | Returns true if there is at least one token remaining in the stream. |
String next() | Consumes and returns the next token. Throws DatalogParseException at EOF. |
String peek() | Returns the next token without consuming it. Throws DatalogParseException at EOF. |
void consume(String s) | Consumes the exact string s from the stream, throwing DatalogParseException if it does not match. |
DatalogParser
DatalogParser is a non-instantiable utility class — all methods are static.
parseProgram
Set<Clause>. Each clause must be in the form head. or head :- body..
The token stream for the complete program text.
The full set of parsed clauses, including both facts and rules.
parseQuery
? from the token stream. For example, the text tc(X, Y)? parses to a PositiveAtom with predicate tc and variables X, Y.
The token stream positioned at the start of a query expression ending in
?.The parsed query atom.
parseClauseAsPositiveAtom
. from the token stream. This is convenient for reading individual ground facts from a token stream incrementally (e.g., facts arriving at runtime for a DatalogExecutor).
The token stream positioned at the start of an atom ending in
..The parsed atom.
parsePositiveAtom
. or ?. This lower-level method is useful when you need to read an atom embedded in a larger custom parsing context.
The token stream positioned at the start of an atom.
The parsed atom.
DatalogParseException
DatalogParseException is a checked exception thrown by all parser and tokenizer methods on any syntax error, unexpected token, or premature EOF. It extends Exception and provides four constructors covering the standard (message), (cause), (message, cause), and full (message, cause, enableSuppression, writableStackTrace) forms.
Code examples
Parse a program from a String
Parse a program from a file
Parse a query
Parse individual facts incrementally
This pattern is used byDatalogExecutor.addFactAsynchronously: