Skip to content

onlyuser/ebnf2yacc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

ebnf2yacc

Copyright (C) 2011-2017 mailto:onlyuser@gmail.com

About

ebnf2yacc is a kleene closure preprocessor for yacc. It takes an ebnf specification as input and generates a yacc grammar.

A Motivating Example

input:

%%

program:
          (expr)+ '\n' {
                for(auto p = $1->begin(); p != $1->end(); p++)
                {
                    printf("%d\n", std::get<0>(*p));
                }
            }
        ;

expr: INTEGER { $$ = $1; };

%%

output:

%{
    #include <vector>
    #include <tuple>
    typedef std::tuple<int> program_term_0_type_t;
    typedef std::vector<program_term_0_type_t> program_recursive_0_type_t;
%}

%union 
{
    program_term_0_type_t* program_term_0_type;
    program_recursive_0_type_t* program_recursive_0_type;
}

%type program_term_0
%type program_recursive_0

%%

program:
      program_recursive_0 '\n' { {
                for(auto p = $1->begin(); p != $1->end(); p++)
                {
                    printf("%d\n", std::get<0>(*p));
                }
            } delete $1;};

program_recursive_0:
      program_term_0 { $$ = new program_recursive_0_type_t; $$->push_back(*$1); delete $1; }
    | program_recursive_0 program_term_0 { $1->push_back(*$2); delete $2; $$ = $1; };

program_term_0:
      expr { $$ = program_term_0_type_t($1); {} };

expr: INTEGER { $$ = $1; };

%%

Usage

cat input.ebnf | ./app/bin/ebnf2yacc -y > output.y

Requirements

Unix tools and 3rd party components (accessible from $PATH):

gcc flex bison valgrind cppcheck doxygen graphviz ticpp

Environment variables:

  • $INCLUDE_PATH_EXTERN -- where "ticpp/ticpp.h" resides
  • $LIB_PATH_EXTERN -- where "libticppd.a" resides

Make Targets

target action
all make binaries
test all + run tests
pure test + use valgrind to check for memory leaks
dot test + generate .png graph for tests
lint use cppcheck to perform static analysis on .cpp files
doc use doxygen to generate documentation
xml test + generate .xml for tests
import test + use ticpp to serialize-to/deserialize-from xml
clean remove all intermediate files

References

"Kleene star"
http://en.wikipedia.org/wiki/Kleene_star

Keywords

Lex, Yacc, Flex, Bison, Parser, EBNF, Kleene Closure, Kleene Star