Convert EBNF Grammar to Context-Free Grammar

I need to write a JavaCUP specification and I was given EBNF grammar. However, I do not know how to convert between them. I heard the basic ideas, but I don’t quite understand what I need to change, what the β€œterminals” will be, etc.

Can someone explain how to convert from one to another, or somewhere where I can read about it?

+4
source share
1 answer

EBNF grammars are similar to regular BNFs, but with some additional features (similar to regular expression operators) as syntactic sugar. Since you did not show your grammar, I can only guess in what parts you need to convert desugar to regular BNF, but here are the most common ones (for a LALR generator such as 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 id is here, however, your parser generator indicates an empty string.

+4
source

Source: https://habr.com/ru/post/1343707/


All Articles