|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectlp.parse.LpParser
public class LpParser
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_WORDIn this grammar
LpTokenType
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 |
---|
private final LpLexer lexer
Constructor Detail |
---|
public LpParser()
LpParser
instance that uses a new LpLexer
instance to tokenize the character input before parsing.
public LpParser(LpLexer lexer)
LpParser
instance that uses lexer
to
tokenize the character input before parsing.
lexer
- the LpLexer
instance used to tokenize the character
input before parsingMethod Detail |
---|
public void setInput(CharSequence input)
LpParser
to the given
CharSequence
.
input
- the character sequence to be parsed
IllegalArgumentException
- if input
is null
LpLexer.setInput(CharSequence)
public void setInput(File input)
LpParser
to the contents of the
given file. The default system character encoding is used to read the
file.
input
- the file to be parsed
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while opening or reading the file
IllegalArgumentException
- if input
is null
LpLexer.setInput(File)
public void setInput(Reader input)
LpParser
to the given
Reader
.
input
- the input reader
IOException
- (wrapped in an ExceptionAdapter
)
in case an I/O exception occurs while reading from the Reader
IllegalArgumentException
- if input
is null
LpLexer.setInput(Reader)
public void close()
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.
close
in interface Closeable
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while closing the underlying lexerLpLexer.close()
public boolean hasMoreTokens()
true
if there are more tokens on the input and
false
if there are no more tokens or if close()
has
already been called.
public LogicProgram parseAllRules()
LogicProgram
object.
LogicProgram
instance containing the parsed rules
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)parseRule()
public LogicProgram parseAllRules(LogicProgram program)
LogicProgram
object.
LogicProgram
instance containing the parsed rules
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)parseRule()
public LpRule parseRule()
LpRule
instance—an object model of the parsed rule.
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 descriptionprotected LpRule parseOnlyRule()
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 descriptionprotected Set<LpLiteral> parseRuleBody(boolean constraint)
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)*)?
constraint
- if true
, at least one literal is required after
the arrow (in case there is one)
LpLiteral
objects
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 descriptionpublic LpLiteral parseLiteral()
LpLiteral
instance—an object model of the parsed literal.
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 descriptionpublic LpAtom parseAtom()
LpAtom
instance—an object model of the parsed atom.
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 descriptionpublic LpTerm parseTerm()
LpTerm
interface—an object model of the
parsed term.
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 descriptionprotected List<LpTerm> parseArguments()
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 descriptionprotected void match(LpTokenType token)
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();
token
- the expected token
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 tokenprotected void expect(LpTokenType token)
LpLexer.getTokenType()
with an expected token. Throws a LpParserException
if the tokens
differ.
token
- the expected token
LpParserException
- in case the lookahead token is not as expectedprotected final void nextToken()
LpLexer
.
IOException
- (wrapped in an ExceptionAdapter
) in
case an I/O exception occurs while identifying the next tokenLpLexer.nextToken()
protected LpLexer getLexer()
lexer
.
lexer
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |