Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## lp.parse
Class LpParser

    java.lang.Object
      lp.parse.LpParser

All Implemented Interfaces:
  Closeable

Direct Known Subclasses:
  DlpGrounder.DlpDetagger, DlpParser, EvolpParser

    public class LpParser

extends Object
implements Closeable

Processes textual input with a logic program or logic programming constructs and produces `LpRule`, `LpLiteral` and `LpTerm` objects. A `LpLexer` is used to tokenize the input first. The grammar according to which the constructs are parsed follows:

    Rule ---> OrdinaryRule | Constraint
    OrdinaryRule ---> Literal (RULE_ARROW (Literal (COMMA Literal)*)?)? DOT
    Constraint ---> RULE_ARROW Literal (COMMA Literal)* DOT
    Literal ---> 'not'? Atom
    Atom ---> PredicateName Arguments?
    PredicateName ---> LOWERCASE_WORD
    Arguments ---> LEFT_PAREN Term (COMMA Term)* RIGHT_PAREN
    Term ---> Constant | Variable | CompoundTerm
    Constant ---> LOWERCASE_WORD
    Variable ---> UPPERCASE_WORD
    CompoundTerm ---> FunctionName Arguments
    FunctionName ---> LOWERCASE_WORD

In this grammar

- Rule, OrdinaryRule, Constraint, Literal, Atom, PredicateName, Arguments, Term, Constant, Variable, CompoundTerm and FunctionName are non-terminals corresponding to the structure of the input
- DOT, RULE_ARROW, COMMA, LEFT_PAREN, RIGHT_PAREN, LOWERCASE_WORD and UPPERCASE_WORD are tokens as defined in `LpTokenType`
- 'not' is a LOWERCASE_WORD with a corresponding lexem "not"

Version:
  0.2.8
Author:
  Martin Slota
See Also:
  `LpLexer`, `LpRule`, `parseRule()`, `parseLiteral()`, `parseTerm()`

Field Summary
`private LpLexer` | `lexer` Container for the underlying lexer instance, through which the input is parsed.

Constructor Summary
`LpParser()` Creates a new `LpParser` instance that uses a new `LpLexer` instance to tokenize the character input before parsing.
`LpParser(LpLexer lexer)` Creates a new `LpParser` instance that uses `lexer` to tokenize the character input before parsing.

Method Summary
`void` | `close()` Closes the underlying lexer.
`protected void` | `expect(LpTokenType token)` Compares the current token (read through `LpLexer.getTokenType()` with an expected token.
`protected LpLexer` | `getLexer()` An accessor method for `lexer`.
`boolean` | `hasMoreTokens()` Returns `true` if there are more tokens on the input and `false` if there are no more tokens or if `close()` has already been called.
`protected void` | `match(LpTokenType token)` Compares the current token (read through `LpLexer.getTokenType()` with an expected token and reads a new token.
`protected void` | `nextToken()` Asks for the next token from the underlying `LpLexer`.
`LogicProgram` | `parseAllRules()` Parses the whole input as logic programming rules and returns them in a new `LogicProgram` object.
`LogicProgram` | `parseAllRules(LogicProgram program)` Parses the whole input as logic programming rules and adds them to the given `LogicProgram` object.
`protected List<LpTerm>` | `parseArguments()` Corresponds to the non-terminal Arguments from the class description.
`LpAtom` | `parseAtom()` Corresponds to the non-terminal Atom from the class description.
`LpLiteral` | `parseLiteral()` Corresponds to the non-terminal Literal from the class description.
`protected LpRule` | `parseOnlyRule()` Parses the next rule on input but doesn't require the leading DOT.
`LpRule` | `parseRule()` Corresponds to the non-terminal Rule from the class description.
`protected Set<LpLiteral>` | `parseRuleBody(boolean constraint)` Parses a rule's body and returns its object model—a list of `LpLiteral` objects.
`LpTerm` | `parseTerm()` Corresponds to the non-terminal Term from the class description.
`void` | `setInput(File input)` Sets the character input of this `LpParser` to the contents of the given file.
`void` | `setInput(CharSequence input)` Sets the character input of this `LpParser` to the given `CharSequence`.
`void` | `setInput(Reader input)` Sets the character input of this `LpParser` to the given `Reader`.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### lexer

    private final LpLexer lexer

  Container for the underlying lexer instance, through which the input is parsed.

Constructor Detail

### LpParser

    public LpParser()

  Creates a new `LpParser` instance that uses a new `LpLexer` instance to tokenize the character input before parsing.

### LpParser

    public LpParser(LpLexer lexer)

  Creates a new `LpParser` instance that uses `lexer` to tokenize the character input before parsing.

  Parameters:
    `lexer` - the `LpLexer` instance used to tokenize the character input before parsing

Method Detail

### setInput

    public void setInput(CharSequence input)

  Sets the character input of this `LpParser` to the given `CharSequence`.

  Parameters:
    `input` - the character sequence to be parsed
  Throws:
    `IllegalArgumentException` - if `input` is `null`
  See Also:
    `LpLexer.setInput(CharSequence)`

### setInput

    public void setInput(File input)

  Sets the character input of this `LpParser` to the contents of the given file. The default system character encoding is used to read the file.

  Parameters:
    `input` - the file to be parsed
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O exception occurs while opening or reading the file
    `IllegalArgumentException` - if `input` is `null`
  See Also:
    `LpLexer.setInput(File)`

### setInput

    public void setInput(Reader input)

  Sets the character input of this `LpParser` to the given `Reader`.

  Parameters:
    `input` - the input reader
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O exception occurs while reading from the `Reader`
    `IllegalArgumentException` - if `input` is `null`
  See Also:
    `LpLexer.setInput(Reader)`

### close

    public void close()

  Closes the underlying lexer. If `setInput(CharSequence)` or `setInput(File)` was used to set the current character source, this method should be called when parsing is finished. In other cases it is up to the programmer whether she will use this method or take care of closing the input reader in some other way.

  Specified by:
    `close` in interface `Closeable`
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O exception occurs while closing the underlying lexer
  See Also:
    `LpLexer.close()`

### hasMoreTokens

    public boolean hasMoreTokens()

  Returns `true` if there are more tokens on the input and `false` if there are no more tokens or if `close()` has already been called.

  Returns:
    as specified above

### parseAllRules

    public LogicProgram parseAllRules()

  Parses the whole input as logic programming rules and returns them in a new `LogicProgram` object.

  Returns:
    a `LogicProgram` instance containing the parsed rules
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the expression (Rule* EOF) (Rule is a non-terminal from the grammar in class description)
  See Also:
    `parseRule()`

### parseAllRules

    public LogicProgram parseAllRules(LogicProgram program)

  Parses the whole input as logic programming rules and adds them to the given `LogicProgram` object.

  Returns:
    a `LogicProgram` instance containing the parsed rules
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the expression (Rule* EOF) (Rule is a non-terminal from the grammar in class description)
  See Also:
    `parseRule()`

### parseRule

    public LpRule parseRule()

  Corresponds to the non-terminal Rule from the class description. Parses the next rule in the input and returns a `LpRule` instance—an object model of the parsed rule.

  Returns:
    the parsed rule
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from class description

### parseOnlyRule

    protected LpRule parseOnlyRule()

  Parses the next rule on input but doesn't require the leading DOT. For internal use by inheriting classes.

  Returns:
    the parsed rule
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from class description

### parseRuleBody

    protected Set<LpLiteral> parseRuleBody(boolean constraint)

  Parses a rule's body and returns its object model—a list of `LpLiteral` objects. If `constraint` is false, the input must match the expression

      (RULE_ARROW (Literal (COMMA Literal)*)?)?

  If `constraint` is `true`, the input must match the expression

      (RULE_ARROW Literal (COMMA Literal)*)?

  Parameters:
    `constraint` - if `true`, at least one literal is required after the arrow (in case there is one)
  Returns:
    the parsed body of a rule as a list of `LpLiteral` objects
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from class description

### parseLiteral

    public LpLiteral parseLiteral()

  Corresponds to the non-terminal Literal from the class description. Parses the next literal in the input and returns a `LpLiteral` instance—an object model of the parsed literal.

  Returns:
    the parsed literal
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) n case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from class description

### parseAtom

    public LpAtom parseAtom()

  Corresponds to the non-terminal Atom from the class description. Parses the next atom on the input and returns a `LpAtom` instance—an object model of the parsed atom.

  Returns:
    the parsed atom
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) n case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from class description

### parseTerm

    public LpTerm parseTerm()

  Corresponds to the non-terminal Term from the class description. Parses the next term in the input and returns an instance of a class implementing the `LpTerm` interface—an object model of the parsed term.

  Returns:
    the parsed term
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from class description

### parseArguments

    protected List<LpTerm> parseArguments()

  Corresponds to the non-terminal Arguments from the class description. Parses a comma separated list of terms enclosed in parenthesis.

  Returns:
    the parsed argument list
  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O error occurs while tokenizing the input
    `LpParserException` - if the input doesn't match the grammar from the class description

### match

    protected void match(LpTokenType token)

  Compares the current token (read through `LpLexer.getTokenType()` with an expected token and reads a new token. Throws a `LpParserException` if the tokens differ. Has the same effects as calling

      expect(token);
      readNewLA();

  Parameters:
    `token` - the expected token
  Throws:
    `LpParserException` - in case the current token is not as expected
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O exception occurs while identifying the next token

### expect

    protected void expect(LpTokenType token)

  Compares the current token (read through `LpLexer.getTokenType()` with an expected token. Throws a `LpParserException` if the tokens differ.

  Parameters:
    `token` - the expected token
  Throws:
    `LpParserException` - in case the lookahead token is not as expected

### nextToken

    protected final void nextToken()

  Asks for the next token from the underlying `LpLexer`.

  Throws:
    `IOException` - (wrapped in an `ExceptionAdapter`) in case an I/O exception occurs while identifying the next token
  See Also:
    `LpLexer.nextToken()`

### getLexer

    protected LpLexer getLexer()

  An accessor method for `lexer`.

  Returns:
    `lexer`

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD