I have to write a JavaCUP specification, and I've been given an EBNF grammar. However, I don't know how to convert between the two. I've heard the basic ideas, but I don't really understand what I need to change, what would be the "terminals", etc.
Can anyone explain how to convert from one to another, or if there's somewhere where I can read about it?
EBNF grammars are similar to normal BNF, but with some extra features (similar to regular expression operators) as syntax sugar. Since you did not show your grammar, I can only guess at what parts you need to desugar to convert to normal BNF, but here are the most common (for a LALR generator like JavaCUP):
B* becomes Bstar, defined as Bstar ::= epsilon; Bstar ::= Bstar B
B+ becomes Bplus, defined as Bplus ::= B; Bplus ::= Bplus B
B? becomes Bquestion, defined as Bquestion ::= epsilon; Bquestion ::= B
B | C becomes BorC, defined as BorC ::= B; BorC ::= C
The epsilon
identifier here is however your parser generator denotes the empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With