Hm,
The language isn't quite like that. I have three reserved words:
"impossible", "if", and "causes".
Terms make up the basic building block of everything else. A term is
either an object constant, variable or a function constant of arity >
0 (which are not reserved words).
For the moment causal law is a statement of one of the following
forms:
term causes term if term.
impossible term if term.
term if term.
At least that's the type of grammar I was going for.
I attempted changing the basic "word" type elements from Token to
Word, giving me the following grammar:
from lepl import *
# language grammar specification - basic objects
IMPOSS = Word('impossible')
CAUSES = Word('causes')
IF = Word('if')
COMMA = Token(',')
PERIOD = Token('.')
LPAREN = Token(r"\(")
RPAREN = Token(r"\)")
OBJECT = Token('[a-z][a-zA-Z0-9]*')
VARIABLE = Token('[A-Z][a-zA-Z0-9]*')
FUNCTION = Token('[a-z][a-zA-Z0-9]*')
TERM = Delayed()
TERM += FUNCTION & LPAREN & TERM & ZeroOrMore(COMMA & TERM) & RPAREN |
\
OBJECT | \
VARIABLE > ''.join
# language grammar specification - causal laws
SCL = TERM & IF & TERM & PERIOD
DCL = TERM & CAUSES & TERM & IF & TERM & PERIOD
EXC = IMPOSS & TERM & IF & TERM & PERIOD
# language grammar specification - action description
STATEMENT = SCL | DCL | EXC
PROGRAM = OneOrMore(STATEMENT)
But I get the following errors:
-bash-3.2 [src]$ python
Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ActionLanguageAL import *
>>> parser = SCL.parse_string
>>> SCL.parse("a if p.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/lepl/matchers.py", line 189,
in parse
return self.null_parser(config, **kargs)(stream)
File "/Library/Python/2.6/site-packages/lepl/matchers.py", line 152,
in null_parser
return make_parser(self, config.stream.null, config, kargs)
File "/Library/Python/2.6/site-packages/lepl/parser.py", line 247,
in make_parser
matcher = make_matcher(matcher, stream, config, kargs)
File "/Library/Python/2.6/site-packages/lepl/parser.py", line 229,
in make_matcher
matcher = rewriter(matcher)
File "/Library/Python/2.6/site-packages/lepl/lexer/rewriters.py",
line 124, in rewriter
tokens = find_tokens(matcher)
File "/Library/Python/2.6/site-packages/lepl/lexer/rewriters.py",
line 62, in find_tokens
for n in non_tokens)))
lepl.lexer.support.LexerError: The grammar contains a mix of Tokens
and non-Token matchers at the top level. If Tokens are used then non-
token matchers that consume input must only appear "inside" Tokens.
The non-Token matchers include: Any.
On Nov 19, 4:07 pm, andrew cooke <
and...@acooke.org> wrote:
> 2 things stand out to me:
>
> 1 - Token('.'). do you really mean that? remember that tokens match
> regular expressions....
>
> 2 - this may be a matter of style. i think it will work your way too
> (LEPL can handle more than one token matching) but Token('impossible')
> and Token('[a-z][a-zA-Z0-9]*') are both going to match the same thing.
> instead i would do:
>
> WORD = Token('[a-z][a-zA-Z0-9]*')
> IMPOSS = WORD('impossible')
> OBJECT = WORD
>
> so only one kind of token will be generated for all of those, and
> OBJECT will match as it did before. IMPOSS will match the *same*
> token, but will then also test the contents for a literal match.
>
> in other words, personally, i define tokens at a lower level than the
> grammars - they're just the basic "different kinds of chunks of text"
> you can match. then *inside* the token i add extra conditions. so i
> normally have just three kinds of tokens: words, numbers and symbols.
>
> seehttp://
www.acooke.org/lepl/lexer.html#usehalf way down where i
> talk abot specialisation.
>
> andrew
>
> 2009/11/19 LogicProgrammer <
logicprogram...@gmail.com>:
> >> > >> > You received this message...
>
> read more »