# Path: pydsl/grammar/symbol.py
# class Symbol(object):
#     pass
# 
# class TerminalSymbol(Symbol):
# 
#     def __init__(self, gd):
#         if not isinstance(gd, Grammar):
#             raise TypeError("Expected Grammar, got %s" % (gd,))
#         Symbol.__init__(self)
#         if not gd:
#             raise Exception
#         self.gd = gd
# 
#     def __hash__(self):
#         return hash(self.gd)
# 
#     def check(self, data):# ->bool:
#         """Checks if input is recognized as this symbol"""
#         return check(self.gd, data)
# 
#     def first(self):
#         return self.gd.first
# 
#     def __eq__(self, other):
#         """StringTerminalSymbol are equals if definition and names are equal"""
#         try:
#             return self.gd == other.gd
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return "<TS: " + str(self.gd) + ">"
# 
# class NullSymbol(Symbol):
#     _instance = None
#     def __new__(cls):
#         if not cls._instance:
#             cls._instance = super(NullSymbol, cls).__new__(cls)
#         return cls._instance
# 
#     def __eq__(self, other):
#         return isinstance(other, NullSymbol)
# 
#     def __ne__(self, other):
#         return not self.__eq__(other)
# 
#     def __bool__(self):
#         return False
# 
# class EndSymbol(Symbol):
#     _instance = None
#     def __new__(cls):
#         if cls._instance is None:
#             cls._instance = super(EndSymbol, cls).__new__(cls)
#         return cls._instance
# 
#     def __hash__(self):
#         assert(EndSymbol._instance is not None)
#         return hash(id(self._instance))
# 
#     def __eq__(self, other):
#         result = isinstance(other, EndSymbol) or EndSymbol == other
#         return result
# 
#     def __bool__(self):
#         return False
# 
#     def __str__(self):
#         return "$"
# 
# Path: pydsl/grammar/definition.py
# class Grammar(object):
# 
#     def __init__(self, base_alphabet = None):
#         self.__base_alphabet = base_alphabet
# 
#     def enum(self):
#         """Generates every possible accepted string"""
#         raise NotImplementedError
# 
#     @property
#     def first(self):
#         """Alphabet that matches every possible first element.
#         the returned value is a subset of the base_alphabet"""
#         return self.alphabet
# 
#     @property
#     def minsize(self):# -> int:
#         """Returns the minimum size in alphabet tokens"""
#         return 0
# 
#     @property
#     def maxsize(self):
#         """Returns the max size in alphabet tokens"""
#         return None
# 
#     @property
#     def alphabet(self):
#         if not self.__base_alphabet:
#             raise AttributeError
#         return self.__base_alphabet
# 
# Path: pydsl/grammar/PEG.py
# class Choice(set, Grammar):
#     """Uses a list of grammar definitions with common base alphabets"""
#     def __init__(self, grammarlist, calculate_base_alphabet = True):
#         set.__init__(self, grammarlist)
#         if calculate_base_alphabet:
#             base_alphabet = set()
#             for x in self:
#                 base_alphabet = base_alphabet.union(x.alphabet)
#         else:
#             base_alphabet = None
#         Grammar.__init__(self, base_alphabet)
# 
#     def __str__(self):
#         return str([str(x) for x in self])
# 
#     def __add__(self, other):
#         return Choice([x for x in self] + [x for x in other])
# 
#     def __hash__(self):
#         return hash(tuple(x for x in self))


from pydsl.grammar.symbol import Symbol, TerminalSymbol, NullSymbol, EndSymbol
from pydsl.grammar.definition import Grammar
from pydsl.grammar.PEG import Choice
                from pydsl.grammar.definition import String
                import collections
            if fulllist.count(rule) > 1:
                raise ValueError("Duplicated rule: " + str(rule))
        self.fulllist = tuple(fulllist)
        self.options = options or {}

    def __hash__(self):
        return hash(self.fulllist)

    @property
    def alphabet(self):
        return Choice([x.gd for x in self.terminal_symbols])

    @property
    def productions(self):
        return [x for x in self.fulllist if isinstance(x, Production)]

    @property
    def terminal_symbols(self):
        return [x for x in self.fulllist if isinstance(x, TerminalSymbol)]

    @property
    def first(self):
        """Returns the a grammar definition that includes all first elements of this grammar"""
        return self.first_lookup(self.initialsymbol)

    def first_lookup(self, symbol, size=1):
        """
        Returns a Grammar Definition with the first n terminal symbols
        produced by the input symbol
        """

