GithubHelp home page GithubHelp logo

ebnf-parser's Introduction

ebnf-parser

A parser for BNF and EBNF grammars used by jison.

install

npm install ebnf-parser

build

To build the parser yourself, clone the git repo then run:

make

This will generate parser.js, which is required by ebnf-parser.js.

usage

The parser translates a string grammar or JSON grammar into a JSON grammar that jison can use (ENBF is transformed into BNF).

var ebnfParser = require('ebnf-parser');

// parse a bnf or ebnf string grammar
ebnfParser.parse("%start ... %");

// transform an ebnf JSON gramamr
ebnfParser.transform({"ebnf": ...});

example grammar

The parser can parse its own BNF grammar, shown below:

%start spec

/* grammar for parsing jison grammar files */

%{
var transform = require('./ebnf-transform').transform;
var ebnf = false;
%}

%%

spec
    : declaration_list '%%' grammar optional_end_block EOF
        {$$ = $1; return extend($$, $3);}
    | declaration_list '%%' grammar '%%' CODE EOF
        {$$ = $1; yy.addDeclaration($$,{include:$5}); return extend($$, $3);}
    ;

optional_end_block
    :
    | '%%'
    ;

declaration_list
    : declaration_list declaration
        {$$ = $1; yy.addDeclaration($$, $2);}
    |
        {$$ = {};}
    ;

declaration
    : START id
        {$$ = {start: $2};}
    | LEX_BLOCK
        {$$ = {lex: $1};}
    | operator
        {$$ = {operator: $1};}
    | ACTION
        {$$ = {include: $1};}
    ;

operator
    : associativity token_list
        {$$ = [$1]; $$.push.apply($$, $2);}
    ;

associativity
    : LEFT
        {$$ = 'left';}
    | RIGHT
        {$$ = 'right';}
    | NONASSOC
        {$$ = 'nonassoc';}
    ;

token_list
    : token_list symbol
        {$$ = $1; $$.push($2);}
    | symbol
        {$$ = [$1];}
    ;

grammar
    : production_list
        {$$ = $1;}
    ;

production_list
    : production_list production
        {$$ = $1;
          if($2[0] in $$) $$[$2[0]] = $$[$2[0]].concat($2[1]);
          else  $$[$2[0]] = $2[1];}
    | production
        {$$ = {}; $$[$1[0]] = $1[1];}
    ;

production
    : id ':' handle_list ';'
        {$$ = [$1, $3];}
    ;

handle_list
    : handle_list '|' handle_action
        {$$ = $1; $$.push($3);}
    | handle_action
        {$$ = [$1];}
    ;

handle_action
    : handle prec action
        {$$ = [($1.length ? $1.join(' ') : '')];
            if($3) $$.push($3);
            if($2) $$.push($2);
            if ($$.length === 1) $$ = $$[0];
        }
    ;

handle
    : handle expression_suffix
        {$$ = $1; $$.push($2)}
    |
        {$$ = [];}
    ;

handle_sublist
    : handle_sublist '|' handle
        {$$ = $1; $$.push($3.join(' '));}
    | handle
        {$$ = [$1.join(' ')];}
    ;

expression_suffix
    : expression suffix
        {$$ = $expression + $suffix; }
    ;

expression
    : ID
        {$$ = $1; }
    | STRING
        {$$ = ebnf ? "'"+$1+"'" : $1; }
    | '(' handle_sublist ')'
        {$$ = '(' + $handle_sublist.join(' | ') + ')'; }
    ;

suffix
    : {$$ = ''}
    | '*'
    | '?'
    | '+'
    ;

prec
    : PREC symbol
        {$$ = {prec: $2};}
    |
        {$$ = null;}
    ;

symbol
    : id
        {$$ = $1;}
    | STRING
        {$$ = yytext;}
    ;

id
    : ID
        {$$ = yytext;}
    ;

