grammar.py: Add support for --start-symbols

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2017-11-04 11:05:39 +01:00
commit c7ecfbff3a

View file

@ -1117,6 +1117,8 @@ def grammar_create_y(grammar, opts):
#include <vector> #include <vector>
#include <string> #include <string>
#include <slog.h>
""") """)
for f in opts['includes']: for f in opts['includes']:
@ -1210,6 +1212,9 @@ def grammar_create_y(grammar, opts):
%param { struct context *context } { void *scanner } %param { struct context *context } { void *scanner }
""") """)
if opts['start'] is not None:
out += "%start " + opts['start']
# productions # productions
out += '\n%%\n\n' out += '\n%%\n\n'
for t, p in grammar.iteritems(): for t, p in grammar.iteritems():
@ -1240,6 +1245,7 @@ def grammar_create_y(grammar, opts):
else: else:
out += indent + "| " + format_yacc_rule(rule) + "\n" out += indent + "| " + format_yacc_rule(rule) + "\n"
out += indent + "{" + "\n" out += indent + "{" + "\n"
out += indent + "\t" + 'slog(PRI_NOTICE, "stack size = %d, %d / %d, %d", yyssp - &yyssa[0], yyss - &yyssa[0], yyvsp - &yyvsa[0], yyvs - &yyvsa[0]);\n'
out += indent + "\t" + "$$ = new " + opts['namespace'] + '::' + t + ";\n" out += indent + "\t" + "$$ = new " + opts['namespace'] + '::' + t + ";\n"
out += indent + "\t" + "$$->type = " + opts['namespace'] + '::' + t + "::t_" + str(n_rule) + ";\n" out += indent + "\t" + "$$->type = " + opts['namespace'] + '::' + t + "::t_" + str(n_rule) + ";\n"
tokens = [] tokens = []
@ -1423,7 +1429,7 @@ def grammar_create_l(grammar, opts):
out += textwrap.dedent("""\ out += textwrap.dedent("""\
. { . {
slog(PRI_NOTICE, "returning character %c", yytext[0]); slog(PRI_NOTICE, "returning character '%c'", yytext[0]);
return yytext[0]; return yytext[0];
} }
@ -1644,6 +1650,7 @@ class GrammarCmd(jwutils.Cmd):
p.add_argument('-t', '--trim-symbols', help='trim grammar tree at symbol', nargs='?', default='') p.add_argument('-t', '--trim-symbols', help='trim grammar tree at symbol', nargs='?', default='')
p.add_argument('-r', '--irrelevant-symbols', help='exclude symbol from output payload', nargs='?', default='') p.add_argument('-r', '--irrelevant-symbols', help='exclude symbol from output payload', nargs='?', default='')
p.add_argument('-c', '--cut-symbols', help='cut grammar tree at symbol', nargs='?', default='') p.add_argument('-c', '--cut-symbols', help='cut grammar tree at symbol', nargs='?', default='')
p.add_argument('-s', '--start-symbols', help='use start-symbols', nargs='?', default=None)
p.add_argument('-f', '--config-file', help='config file', nargs='?', default=None) p.add_argument('-f', '--config-file', help='config file', nargs='?', default=None)
return p return p
@ -1737,7 +1744,8 @@ class CmdCreate(DerivedGrammarCmd):
"namespace" : args.namespace, "namespace" : args.namespace,
"includes" : includes, "includes" : includes,
"mip" : mip, "mip" : mip,
"config" : config "config" : config,
"start" : args.start_symbols
} }
cmd = getattr(sys.modules[__name__], 'grammar_create_' + ext) cmd = getattr(sys.modules[__name__], 'grammar_create_' + ext)