Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert EBNF Grammar to Context-Free Grammar

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?

like image 977
muttley91 Avatar asked Mar 15 '11 02:03

muttley91


1 Answers

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.

like image 166
Jeremiah Willcock Avatar answered Sep 21 '22 14:09

Jeremiah Willcock