Take the 2-minute tour ×

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


And I also found this tutorial regarding it:


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

share|improve this question

3 Answers 3

up vote 7 down vote accepted
class Grammar(metaclass=GrammarClass)

is using Python3 syntax. The equivalent Python2 syntax would be

class Grammar(object):

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

share|improve this answer
Thank you so much! Stupidly, I hadn't thought the problem was on the Python version! :) –  Dario Aug 15 '11 at 12:45

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.

share|improve this answer

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

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

share|improve this answer

Your Answer


