{"name":"ebnf-parser","version":"0.1.4","description":"A parser for BNF and EBNF grammars used by jison","main":"ebnf-parser.js","scripts":{"test":"make test"},"repository":"","keywords":["bnf","ebnf","grammar","parser","jison"],"author":{"name":"Zach Carter"},"license":"MIT","devDependencies":{"jison":"0.4.x","lex-parser":"0.1.0","test":"*"},"_id":"ebnf-parser@0.1.4","dist":{"shasum":"0f117e766230b13c4066bbe118f50156f7d307c0","tarball":"http://registry-npm.ywwl.com/ebnf-parser/download/ebnf-parser-0.1.4.tgz","integrity":"sha512-WwRRc1oqxVJQE4ES5eNLfu8qh7aaAoTnp5+OBetZmKUKlEgPYTDbX7vhjzjpzph1oxLGbhFk/1hHJAYwhM5B/A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDfJxZo66jKwElZSbBguofK78Ff+91st3kValROuRHa9QIgHfQ5mGQ6YRBAqBf2uTneJw0PLE1Cp4oS5pd+b7T7uss="}],"size":14904,"noattachment":false,"key":"/ebnf-parser/-/ebnf-parser-0.1.4.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"zaach","email":"zack.carter@gmail.com"},"maintainers":[{"name":"zaach","email":"zack.carter@gmail.com"}],"directories":{},"readme":"# ebnf-parser\n\nA parser for BNF and EBNF grammars used by jison.\n\n## install\n\n    npm install ebnf-parser\n\n\n## build\n\nTo build the parser yourself, clone the git repo then run:\n\n    make\n\nThis will generate `parser.js`, which is required by `ebnf-parser.js`.\n\n## usage\n\nThe parser translates a string grammar or JSON grammar into a JSON grammar that jison can use (ENBF is transformed into BNF).\n\n    var ebnfParser = require('ebnf-parser');\n\n    // parse a bnf or ebnf string grammar\n    ebnfParser.parse(\"%start ... %\");\n\n    // transform an ebnf JSON gramamr\n    ebnfParser.transform({\"ebnf\": ...});\n\n\n## example grammar\n\nThe parser can parse its own BNF grammar, shown below:\n\n    %start spec\n\n    /* grammar for parsing jison grammar files */\n\n    %{\n    var transform = require('./ebnf-transform').transform;\n    var ebnf = false;\n    %}\n\n    %%\n\n    spec\n        : declaration_list '%%' grammar optional_end_block EOF\n            {$$ = $1; return extend($$, $3);}\n        | declaration_list '%%' grammar '%%' CODE EOF\n            {$$ = $1; yy.addDeclaration($$,{include:$5}); return extend($$, $3);}\n        ;\n\n    optional_end_block\n        :\n        | '%%'\n        ;\n\n    declaration_list\n        : declaration_list declaration\n            {$$ = $1; yy.addDeclaration($$, $2);}\n        |\n            {$$ = {};}\n        ;\n\n    declaration\n        : START id\n            {$$ = {start: $2};}\n        | LEX_BLOCK\n            {$$ = {lex: $1};}\n        | operator\n            {$$ = {operator: $1};}\n        | ACTION\n            {$$ = {include: $1};}\n        ;\n\n    operator\n        : associativity token_list\n            {$$ = [$1]; $$.push.apply($$, $2);}\n        ;\n\n    associativity\n        : LEFT\n            {$$ = 'left';}\n        | RIGHT\n            {$$ = 'right';}\n        | NONASSOC\n            {$$ = 'nonassoc';}\n        ;\n\n    token_list\n        : token_list symbol\n            {$$ = $1; $$.push($2);}\n        | symbol\n            {$$ = [$1];}\n        ;\n\n    grammar\n        : production_list\n            {$$ = $1;}\n        ;\n\n    production_list\n        : production_list production\n            {$$ = $1;\n              if($2[0] in $$) $$[$2[0]] = $$[$2[0]].concat($2[1]);\n              else  $$[$2[0]] = $2[1];}\n        | production\n            {$$ = {}; $$[$1[0]] = $1[1];}\n        ;\n\n    production\n        : id ':' handle_list ';'\n            {$$ = [$1, $3];}\n        ;\n\n    handle_list\n        : handle_list '|' handle_action\n            {$$ = $1; $$.push($3);}\n        | handle_action\n            {$$ = [$1];}\n        ;\n\n    handle_action\n        : handle prec action\n            {$$ = [($1.length ? $1.join(' ') : '')];\n                if($3) $$.push($3);\n                if($2) $$.push($2);\n                if ($$.length === 1) $$ = $$[0];\n            }\n        ;\n\n    handle\n        : handle expression_suffix\n            {$$ = $1; $$.push($2)}\n        |\n            {$$ = [];}\n        ;\n\n    handle_sublist\n        : handle_sublist '|' handle\n            {$$ = $1; $$.push($3.join(' '));}\n        | handle\n            {$$ = [$1.join(' ')];}\n        ;\n\n    expression_suffix\n        : expression suffix\n            {$$ = $expression + $suffix; }\n        ;\n\n    expression\n        : ID\n            {$$ = $1; }\n        | STRING\n            {$$ = ebnf ? \"'\"+$1+\"'\" : $1; }\n        | '(' handle_sublist ')'\n            {$$ = '(' + $handle_sublist.join(' | ') + ')'; }\n        ;\n\n    suffix\n        : {$$ = ''}\n        | '*'\n        | '?'\n        | '+'\n        ;\n\n    prec\n        : PREC symbol\n            {$$ = {prec: $2};}\n        |\n            {$$ = null;}\n        ;\n\n    symbol\n        : id\n            {$$ = $1;}\n        | STRING\n            {$$ = yytext;}\n        ;\n\n    id\n        : ID\n            {$$ = yytext;}\n        ;\n\n    action\n        : '{' action_body '}'\n            {$$ = $2;}\n        | ACTION\n            {$$ = $1;}\n        | ARROW_ACTION\n            {$$ = '$$ ='+$1+';';}\n        |\n            {$$ = '';}\n        ;\n\n    action_body\n        :\n            {$$ = '';}\n        | ACTION_BODY\n            {$$ = yytext;}\n        | action_body '{' action_body '}' ACTION_BODY\n            {$$ = $1+$2+$3+$4+$5;}\n        | action_body '{' action_body '}'\n            {$$ = $1+$2+$3+$4;}\n        ;\n\n    %%\n\n    // transform ebnf to bnf if necessary\n    function extend (json, grammar) {\n        json.bnf = ebnf ? transform(grammar) : grammar;\n        return json;\n    }\n\n## license\n\nMIT\n","publish_time":1371926796817,"_cnpm_publish_time":1371926796817,"dist-tags":{"latest":"0.1.10"}}