TL;DR:
How does one computationally model a grammar's productions such that an indefinite number of products exist for the same left hand side?
I'm working on a project regarding formal language theory and am trying to write a class for building regular grammar objects which can be passed to a finite state machine. My naïve attempt was to create an API for adding a production for each permitted input. A stripped down version of my attempt is as follows (based on the formal definition of a formal grammar G = (N, Σ, P, S)
):
class ContextFreeGrammar:
def __init__(self, variables, alphabet, production_rules, start_variable):
self.variables = variables
self.alphabet = alphabet
self.production_rules = production_rules
self.start_variable = start_variable
def __repr__(self):
return '{}({}, {}, {}, {})'.format(
self.__class__.__name__,
self.variables,
self.alphabet,
self.production_rules,
self.start_variable
)
class RegularGrammar(ContextFreeGrammar):
_regular_expression_grammar = None # TODO
@classmethod
def from_regular_expression(cls, regular_expression):
raise NotImplementedError()
I haven't gotten to the point of actually writing the finite state automaton or the pushdown automaton yet.
The grammar for a regular expression is context-free, so I have included my definition in WSN below:
syntax = expression .
expression = term "|" expression .
expression = term .
term = factor repetition term .
term = factor term .
term = .
repetition = "*" .
repetition = "+" .
repetition = "?" .
repetition = "{" nonnegative_integer "," nonnegative_integer "}" .
repetition = "{" nonnegative_integer ",}" .
repetition = "{," nonnegative_integer "}" .
nonnegative_integer = nonzero_arabic_numeral arabic_numerals .
nonnegative_integer = arabic_numeral .
nonzero_arabic_numeral = "1" .
nonzero_arabic_numeral = "2" .
nonzero_arabic_numeral = "3" .
nonzero_arabic_numeral = "4" .
nonzero_arabic_numeral = "5" .
nonzero_arabic_numeral = "6" .
nonzero_arabic_numeral = "7" .
nonzero_arabic_numeral = "8" .
nonzero_arabic_numeral = "9" .
arabic_numeral = nonzero_arabic_numeral .
arabic_numeral = "0" .
arabic_numerals = arabic_numeral .
arabic_numerals = arabic_numeral arabic_numerals .
factor = "(" expression ")" .
factor = character_class .
factor = character .
escaped_character = "\\." .
escaped_character = "\\(" .
escaped_character = "\\)" .
escaped_character = "\\+" .
escaped_character = "\\*" .
escaped_character = "\\?" .
escaped_character = "\\[" .
escaped_character = "\\]" .
escaped_character = "\\\\" .
escaped_character = "\\{" .
escaped_character = "\\}" .
escaped_character = "\\|" .
character -> TODO ;
character_class = TODO .
One can easily see that I am explicitly splitting alternations into separate productions. I am doing this for ease of implementation. But I am stuck on how I should go about doing character classes and such. I was wanting production_rules
to be a map from the each left hand side to a set of each of its corresponding right hand sides. But that looks infeasible now.
ab*
means "ana
followed by any number ofb
s", not "any number ofab
s. – Fallscharacter
productions; there will be one production for every character in the alphabet other than the ones you need to escape. – Falls.
wildcard is used, I know it could be any possible character. But if I assume that I'm working with Unicode, that's a lot of possible characters. Unicode 7.0 contains 112,956 characters. I think for the sake of characters that require multiple code points, I'm going to scrap ranges in character classes. That makes this slightly easier. I think I might subclassset
or something to that effect once for normal character classes and once for negated character classes and cast a period to an empty negated character class. – Namhoi