last_terminal
):
raise NotImplementedException
- def add_to_symbols(self, pyacc, action, symbols):
- return action
+ def add_to_symbols(self, pyacc, last_action, symbols):
+ return last_action
class Action(Item):
# GENERATE ELEMENT() BEGIN
last_terminal
):
return last_terminal
- def add_to_symbols(self, pyacc, action, symbols):
- assert action is None
+ def add_to_symbols(self, pyacc, last_action, symbols):
+ assert last_action is None
return self[0]
class DPrec(Item):
else:
assert False
return self.symbol if self.symbol >= 0 else last_terminal
- def add_to_symbols(self, pyacc, action, symbols):
- assert action is None
+ def add_to_symbols(self, pyacc, last_action, symbols):
+ assert last_action is None
symbols.append(
(pyacc.terminal_symbols[self.symbol].character_set, [])
if self.symbol >= 0 else
pyacc.productions.append(self)
def to_lr1_production(self, pyacc):
- action = None
+ last_action = None
symbols = []
lookaheads = []
for i in self:
- action = i.add_to_symbols(pyacc, action, symbols)
+ last_action = i.add_to_symbols(pyacc, last_action, symbols)
return (
+ # precedence
(
- # precedence
- (
- (pyacc.terminal_symbols[self.precedence_terminal].precedence << 2) |
- (pyacc.terminal_symbols[self.precedence_terminal].associativity & 3)
- if self.precedence_terminal != -1 else
- -1
- ),
- # symbols (list of terminal_set, nonterminal_set)
- symbols,
- # lookaheads (list of initial_set, can_be_empty)
- [([], False) for i in range(len(symbols))] + [([], True)],
- # group_bounds
- []
+ (pyacc.terminal_symbols[self.precedence_terminal].precedence << 2) |
+ (pyacc.terminal_symbols[self.precedence_terminal].associativity & 3)
+ if self.precedence_terminal != -1 else
+ -1
),
- action if action is not None else PYACC.BracedCode()
+ # symbols (list of terminal_set, nonterminal_set)
+ symbols,
+ # lookaheads (list of initial_set, can_be_empty)
+ [([], False) for i in range(len(symbols))] + [([], True)],
+ # ref_data
+ last_action if last_action is not None else PYACC.BracedCode()
)
class Section(element.Element):
[([], self.nonterminal_symbols[self.start_nonterminal].character_set)],
# lookaheads (list of initial_set, can_be_empty)
[([], False), ([], True)],
- # group_bounds
- []
+ # ref_data
+ PYACC.BracedCode()
)
]
- actions = []
for i in self.productions:
- lr1_production, action = i.to_lr1_production(self)
- lr1_productions.append(lr1_production)
- actions.append(action)
+ lr1_productions.append(i.to_lr1_production(self))
# propagate lookaheads
modified = True
lookaheads[i] = (initial_set, can_be_empty)
modified = True
- return (lr1.LR1(lr1_productions, len(self.terminal_symbols), 0), actions)
+ return lr1.LR1(lr1_productions, len(self.terminal_symbols), 0)
# GENERATE FACTORY(element.Element) BEGIN
tag_to_class = {
self.n_terminals = n_terminals
def generate(pyacc, skel_file, out_file):
- lr1, actions = pyacc.to_lr1()
- lr1dfa = lr1.to_lalr1()
+ lr1dfa = pyacc.to_lr1().to_lalr1()
# generate translate table for terminal symbols
# this undoes yacc/bison's rather wasteful mapping of 0x00..0xff to literal
{1:s}
break;
'''.format(
- i + 1,
- actions[i].get_text(
- bison_lr1dfa.rule_data[i + 1, 1] # length of production
+ i,
+ lr1dfa.productions[i][1].get_text(
+ lr1dfa.productions[i][0] # length of production
)
)
- for i in range(len(actions))
- if len(actions[i])
+ for i in range(len(lr1dfa.productions))
+ if len(lr1dfa.productions[i][1])
]
)
)
# priority,
# symbols,
# lookaheads,
- # group_bounds
+ # ref_data
# )
# priority: bit 0 = right to left, bits 1: = numeric priority
# symbols: list of symbol_desc
# note: last lookahead_desc is a sentinel consisting of ([], True), so that
# initial_set is empty (will be augmented by context) and can_be_empty is
# True (because all symbols from the end to the end can obviously be empty)
- # group_bounds: list of group_bound
+ # ref_data: can be anything for caller's use, usually list of group_bound
# group_bound: (start_index, end_index, tag, kwargs)
# where start_index, end_index are indices into list of character_set,
# and tag, kwargs will be passed to apply_markup() hence factory(),
next_items.append((i, j + 1, lookahead_set))
return next_items, next_item_to_index, nonterminal0, nonterminal1
- def parse_text(self, text, i):
- items = [(0, 0, [self.eof_terminal, self.eof_terminal + 1])]
- item_to_index = {(0, 0): 0}
- value_stack = []
- state_stack = []
- lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
- while True:
- self.lookahead_item_set_closure(items, item_to_index)
- value_stack.append(i)
- state_stack.append(items)
- items, item_to_index, reductions, _, _ = (
- self.lookahead_item_set_action(items, lookahead_character)
- )
- if len(items) != 0:
- if len(reductions) != 0:
- sys.stderr.write(
- 'shift/reduce conflict: {0:s} vs {1:s}\n'.format(
- ','.join([str(i) for i, _, _ in next_items]),
- ','.join([str(i) for i in reductions])
- )
- )
- i += 1
- lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
- elif len(reductions) != 0:
- if len(reductions) != 1:
- sys.stderr.write(
- 'reduce/reduce conflict: {0:s}\n'.format(
- ','.join([str(i) for i in reductions])
- )
- )
- reduce = min(reductions)
- _, symbols, _, group_bounds = self.productions[reduce]
- base = len(value_stack) - len(symbols) - 1
- for j in range(len(group_bounds) - 1, -1, -1):
- k, l, tag, _ = group_bounds[j]
- k += base
- if l != 1:
- value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
- sys.stderr.write(
- 'text \'{0:s}\' tag \'{1:s}\'\n'.format(
- text[value_stack[k]:value_stack[k + 1]],
- tag
- )
- )
- del value_stack[base + 1:]
- del state_stack[base + 1:]
- if reduce == 0:
- assert base == 0
- return
- items, item_to_index, _, _ = (
- self.lookahead_item_set_goto(state_stack[-1], reduce)
- )
- assert len(items) != 0
- else:
- raise Exception(
- 'syntax error at {0:d}: {1:s}'.format(i, text[i:])
- )
+ #def parse_text(self, text, i):
+ # items = [(0, 0, [self.eof_terminal, self.eof_terminal + 1])]
+ # item_to_index = {(0, 0): 0}
+ # value_stack = []
+ # state_stack = []
+ # lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
+ # while True:
+ # self.lookahead_item_set_closure(items, item_to_index)
+ # value_stack.append(i)
+ # state_stack.append(items)
+ # items, item_to_index, reductions, _, _ = (
+ # self.lookahead_item_set_action(items, lookahead_character)
+ # )
+ # if len(items) != 0:
+ # if len(reductions) != 0:
+ # sys.stderr.write(
+ # 'shift/reduce conflict: {0:s} vs {1:s}\n'.format(
+ # ','.join([str(i) for i, _, _ in next_items]),
+ # ','.join([str(i) for i in reductions])
+ # )
+ # )
+ # i += 1
+ # lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
+ # elif len(reductions) != 0:
+ # if len(reductions) != 1:
+ # sys.stderr.write(
+ # 'reduce/reduce conflict: {0:s}\n'.format(
+ # ','.join([str(i) for i in reductions])
+ # )
+ # )
+ # reduce = min(reductions)
+ # _, symbols, _, ref_data = self.productions[reduce]
+ # base = len(value_stack) - len(symbols) - 1
+ # for j in range(len(ref_data) - 1, -1, -1):
+ # k, l, tag, _ = ref_data[j]
+ # k += base
+ # if l != 1:
+ # value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
+ # sys.stderr.write(
+ # 'text \'{0:s}\' tag \'{1:s}\'\n'.format(
+ # text[value_stack[k]:value_stack[k + 1]],
+ # tag
+ # )
+ # )
+ # del value_stack[base + 1:]
+ # del state_stack[base + 1:]
+ # if reduce == 0:
+ # assert base == 0
+ # return
+ # items, item_to_index, _, _ = (
+ # self.lookahead_item_set_goto(state_stack[-1], reduce)
+ # )
+ # assert len(items) != 0
+ # else:
+ # raise Exception(
+ # 'syntax error at {0:d}: {1:s}'.format(i, text[i:])
+ # )
- def parse_yychunk(self, root, pos, off, factory, yychunk_iter):
- if pos < 0:
- pos, off = element.to_start_relative(root, pos, off)
+ #def parse_yychunk(self, root, pos, off, factory, yychunk_iter):
+ # if pos < 0:
+ # pos, off = element.to_start_relative(root, pos, off)
- items = [(0, 0, [self.eof_terminal, self.eof_terminal + 1])]
- item_to_index = {(0, 0): 0}
- value_stack = []
- state_stack = []
- text = element.get_text(root, pos)
- while off >= len(text):
- if pos < len(root):
- pos += 1
- off = 0
- else:
- try:
- next(yychunk_iter)
- except StopIteration:
- lookahead_character = self.eof_terminal
- break
- text = element.get_text(root, pos)
- else:
- lookahead_character = ord(text[off])
- while True:
- self.lookahead_item_set_closure(items, item_to_index)
- value_stack.append((pos, off))
- state_stack.append(items)
- items, item_to_index, reductions, _, _ = (
- self.lookahead_item_set_action(items, lookahead_character)
- )
- if len(items) != 0:
- if len(reductions) != 0:
- sys.stderr.write(
- 'shift/reduce conflict: {0:s} vs {1:s}\n'.format(
- ','.join([str(i) for i, _ in next_lookahead_item_set.keys()]),
- ','.join([str(i) for i in reductions])
- )
- )
- off += 1
- while off >= len(text):
- if pos < len(root):
- pos += 1
- off = 0
- else:
- try:
- next(yychunk_iter)
- except StopIteration:
- lookahead_character = self.eof_terminal
- break
- text = element.get_text(root, pos)
- else:
- lookahead_character = ord(text[off])
- elif len(reductions) != 0:
- if len(reductions) != 1:
- sys.stderr.write(
- 'reduce/reduce conflict: {0:s}\n'.format(
- ','.join([str(i) for i in reductions])
- )
- )
- reduce = min(reductions)
- _, symbols, _, group_bounds = self.productions[reduce]
- base = len(value_stack) - len(symbols) - 1
- end_relative = len(value_stack)
- for j in range(len(group_bounds) - 1, -1, -1):
- k, l, tag, kwargs = group_bounds[j]
- k += base
- assert k < end_relative
- if l != 1:
- value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
- end_relative = max(k + 1, end_relative + 1 - l)
- while end_relative > k + 1:
- end_relative -= 1
- pos1, off1 = value_stack[end_relative]
- value_stack[end_relative] = (
- element.to_end_relative(root, pos1, off1)
- )
- pos0, off0 = value_stack[k]
- pos1, off1 = value_stack[k + 1]
- work.apply_markup(
- root,
- pos0,
- off0,
- pos1,
- off1,
- factory,
- tag,
- **kwargs
- )
- if end_relative < len(value_stack):
- pos, off = value_stack[-1]
- pos, off = element.to_start_relative(root, pos, off)
- text = element.get_text(root, pos)
- del value_stack[base + 1:]
- del state_stack[base + 1:]
- if reduce == 0:
- assert base == 0
- return
- items, item_to_index, _, _ = (
- self.lookahead_item_set_goto(state_stack[-1], reduce)
- )
- assert len(items) != 0
- else:
- raise Exception(
- 'syntax error at {0:d},{1:d}: {2:s}'.format(pos, off, text[off:])
- )
+ # items = [(0, 0, [self.eof_terminal, self.eof_terminal + 1])]
+ # item_to_index = {(0, 0): 0}
+ # value_stack = []
+ # state_stack = []
+ # text = element.get_text(root, pos)
+ # while off >= len(text):
+ # if pos < len(root):
+ # pos += 1
+ # off = 0
+ # else:
+ # try:
+ # next(yychunk_iter)
+ # except StopIteration:
+ # lookahead_character = self.eof_terminal
+ # break
+ # text = element.get_text(root, pos)
+ # else:
+ # lookahead_character = ord(text[off])
+ # while True:
+ # self.lookahead_item_set_closure(items, item_to_index)
+ # value_stack.append((pos, off))
+ # state_stack.append(items)
+ # items, item_to_index, reductions, _, _ = (
+ # self.lookahead_item_set_action(items, lookahead_character)
+ # )
+ # if len(items) != 0:
+ # if len(reductions) != 0:
+ # sys.stderr.write(
+ # 'shift/reduce conflict: {0:s} vs {1:s}\n'.format(
+ # ','.join([str(i) for i, _ in next_lookahead_item_set.keys()]),
+ # ','.join([str(i) for i in reductions])
+ # )
+ # )
+ # off += 1
+ # while off >= len(text):
+ # if pos < len(root):
+ # pos += 1
+ # off = 0
+ # else:
+ # try:
+ # next(yychunk_iter)
+ # except StopIteration:
+ # lookahead_character = self.eof_terminal
+ # break
+ # text = element.get_text(root, pos)
+ # else:
+ # lookahead_character = ord(text[off])
+ # elif len(reductions) != 0:
+ # if len(reductions) != 1:
+ # sys.stderr.write(
+ # 'reduce/reduce conflict: {0:s}\n'.format(
+ # ','.join([str(i) for i in reductions])
+ # )
+ # )
+ # reduce = min(reductions)
+ # _, symbols, _, ref_data = self.productions[reduce]
+ # base = len(value_stack) - len(symbols) - 1
+ # end_relative = len(value_stack)
+ # for j in range(len(ref_data) - 1, -1, -1):
+ # k, l, tag, kwargs = ref_data[j]
+ # k += base
+ # assert k < end_relative
+ # if l != 1:
+ # value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
+ # end_relative = max(k + 1, end_relative + 1 - l)
+ # while end_relative > k + 1:
+ # end_relative -= 1
+ # pos1, off1 = value_stack[end_relative]
+ # value_stack[end_relative] = (
+ # element.to_end_relative(root, pos1, off1)
+ # )
+ # pos0, off0 = value_stack[k]
+ # pos1, off1 = value_stack[k + 1]
+ # work.apply_markup(
+ # root,
+ # pos0,
+ # off0,
+ # pos1,
+ # off1,
+ # factory,
+ # tag,
+ # **kwargs
+ # )
+ # if end_relative < len(value_stack):
+ # pos, off = value_stack[-1]
+ # pos, off = element.to_start_relative(root, pos, off)
+ # text = element.get_text(root, pos)
+ # del value_stack[base + 1:]
+ # del state_stack[base + 1:]
+ # if reduce == 0:
+ # assert base == 0
+ # return
+ # items, item_to_index, _, _ = (
+ # self.lookahead_item_set_goto(state_stack[-1], reduce)
+ # )
+ # assert len(items) != 0
+ # else:
+ # raise Exception(
+ # 'syntax error at {0:d},{1:d}: {2:s}'.format(pos, off, text[off:])
+ # )
def to_clr1(self):
_lr1dfa = lr1dfa.LR1DFA(
[],
[
- (len(symbols), group_bounds)
- for _, symbols, _, group_bounds in self.productions
+ (len(symbols), ref_data)
+ for _, symbols, _, ref_data in self.productions
],
self.n_terminals,
self.eof_terminal
_lr1dfa = lr1dfa.LR1DFA(
[],
[
- (len(symbols), group_bounds)
- for _, symbols, _, group_bounds in self.productions
+ (len(symbols), ref_data)
+ for _, symbols, _, ref_data in self.productions
],
self.n_terminals,
self.eof_terminal
# action: shift = new state * 2, reduce = production * 2 + 1, error = -1
# goto: reduce = production, error = -1 (but error can't really happen)
# productions: list of production
- # production: (len(symbols), group_bounds)
+ # production: (len(symbols), ref_data)
# len(symbols): how many states to pop stack to reduce this production
- # group_bounds: list of group_bound
+ # ref_data: can be anything for caller's use, usually list of group_bound
# group_bound: (start_index, end_index, tag, kwargs)
# where start_index, end_index are indices into list of character_set,
# and tag, kwargs will be passed to apply_markup() hence factory(),
self.n_terminals = n_terminals
self.eof_terminal = eof_terminal
- def parse_text(self, text, i):
- state = 0
- value_stack = []
- state_stack = []
- lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
- while True:
- value_stack.append(i)
- state_stack.append(state)
- action = self.states[state][1][
- bisect.bisect_right(self.states[state][0], lookahead_character)
- ]
- if action == -1:
- raise Exception(
- 'syntax error at {0:d}: {1:s}'.format(i, text[i:])
- )
- if (action & 1) == 0:
- state = action >> 1
- i += 1
- lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
- else:
- reduce = action >> 1
- len_symbols, group_bounds = self.productions[reduce]
- base = len(value_stack) - len_symbols - 1
- for j in range(len(group_bounds) - 1, -1, -1):
- k, l, tag, _ = group_bounds[j]
- k += base
- if l != 1:
- value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
- sys.stdout.write(
- 'text \'{0:s}\' tag \'{1:s}\'\n'.format(
- text[value_stack[k]:value_stack[k + 1]],
- tag
- )
- )
- del value_stack[base + 1:]
- del state_stack[base + 1:]
- if reduce == 0:
- assert base == 0
- return
- state = self.states[state_stack[-1]][3][
- bisect.bisect_right(self.states[state_stack[-1]][2], reduce)
- ]
- assert state != -1
+ #def parse_text(self, text, i):
+ # state = 0
+ # value_stack = []
+ # state_stack = []
+ # lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
+ # while True:
+ # value_stack.append(i)
+ # state_stack.append(state)
+ # action = self.states[state][1][
+ # bisect.bisect_right(self.states[state][0], lookahead_character)
+ # ]
+ # if action == -1:
+ # raise Exception(
+ # 'syntax error at {0:d}: {1:s}'.format(i, text[i:])
+ # )
+ # if (action & 1) == 0:
+ # state = action >> 1
+ # i += 1
+ # lookahead_character = ord(text[i]) if i < len(text) else self.eof_terminal
+ # else:
+ # reduce = action >> 1
+ # len_symbols, ref_data = self.productions[reduce]
+ # base = len(value_stack) - len_symbols - 1
+ # for j in range(len(ref_data) - 1, -1, -1):
+ # k, l, tag, _ = ref_data[j]
+ # k += base
+ # if l != 1:
+ # value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
+ # sys.stdout.write(
+ # 'text \'{0:s}\' tag \'{1:s}\'\n'.format(
+ # text[value_stack[k]:value_stack[k + 1]],
+ # tag
+ # )
+ # )
+ # del value_stack[base + 1:]
+ # del state_stack[base + 1:]
+ # if reduce == 0:
+ # assert base == 0
+ # return
+ # state = self.states[state_stack[-1]][3][
+ # bisect.bisect_right(self.states[state_stack[-1]][2], reduce)
+ # ]
+ # assert state != -1
- def parse_yychunk(self, root, pos, off, factory, yychunk_iter):
- if pos < 0:
- pos, off = element.to_start_relative(root, pos, off)
+ #def parse_yychunk(self, root, pos, off, factory, yychunk_iter):
+ # if pos < 0:
+ # pos, off = element.to_start_relative(root, pos, off)
- state = 0
- value_stack = []
- state_stack = []
- text = element.get_text(root, pos)
- while off >= len(text):
- if pos < len(root):
- pos += 1
- off = 0
- else:
- try:
- next(yychunk_iter)
- except StopIteration:
- lookahead_character = self.eof_terminal
- break
- text = element.get_text(root, pos)
- else:
- lookahead_character = ord(text[off])
- while True:
- value_stack.append((pos, off))
- state_stack.append(state)
- action = self.states[state][1][
- bisect.bisect_right(self.states[state][0], lookahead_character)
- ]
- #print('lookahead_character', lookahead_character, 'action', action)
- if action == -1:
- raise Exception(
- 'syntax error at {0:d},{1:d}: {2:s}'.format(pos, off, text[off:])
- )
- if (action & 1) == 0:
- state = action >> 1
- off += 1
- while off >= len(text):
- if pos < len(root):
- pos += 1
- off = 0
- else:
- try:
- next(yychunk_iter)
- except StopIteration:
- lookahead_character = self.eof_terminal
- break
- text = element.get_text(root, pos)
- else:
- lookahead_character = ord(text[off])
- else:
- reduce = action >> 1
- len_symbols, group_bounds = self.productions[reduce]
- base = len(value_stack) - len_symbols - 1
- end_relative = len(value_stack)
- for j in range(len(group_bounds) - 1, -1, -1):
- k, l, tag, kwargs = group_bounds[j]
- k += base
- assert k < end_relative
- if l != 1:
- value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
- end_relative = max(k + 1, end_relative + 1 - l)
- while end_relative > k + 1:
- end_relative -= 1
- pos1, off1 = value_stack[end_relative]
- value_stack[end_relative] = (
- element.to_end_relative(root, pos1, off1)
- )
- pos0, off0 = value_stack[k]
- pos1, off1 = value_stack[k + 1]
- work.apply_markup(
- root,
- pos0,
- off0,
- pos1,
- off1,
- factory,
- tag,
- **kwargs
- )
- if end_relative < len(value_stack):
- pos, off = value_stack[-1]
- pos, off = element.to_start_relative(root, pos, off)
- text = element.get_text(root, pos)
- del value_stack[base + 1:]
- del state_stack[base + 1:]
- if reduce == 0:
- assert base == 0
- return
- state = self.states[state_stack[-1]][3][
- bisect.bisect_right(self.states[state_stack[-1]][2], reduce)
- ]
- assert state != -1
+ # state = 0
+ # value_stack = []
+ # state_stack = []
+ # text = element.get_text(root, pos)
+ # while off >= len(text):
+ # if pos < len(root):
+ # pos += 1
+ # off = 0
+ # else:
+ # try:
+ # next(yychunk_iter)
+ # except StopIteration:
+ # lookahead_character = self.eof_terminal
+ # break
+ # text = element.get_text(root, pos)
+ # else:
+ # lookahead_character = ord(text[off])
+ # while True:
+ # value_stack.append((pos, off))
+ # state_stack.append(state)
+ # action = self.states[state][1][
+ # bisect.bisect_right(self.states[state][0], lookahead_character)
+ # ]
+ # #print('lookahead_character', lookahead_character, 'action', action)
+ # if action == -1:
+ # raise Exception(
+ # 'syntax error at {0:d},{1:d}: {2:s}'.format(pos, off, text[off:])
+ # )
+ # if (action & 1) == 0:
+ # state = action >> 1
+ # off += 1
+ # while off >= len(text):
+ # if pos < len(root):
+ # pos += 1
+ # off = 0
+ # else:
+ # try:
+ # next(yychunk_iter)
+ # except StopIteration:
+ # lookahead_character = self.eof_terminal
+ # break
+ # text = element.get_text(root, pos)
+ # else:
+ # lookahead_character = ord(text[off])
+ # else:
+ # reduce = action >> 1
+ # len_symbols, ref_data = self.productions[reduce]
+ # base = len(value_stack) - len_symbols - 1
+ # end_relative = len(value_stack)
+ # for j in range(len(ref_data) - 1, -1, -1):
+ # k, l, tag, kwargs = ref_data[j]
+ # k += base
+ # assert k < end_relative
+ # if l != 1:
+ # value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
+ # end_relative = max(k + 1, end_relative + 1 - l)
+ # while end_relative > k + 1:
+ # end_relative -= 1
+ # pos1, off1 = value_stack[end_relative]
+ # value_stack[end_relative] = (
+ # element.to_end_relative(root, pos1, off1)
+ # )
+ # pos0, off0 = value_stack[k]
+ # pos1, off1 = value_stack[k + 1]
+ # work.apply_markup(
+ # root,
+ # pos0,
+ # off0,
+ # pos1,
+ # off1,
+ # factory,
+ # tag,
+ # **kwargs
+ # )
+ # if end_relative < len(value_stack):
+ # pos, off = value_stack[-1]
+ # pos, off = element.to_start_relative(root, pos, off)
+ # text = element.get_text(root, pos)
+ # del value_stack[base + 1:]
+ # del state_stack[base + 1:]
+ # if reduce == 0:
+ # assert base == 0
+ # return
+ # state = self.states[state_stack[-1]][3][
+ # bisect.bisect_right(self.states[state_stack[-1]][2], reduce)
+ # ]
+ # assert state != -1
- def yyparse(self, root, pos, off, factory, yylex_iter):
- if pos < 0:
- pos, off = element.to_start_relative(root, pos, off)
+ #def yyparse(self, root, pos, off, factory, yylex_iter):
+ # if pos < 0:
+ # pos, off = element.to_start_relative(root, pos, off)
- state = 0
- value_stack = []
- state_stack = []
- try:
- end_pos, end_off, lookahead_character = next(yylex_iter)
- except StopIteration:
- lookahead_character = self.eof_terminal
- end_pos, end_off = element.to_end_relative(root, pos, off)
- while True:
- value_stack.append((pos, off))
- state_stack.append(state)
- action = self.states[state][1][
- bisect.bisect_right(self.states[state][0], lookahead_character)
- ]
- #print('lookahead_character', lookahead_character, 'action', action)
- if action == -1:
- raise Exception(
- 'syntax error at {0:d},{1:d}: {2:d}'.format(pos, off, lookahead_character)
- )
- if (action & 1) == 0:
- state = action >> 1
- pos, off = element.to_start_relative(root, end_pos, end_off)
- try:
- end_pos, end_off, lookahead_character = next(yylex_iter)
- except StopIteration:
- lookahead_character = self.eof_terminal
- #end_pos, end_off = element.to_end_relative(root, pos, off)
- else:
- reduce = action >> 1
- len_symbols, group_bounds = self.productions[reduce]
- base = len(value_stack) - len_symbols - 1
- end_relative = len(value_stack)
- for j in range(len(group_bounds) - 1, -1, -1):
- k, l, tag, kwargs = group_bounds[j]
- k += base
- assert k < end_relative
- if l != 1:
- value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
- end_relative = max(k + 1, end_relative + 1 - l)
- while end_relative > k + 1:
- end_relative -= 1
- pos1, off1 = value_stack[end_relative]
- value_stack[end_relative] = (
- element.to_end_relative(root, pos1, off1)
- )
- pos0, off0 = value_stack[k]
- pos1, off1 = value_stack[k + 1]
- work.apply_markup(
- root,
- pos0,
- off0,
- pos1,
- off1,
- factory,
- tag,
- **kwargs
- )
- if end_relative < len(value_stack):
- pos, off = value_stack[-1]
- pos, off = element.to_start_relative(root, pos, off)
- del value_stack[base + 1:]
- del state_stack[base + 1:]
- if reduce == 0:
- assert base == 0
- return
- state = self.states[state_stack[-1]][3][
- bisect.bisect_right(self.states[state_stack[-1]][2], reduce)
- ]
- assert state != -1
+ # state = 0
+ # value_stack = []
+ # state_stack = []
+ # try:
+ # end_pos, end_off, lookahead_character = next(yylex_iter)
+ # except StopIteration:
+ # lookahead_character = self.eof_terminal
+ # end_pos, end_off = element.to_end_relative(root, pos, off)
+ # while True:
+ # value_stack.append((pos, off))
+ # state_stack.append(state)
+ # action = self.states[state][1][
+ # bisect.bisect_right(self.states[state][0], lookahead_character)
+ # ]
+ # #print('lookahead_character', lookahead_character, 'action', action)
+ # if action == -1:
+ # raise Exception(
+ # 'syntax error at {0:d},{1:d}: {2:d}'.format(pos, off, lookahead_character)
+ # )
+ # if (action & 1) == 0:
+ # state = action >> 1
+ # pos, off = element.to_start_relative(root, end_pos, end_off)
+ # try:
+ # end_pos, end_off, lookahead_character = next(yylex_iter)
+ # except StopIteration:
+ # lookahead_character = self.eof_terminal
+ # #end_pos, end_off = element.to_end_relative(root, pos, off)
+ # else:
+ # reduce = action >> 1
+ # len_symbols, ref_data = self.productions[reduce]
+ # base = len(value_stack) - len_symbols - 1
+ # end_relative = len(value_stack)
+ # for j in range(len(ref_data) - 1, -1, -1):
+ # k, l, tag, kwargs = ref_data[j]
+ # k += base
+ # assert k < end_relative
+ # if l != 1:
+ # value_stack[k + 1:k + l + 1] = [value_stack[k + l]]
+ # end_relative = max(k + 1, end_relative + 1 - l)
+ # while end_relative > k + 1:
+ # end_relative -= 1
+ # pos1, off1 = value_stack[end_relative]
+ # value_stack[end_relative] = (
+ # element.to_end_relative(root, pos1, off1)
+ # )
+ # pos0, off0 = value_stack[k]
+ # pos1, off1 = value_stack[k + 1]
+ # work.apply_markup(
+ # root,
+ # pos0,
+ # off0,
+ # pos1,
+ # off1,
+ # factory,
+ # tag,
+ # **kwargs
+ # )
+ # if end_relative < len(value_stack):
+ # pos, off = value_stack[-1]
+ # pos, off = element.to_start_relative(root, pos, off)
+ # del value_stack[base + 1:]
+ # del state_stack[base + 1:]
+ # if reduce == 0:
+ # assert base == 0
+ # return
+ # state = self.states[state_stack[-1]][3][
+ # bisect.bisect_right(self.states[state_stack[-1]][2], reduce)
+ # ]
+ # assert state != -1
def __repr__(self):
return 'lr1dfa.LR1DFA({0:s}, {1:s}, {2:d}, {3:d})'.format(