action
    : '{' action_body '}'
        {$$ = $2;}
    | ACTION
        {$$ = $1;}
    | ARROW_ACTION
        {$$ = '$$ ='+$1+';';}
    |
        {$$ = '';}
    ;

action_body
    :
        {$$ = '';}
    | ACTION_BODY
        {$$ = yytext;}
    | action_body '{' action_body '}' ACTION_BODY
        {$$ = $1+$2+$3+$4+$5;}
    | action_body '{' action_body '}'
        {$$ = $1+$2+$3+$4;}
    ;

%%

// transform ebnf to bnf if necessary
function extend (json, grammar) {
    json.bnf = ebnf ? transform(grammar) : grammar;
    return json;
}

license

MIT

ebnf-parser's People

Contributors

cdibbs avatar gerhobbelt avatar redchair123 avatar zaach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ebnf-parser's Issues

named references test failures

Hey @cdibbs, I'm getting tests failures on the new feature. Were these passing for you? Are you using jison 0.4.10?

    test named group ()
      ⚡ Error: Parse error on line 1:
      one, two
      ^
      Expecting 'word[alice]', got 'word'
          at typal_constructor.parseError (/Users/zcarter/repos/ebnf-parser/node_modules/jison/lib/jison.js:1185:15)
          at typal_constructor.parse (/Users/zcarter/repos/ebnf-parser/node_modules/jison/lib/jison.js:1301:22)
          at /Users/zcarter/repos/ebnf-parser/tests/ebnf.js:62:39
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:29:20)
          at next (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:69:7)
          at Object.end (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:25:7)
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:41:28)
          at next (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:69:7)
          at Object.end (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:25:7)
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:41:28)
    test named option (?)
      ⚡ Error: Parse error on line 1:
      oneor two
      ^
      Expecting 'word[alex]', got 'word'
          at typal_constructor.parseError (/Users/zcarter/repos/ebnf-parser/node_modules/jison/lib/jison.js:1185:15)
          at typal_constructor.parse (/Users/zcarter/repos/ebnf-parser/node_modules/jison/lib/jison.js:1301:22)
          at /Users/zcarter/repos/ebnf-parser/tests/ebnf.js:62:39
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:29:20)
          at next (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:69:7)
          at Object.end (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:25:7)
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:46:14)
          at next (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:69:7)
          at Object.end (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:25:7)
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:41:28)
    test named complex expression (())
      ⚡ Error: Parse error on line 1:
      one two three four, 
      ^
      Expecting 'word[alpha]', got 'word'
          at typal_constructor.parseError (/Users/zcarter/repos/ebnf-parser/node_modules/jison/lib/jison.js:1185:15)
          at typal_constructor.parse (/Users/zcarter/repos/ebnf-parser/node_modules/jison/lib/jison.js:1301:22)
          at /Users/zcarter/repos/ebnf-parser/tests/ebnf.js:62:39
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:29:20)
          at next (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:69:7)
          at Object.end (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:25:7)
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:46:14)
          at next (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:69:7)
          at Object.end (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:25:7)
          at test (/Users/zcarter/repos/ebnf-parser/node_modules/test/test.js:46:14)

Error: Cannot find module 'lex-parser'

lex-parser should be declared as a dependency instead of a devDependency in package.json.

Since lex-parser is currently declared as a devDependency, it's not being installed when I run npm install --save ebnf-parser.

Consequently, I am getting the following error when I require("ebnf-parser") in my code:

Error: Cannot find module 'lex-parser'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
    at Function.Module._load (internal/modules/cjs/loader.js:529:25)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/kyle/tyson/node_modules/ebnf-parser/ebnf-parser.js:3:16)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)

Proposed solution

Declare lex-parser as a dependency instead of a devDependency in package.json.

Create LICENSE file

Would you please create a license file with your copyright information and the full text of the MIT license? The legal language in the MIT license requires that the text of the license accompany the source code.

please add tags for releases

Currently there is no tags in this repo. Adding tags help those who want to use the github repo directly like debian, rather than via npm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.