Class DelpParser

All Implemented Interfaces:
DelpParserConstants

public class DelpParser extends Parser<DefeasibleLogicProgram,Formula> implements DelpParserConstants
This class implements a parser for defeasible logic programs. The BNF for defeasible logic program files is given by (start symbol is THEORY)
 THEORY                 ::== (EXPRESSION)+
 EXPRESSION             ::== FACT | STRICTRULE | DEFEASIBLERULE
 FACT                   ::== LITERAL + "."
 STRICTRULE             ::== LITERAL + "<-" + RULEBODY + "."
 DEFEASIBLERULE         ::== LITERAL + "-<" + RULEBODY + "."
 RULEBODY               ::== LITERAL | LITERAL + "," + RULEBODY
 LITERAL                ::== "~" + ATOM | ATOM
 ATOM                   ::== PREDICATE | PREDICATE + "(" + TERMLIST + ")"
 TERMLIST               ::== TERM | TERM + "," + TERMLIST
 TERM                   ::== VARIABLE | CONSTANT | QUOTED_STRING

 PREDICATE is a sequence of symbols from {a,...,z,A,...,Z,0,...,9,_,-} with a letter at the beginning.
 VARIABLE is a sequence of symbols from {a,...,z,A,...,Z,0,...,9,_,-} with an uppercase letter at the beginning.
 CONSTANT is  a sequence of symbols from {a,...,z,A,...,Z,0,...,9,_,-} with an lowercase letter at the beginning.
 QUOTED_STRING is all characters between double quotes.
 
  • Field Details

    • token_source

      public DelpParserTokenManager token_source
      Generated Token Manager.
    • token

      public Token token
      Current token.
    • jj_nt

      public Token jj_nt
      Next token.
  • Constructor Details

    • DelpParser

      public DelpParser()
      Constructs a new `DelpParser` instance with an empty input reader.
    • DelpParser

      public DelpParser(InputStream stream)
      Creates a new parser instance with the specified input stream.
      Parameters:
      stream - The input stream to be used by the parser.
    • DelpParser

      public DelpParser(InputStream stream, String encoding)
      Creates a new parser instance with the specified input stream and character encoding.
      Parameters:
      stream - The input stream to be used by the parser.
      encoding - The character encoding of the input stream. If null, the default encoding is used.
    • DelpParser

      public DelpParser(Reader stream)
      Creates a new parser instance with the specified reader.
      Parameters:
      stream - The reader to be used by the parser.
    • DelpParser

      public DelpParser(DelpParserTokenManager tm)
      Creates a new parser instance with the specified token manager.
      Parameters:
      tm - The token manager to be used by the parser.
  • Method Details

    • parseBeliefBase

      public DefeasibleLogicProgram parseBeliefBase(Reader reader) throws ParserException
      Parses a defeasible logic program from the given reader.

      This method initializes the parser with the provided reader and parses the input to construct a DefeasibleLogicProgram. It uses the current logical signature to guide the parsing process.

      Specified by:
      parseBeliefBase in class Parser<DefeasibleLogicProgram,Formula>
      Parameters:
      reader - The reader from which the defeasible logic program is read.
      Returns:
      A DefeasibleLogicProgram constructed from the input.
      Throws:
      ParserException - If an error occurs during parsing.
    • parseFormula

      public Formula parseFormula(Reader reader) throws ParserException
      A formula here is a Literal, that is an Atom or a negated Atom. The class DelpQuery encapsulates the following. The Atom is either a DeLP predicate (a predicate with arity > 0) or a DeLP constant or variable (a predicate with arity == 0). In the case of a real predicate, test all arguments whether they are DeLP variables (= begin with upper case letter) or DeLP constants. All predicates and constants need to be present in the current signature to parse.
      Specified by:
      parseFormula in class Parser<DefeasibleLogicProgram,Formula>
      Parameters:
      reader - the reader to parse from
      Returns:
      a Formula, which is always a DelpQuery in this implementation, that has been successfully parsed with the current signature in mind
      Throws:
      ParserException - if the reader cannot be successfully parsed into a formula
    • getSignature

      public FolSignature getSignature()
      Return the signature
      Returns:
      the signature
    • Theory

      public final DefeasibleLogicProgram Theory(FolSignature signature) throws ParseException
      Parses a defeasible logic program from a given signature.
      Parameters:
      signature - The signature that defines the vocabulary of the logic program.
      Returns:
      A DefeasibleLogicProgram instance representing the parsed program.
      Throws:
      ParseException - If an error occurs during parsing.
    • Expression

      public final void Expression(DefeasibleLogicProgram delp, FolSignature signature) throws ParseException
      Parses an expression in the defeasible logic program.
      Parameters:
      delp - The defeasible logic program to which parsed components are added.
      signature - The signature that defines the vocabulary of the logic program.
      Throws:
      ParseException - If an error occurs during parsing.
    • Formula

      public final FolFormula Formula(FolSignature signature) throws ParseException
      Parses a formula from the given signature.
      Parameters:
      signature - The signature that defines the vocabulary of the logic program.
      Returns:
      A FolFormula representing the parsed formula.
      Throws:
      ParseException - If an error occurs during parsing.
    • Literal

      public final FolFormula Literal(DefeasibleLogicProgram delp, FolSignature signature) throws ParseException
      Parses a literal, which could be an atom or its negation.
      Parameters:
      delp - The defeasible logic program to which the literal might be added.
      signature - The signature that defines the vocabulary of the logic program.
      Returns:
      A FolFormula representing the parsed literal.
      Throws:
      ParseException - If an error occurs during parsing.
    • Atom

      public final FolAtom Atom(DefeasibleLogicProgram delp, FolSignature signature) throws ParseException
      Parses an atom (predicate with optional terms) from the input.

      This method constructs a FolAtom by parsing a predicate name followed by a sequence of terms, if present. The predicate is added to the signature if it is not already present, and its arity is checked for correctness. If the arity is incorrect, a ParseException is thrown.

      Parameters:
      delp - The defeasible logic program to which the predicate might be added.
      signature - The logical signature to which the predicate might be added.
      Returns:
      A FolAtom constructed from the parsed predicate and terms.
      Throws:
      ParseException - If an error occurs during parsing, such as an incorrect predicate arity.
    • Term

      public final Term Term(DefeasibleLogicProgram delp, FolSignature signature) throws ParseException
      Parses a term (either a variable or a constant) from the input.

      This method constructs a Term based on the input token. It handles variables (identified by uppercase names) and constants (both plain and quoted). If a quoted term is encountered, the quotes are removed before creating the constant.

      Parameters:
      delp - The defeasible logic program used for creating constants (if needed).
      signature - The logical signature used for creating constants (if needed).
      Returns:
      A Term constructed from the parsed token.
      Throws:
      ParseException - If an error occurs during parsing.
    • ReInit

      public void ReInit(InputStream stream)
      Reinitializes the parser with a new input stream.
      Parameters:
      stream - The new input stream to be used by the parser.
    • ReInit

      public void ReInit(InputStream stream, String encoding)
      Reinitializes the parser with a new input stream and character encoding.
      Parameters:
      stream - The new input stream to be used by the parser.
      encoding - The character encoding of the new input stream. If null, the default encoding is used.
    • ReInit

      public void ReInit(Reader stream)
      Reinitializes the parser with a new reader.
      Parameters:
      stream - The new reader to be used by the parser.
    • ReInit

      public void ReInit(DelpParserTokenManager tm)
      Reinitializes the parser with a new token manager.
      Parameters:
      tm - The new token manager to be used by the parser.
    • getNextToken

      public final Token getNextToken()
      Retrieves the next token in the input stream.

      This method advances to the next token by either using the previously cached next token or fetching a new token from the token source if there is no cached next token. The token stream is updated and the token index and generation are incremented accordingly.

      Returns:
      The next Token in the input stream.
    • getToken

      public final Token getToken(int index)
      Retrieves a specific token from the input stream by its index.

      This method traverses the token stream to reach the token at the specified index. If the token at the given index is not already available, it fetches the necessary tokens from the token source until the desired token is reached.

      Parameters:
      index - The zero-based index of the token to retrieve.
      Returns:
      The Token at the specified index in the input stream.
    • generateParseException

      public ParseException generateParseException()
      Generate ParseException.
      Returns:
      the ParseException
    • enable_tracing

      public final void enable_tracing()
      Enable tracing.
    • disable_tracing

      public final void disable_tracing()
      Disable tracing.