recursion - Grammar transformation for recursive descent -
i trying transform given grammar in order ready recursive descent. rules end are:
seq --> constseq | operz z --> exprx | operx y --> exprx | operx x --> expr | exproperx | ε .
what supposed z , y same?
is of them eliminated?
a bit hard without knowing software system used (and intelligence).
if on left hand side non-terminals must unique (no or'ed rules y --> exprx
, y --> operx
), purpose metainformation, maybe different semantic interpretation of generated ast.
but yes, fine possibility loop reducing grammar. on other hand produced function y call z.
(mind exprx | operx
might same operx | exprx
depending on specific grammar's symbols or type ll(1)
.)
also a --> x b
, b -> x a
redundant.
one need loop on rules till no reduction possible.
boolean checkrulesarethesame(rule rule1, rule rule2, context context) { context.setnonterminalsthesame(true, rule1.nonterminal, rule2.nonterminal); if (productionsthesame(rule1.production, rule2.production, context)) { return true; } context.setnonterminalsthesame(false, rule1.nonterminal, rule2.nonterminal); return false; }
the rarer case a --> x b
, b -> x c
, c -> x a
not found comparison of 2 non-terminals.