On 18/07/19 18:14, Ethan Furman wrote:
> On 07/18/2019 06:04 AM, Antoon Pardon wrote:
>
>> I am experimenting with writing an Earley Parser. Now I would like to
>> have the non-terminals from the grammer I am reading in, be represented
>> bye an enum like type. So that if the grammer contains the following
>> production: Term -> Term '+' Factor I can reprensent the right hand side
>> with a list that gets printed something like the following:
>> [<Non_Terminal.Term:1>, '+', <Non_Terminal.Factor:2>] I am a bit at a
>> loss right now on how to start. Can someone point me in the right
>> direction?
>
> The basic method is:
>
> from enum import Enum     # `from aenum` [1][2] if less than Python 3.4
>
> Class NonTerminal(Enum):
>     Term = 1
>     Factor = 2
>     ...
>
> The docs [3] also have a lot of information.
>
> Does that answer your question?

I don't seem to have made myself clear. The grammar with its Terminals
and NonTerminals is read in from a file. The program doesn't know what
they will be.

For the moment what I am thinking about is something as follows:

grammar = LoadGrammer(GrammarFile)
EnumList = ['class NonTerminal(Enum):\n']
for Index, NonTerminal in enumerate(grammar.NonTerminals):
    EnumList.append('    %s = %d\n' % (NonTerminal, Index))
exec(''.join(EnumList), ..., ...)

The problem with this approach is that I can't use these values until
the whole grammer is loaded. I would prefer some way to add values
during the loading of the grammar.

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to