# Path: pydsl/grammar/symbol.py
# class Symbol(object):
#     pass
# 
# class TerminalSymbol(Symbol):
# 
#     def __init__(self, gd):
#         if not isinstance(gd, Grammar):
#             raise TypeError("Expected Grammar, got %s" % (gd,))
#         Symbol.__init__(self)
#         if not gd:
#             raise Exception
#         self.gd = gd
# 
#     def __hash__(self):
#         return hash(self.gd)
# 
#     def check(self, data):# ->bool:
#         """Checks if input is recognized as this symbol"""
#         return check(self.gd, data)
# 
#     def first(self):
#         return self.gd.first
# 
#     def __eq__(self, other):
#         """StringTerminalSymbol are equals if definition and names are equal"""
#         try:
#             return self.gd == other.gd
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return "<TS: " + str(self.gd) + ">"
# 
# class NullSymbol(Symbol):
#     _instance = None
#     def __new__(cls):
#         if not cls._instance:
#             cls._instance = super(NullSymbol, cls).__new__(cls)
#         return cls._instance
# 
#     def __eq__(self, other):
#         return isinstance(other, NullSymbol)
# 
#     def __ne__(self, other):
#         return not self.__eq__(other)
# 
#     def __bool__(self):
#         return False
# 
# class EndSymbol(Symbol):
#     _instance = None
#     def __new__(cls):
#         if cls._instance is None:
#             cls._instance = super(EndSymbol, cls).__new__(cls)
#         return cls._instance
# 
#     def __hash__(self):
#         assert(EndSymbol._instance is not None)
#         return hash(id(self._instance))
# 
#     def __eq__(self, other):
#         result = isinstance(other, EndSymbol) or EndSymbol == other
#         return result
# 
#     def __bool__(self):
#         return False
# 
#     def __str__(self):
#         return "$"
# 
# Path: pydsl/grammar/definition.py
# class Grammar(object):
# 
#     def __init__(self, base_alphabet = None):
#         self.__base_alphabet = base_alphabet
# 
#     def enum(self):
#         """Generates every possible accepted string"""
#         raise NotImplementedError
# 
#     @property
#     def first(self):
#         """Alphabet that matches every possible first element.
#         the returned value is a subset of the base_alphabet"""
#         return self.alphabet
# 
#     @property
#     def minsize(self):# -> int:
#         """Returns the minimum size in alphabet tokens"""
#         return 0
# 
#     @property
#     def maxsize(self):
#         """Returns the max size in alphabet tokens"""
#         return None
# 
#     @property
#     def alphabet(self):
#         if not self.__base_alphabet:
#             raise AttributeError
#         return self.__base_alphabet
# 
# Path: pydsl/grammar/PEG.py
# class Choice(set, Grammar):
#     """Uses a list of grammar definitions with common base alphabets"""
#     def __init__(self, grammarlist, calculate_base_alphabet = True):
#         set.__init__(self, grammarlist)
#         if calculate_base_alphabet:
#             base_alphabet = set()
#             for x in self:
#                 base_alphabet = base_alphabet.union(x.alphabet)
#         else:
#             base_alphabet = None
#         Grammar.__init__(self, base_alphabet)
# 
#     def __str__(self):
#         return str([str(x) for x in self])
# 
#     def __add__(self, other):
#         return Choice([x for x in self] + [x for x in other])
# 
#     def __hash__(self):
#         return hash(tuple(x for x in self))


# Path: pydsl/grammar/BNF.py
from pydsl.grammar.symbol import Symbol, TerminalSymbol, NullSymbol, EndSymbol
from pydsl.grammar.definition import Grammar
from pydsl.grammar.PEG import Choice
                from pydsl.grammar.definition import String
                import collections

            if fulllist.count(rule) > 1:
                raise ValueError("Duplicated rule: " + str(rule))
        self.fulllist = tuple(fulllist)
        self.options = options or {}

    def __hash__(self):
        return hash(self.fulllist)

    @property
    def alphabet(self):
        return Choice([x.gd for x in self.terminal_symbols])

    @property
    def productions(self):
        return [x for x in self.fulllist if isinstance(x, Production)]

    @property
    def terminal_symbols(self):
        return [x for x in self.fulllist if isinstance(x, TerminalSymbol)]

    @property
    def first(self):
        """Returns the a grammar definition that includes all first elements of this grammar"""
        return self.first_lookup(self.initialsymbol)

    def first_lookup(self, symbol, size=1):
        """
        Returns a Grammar Definition with the first n terminal symbols
        produced by the input symbol
        """

        if isinstance(symbol, (TerminalSymbol, NullSymbol)):