13

I found (after another question here on StackOverflow) this interesting library written in Python which goal is the grammar parsing.

http://code.google.com/p/modgrammar/

And I also found this tutorial regarding it:

http://packages.python.org/modgrammar/tutorial.html

So, after reading all the tutorial, I understood that this is what I'm looking for! I tried to write the first example in the tutorial:

from modgrammar import *
class MyGrammar (Grammar):
  grammar = (LITERAL("Hello,"), LITERAL("world!"))

but I ran into this error:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from modgrammar import *
  File "/Users/tesi/Desktop/Prova/modgrammar/__init__.py", line 503
    class Grammar(metaclass=GrammarClass):
                           ^
SyntaxError: invalid syntax

The problem seems to be in the metaclass declaration. Might be I have to add a "compilation flag" when I call the python interpreter? Some good news?! :) Thx

Dario
  • 215
  • 2
  • 10

3 Answers3

19
class Grammar(metaclass=GrammarClass)

is using Python3 syntax. The equivalent Python2 syntax would be

class Grammar(object):
    __metaclass__=GrammarClass

but since there may be lots of other Python3-isms, you may have to use Python3 to use modgrammar.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thank you so much! Stupidly, I hadn't thought the problem was on the Python version! :) – Dario Aug 15 '11 at 12:45
4

I've backport modgrammar to Python 2.6+. Backport will also work with Python 3.x. You can find it here: https://github.com/don-ramon/modgrammar-py2.

Aleksey Rembish
  • 885
  • 7
  • 5
1

Yeah, this is Python3 library http://code.google.com/p/modgrammar/source/browse/setup.py#32

Python2x doesn't support class Grammar(metaclass=GrammarClass)

agf
  • 171,228
  • 44
  • 289
  • 238
seriyPS
  • 6,817
  • 2
  • 25
  • 16