def leftcorner_parents(self, cat):
"""
Return the set of all nonterminals for which the given category
is a left corner. This is the inverse of the leftcorner relation.
:param cat: the suggested leftcorner
:type cat: Nonterminal
:return: the set of all parents to the leftcorner
:rtype: set(Nonterminal)
"""
return self._leftcorner_parents.get(cat, set([cat]))
python类nonterminals()的实例源码
def __init__(self, start, productions):
"""
Create a new feature-based grammar, from the given start
state and set of ``Productions``.
:param start: The start symbol
:type start: FeatStructNonterminal
:param productions: The list of productions that defines the grammar
:type productions: list(Production)
"""
CFG.__init__(self, start, productions)
# The difference with CFG is that the productions are
# indexed on the TYPE feature of the nonterminals.
# This is calculated by the method _get_type_if_possible().
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
grammar.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None):
"""
Return a pair consisting of a starting category and a list of
``Productions``.
:param input: a grammar, either in the form of a string or else
as a list of strings.
:param nonterm_parser: a function for parsing nonterminals.
It should take a ``(string, position)`` as argument and
return a ``(nonterminal, position)`` as result.
:param probabilistic: are the grammar rules probabilistic?
:type probabilistic: bool
:param encoding: the encoding of the grammar, if it is a binary string
:type encoding: str
"""
if encoding is not None:
input = input.decode(encoding)
if isinstance(input, string_types):
lines = input.split('\n')
else:
lines = input
start = None
productions = []
continue_line = ''
for linenum, line in enumerate(lines):
line = continue_line + line.strip()
if line.startswith('#') or line=='': continue
if line.endswith('\\'):
continue_line = line[:-1].rstrip()+' '
continue
continue_line = ''
try:
if line[0] == '%':
directive, args = line[1:].split(None, 1)
if directive == 'start':
start, pos = nonterm_parser(args, 0)
if pos != len(args):
raise ValueError('Bad argument to start directive')
else:
raise ValueError('Bad directive')
else:
# expand out the disjunctions on the RHS
productions += _read_production(line, nonterm_parser, probabilistic)
except ValueError as e:
raise ValueError('Unable to parse line %s: %s\n%s' %
(linenum+1, line, e))
if not productions:
raise ValueError('No productions found!')
if not start:
start = productions[0].lhs()
return (start, productions)