1

In my current project, I have written grammar in Xtext with nice functionalities. For instance, code snippet of my grammar

Device:
     deviceName =  ID ':'

     ('region' ':' ( deviceRegions += DeviceRegions)+ )* ;

DeviceRegions:
     regionLabel = [RegionLabel] ';'  
     // It stores a List of  regionLabel functionalities
;

RegionLabel: name = ID ;

Using the above grammar, I write the following high-level specification:

 DeviceOne : 
         region : 
              Room ;
              Floor ;
              Building; 
    DeviceTwo: 
         region : 
              Room ;
              Floor ;
              Building;

I would like to see an equivalent BNF grammar of grammar written in xText. The Equivallent grammar would be the following for instance:

Device = ID ':'
     ( 'region' ':'  (deviceRegions = DeviceRegions)+)*  ;

 DeviceRegions : 
       regionLabel = RegionLabel ';'  ;

 RegionLabel = 'room' | 'Floor' | 'Building' ;

  ID  = 'A'..'Z' ('a' ..'z' | 'A' ..'Z')* ;

My question is that "Is there any way to convert xText written grammar into equivaluent BNF grammar or Should I do it manually ?

I know that xText grammar is very easy to learn and write. However, I have a requirement of having BNF grammar.

user-517752
  • 1,188
  • 5
  • 21
  • 54

2 Answers2

3

Need to do the same for documentation (visualization of xText grammar using railroad diagrams), first time did by hand, but as the grammar evolves it becomes boring, found two useful articles: Simple solution - http://fornax-sculptor.blogspot.nl/2010/05/generating-syntaxrailroad-diagrams-from.html A more solid one http://xtexterience.wordpress.com/2011/05/13/an-ebnf-grammar-in-xtext/

Albatros
  • 46
  • 1
2

Please note that it is not possible to produce an 'equivalent' EBNF from an Xtext grammar. Xtext grammars support the notion of cross references where you do not reference production rules but produced instances. This cannot be expressed in EBNF. Anyway, it's possible to write a generator fragment that produces output from an Xtext grammar, e.g. the Antlr grammar is created that way. Have a look at the docs to learn more about that.

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
  • More info about Antlr to EBNF in http://stackoverflow.com/questions/9921850/generate-ebnf-from-antlr – fikovnik Apr 20 '13 at 09:09