Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract BNF grammar rules from yacc file

I have a yacc file describing a certain language for which i am developing an Editor using IMP (eclipse project).I am using LPG as a parser generator, so I needed to extract a BNF rules from my yacc file. the yacc file i received contains rules and actions. However, I wanted to extract only the rules of the grammar description to be used in LPG. one way of doing this is by manually extracting the rules and reformatting it to BNF syntax (or may be writing a program to convert it to the way i want it). I was wondering if there is an automated way of doing this. I read in some blogs that bison could help but i couldn't exactly find the appropriate commands. does anybody knows how to deal with this problem.

I can't really post the yacc file i have since it is confidential. but i could give an example as follows

argExprList:
       assignExp
          {
            // some rules here 
          }
        | assignExpList ',' assignExp
            {
              //some other rules here
            }
        ;

what i wanted it to be converted to is simply like

argExpList ::= assignExp|assignExpList ',' assignExp
like image 880
lferasu Avatar asked Oct 20 '13 12:10

lferasu


1 Answers

Bison can help, if you're prepared to do some post-processing.

If you run bison with the -v option, it will produce a file called filename.output (where filename is the basename of the .y file); that file contains a copy of the grammar and a description of every state. The grammar does not have actions, and it has one production per line. But you'll need to do some work:

  1. Every production is numbered. You'll need to remove those numbers.

  2. If there are mid-rule actions, they will show up as oddly-named empty non-terminals. The names will be something like $@8 or @2. You'll need to delete these tokens and their corresponding empty productions.

  3. Empty productions are (at least in one recent version of bison) shown as /* empty */. That might not be your preference.

  4. It does not change : to ::=

I've extracted grammars from yacc/bison files in this way, and it's very straight-forward; you can make all the above changes with a simple sed or awk script.

like image 193
rici Avatar answered Oct 24 '22 13:10

rici