From fee94deb48de9ab6d1c341e2d1790b9dda5d560d Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sun, 29 Oct 2017 14:18:52 +0100 Subject: [PATCH] grammar.py: Don't create productions for special sequences Signed-off-by: Jan Lindemann --- tools/python/jwutils/grammar.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tools/python/jwutils/grammar.py b/tools/python/jwutils/grammar.py index 1c84b2c..197c9c9 100644 --- a/tools/python/jwutils/grammar.py +++ b/tools/python/jwutils/grammar.py @@ -177,6 +177,9 @@ def format_ebnf_rule(grammar, rule): def format_yacc_rule(rule): r = '' for c in rule: + if c.tp != t_target_lang: + slog(DEBUG, "ignoring non-target-language token", c.token, "in rule") + continue r += tok2sym(c.token) + ' ' return r[:-1] @@ -516,6 +519,7 @@ def grammar_parse_ebnf_tokens(tokens): grammar_add_symbol(grammar, s, None) grammar[s].set_type(p_terminal) for s in specials: + slog(INFO, "found special sequence symbol", s) grammar_add_symbol(grammar, s, None) grammar[s].set_type(p_special) @@ -1123,14 +1127,20 @@ def create_yacc(grammar): out += '\n' for t, p in grammar.iteritems(): if p.tp == p_terminal: - #out += '%token ' + p.sym + (20 - len(p.sym)) * ' ' + '/* ' + t + ' */' +'\n' + out += '%token <' + p.sym + '> ' + p.sym + (20 - len(p.sym)) * ' ' + '/* ' + t + ' */' +'\n' + + # special tokens + out += '\n' + for t, p in grammar.iteritems(): + if p.tp == p_special: + if p.token == '?': # TODO: why is this among the symbols anyway? + continue out += '%token <' + p.sym + '> ' + p.sym + (20 - len(p.sym)) * ' ' + '/* ' + t + ' */' +'\n' # regex tokens out += '\n' for t, p in grammar.iteritems(): if p.tp == p_literal: - #out += '%token ' + p.sym + (20 - len(p.sym)) * ' ' + '/* ' + t + ' */' +'\n' out += '%token <' + p.sym + '> ' + p.sym + (20 - len(p.sym)) * ' ' + '/* ' + t + ' */' +'\n' # types @@ -1149,10 +1159,15 @@ def create_yacc(grammar): # productions out += '\n%%\n\n' for t, p in grammar.iteritems(): + if not len(p.rules): continue - if p.term is not None: + if p.tp == p_terminal: continue + if p.tp == p_special: + continue + slog(INFO, "creating production for symbol", p.str()) + #if p.is_lexical_element is True: # continue if len(p.rules) == 0: