bnfc
bnfc copied to clipboard
Generalized semantic dummies
Status quo
As section 4.2 of the manual describes, LBNF allows for semantic dummies like
_ . Statement ::= Statement ";";
But that seems to only work if the right-hand side of the rule (1) only contains one non-terminal (category) and if it (2) matches the left-hand side non-terminal.
Is this intentional, because I'm getting the following error message:
Bad coercion in rule _. Identifier ::= Ident
Generalized dummies
Idea
I'd like to be able to write something like
_ . Designator ::= Identifier ;
FooL . Foo ::= Identifier;
BarL . Bar ::= Designator;
and want singleton rules to be propagated into the appropriate spots so that I'm left with
FooL . Foo ::= Identifier;
BarL . Bar ::= Identifier;
I can't mathematically prove if right now, but I'm pretty sure that every rule
_ . Rule_k ::= { Non_Terminal_i };
can be propagated simply by replacing all occurrences of Rule_k
in the rest of the grammar. Finally, Rule_k
itself would have to vanish of course.
Why?
Some grammars use dummy rules extensively in order to become more readable. While one certainly could find lots of arguments against the usefulness of such practice, VHDL's grammar is a good example that contains those pieces of BNF. For that reason, generalized dummies would help to reduce the amount of necessary changes to the original grammar in order to become translatable.
What do you think about this idea?
It seems that what you are describing is like some sort of text-substitution macro. I.e. replace every occurence of Designator
by Identifier
in the source file? If so, you should be able to implement it as a preprocessor, or even use an existing one (m4?)
But it seems that what you want to do is to be able to reuse a grammar written in a different formalism, maybe ebnf? In that case an other approach could be to write a tool to convert ebnf grammars to lbnf.
But it seems that what you want to do is to be able to reuse a grammar written in a different formalism, maybe ebnf?
Yes, I already stated that at the Why?
part of my previous post. The reason why I suggested an additional feature for BNFC is that most grammars written in EBNF don't necessarily stick to a concrete grammar for EBNF. There are lots of derivations of the according grammar with VHDL being a popular example. Hence, I don't think that it's feasible to create a single tool that can convert any EBNF-like grammar into LBNF. I rather think that this will always be a manual task (at least for older languages like the aforementioned VHDL). So my proposition is: Let's make this manual task as easy as possible.
The question is: Would you accept a PR for this feature?
Furthermore, I think that using a macro processor for this task would be considered a crime in some parts of the world :)
The question is: Would you accept a PR for this feature?
Yes but with a different syntax that makes it clear that it's a macro and not a rule. I suggest maybe using a new keyword like macro
or alias
? That would give you something like this:
macro Designator = Identifier ;
BarL . Bar ::= Designator;