プログラミング + アカデミック + 何か面白いこと

  1. Programming
  2. 197 view

[Parser][PEG]Try to use PEG.js for rich config file

Lately, I’m interested in PEG(Parsing Expression Grammar) which can treat
wider grammar than CFG(Context Free Grammar).
Then, I make new language for config file with it.

PEG.js which generates parser for PEG is published on github.
I use it.
http://pegjs.majda.cz/

The language’s grammar which I make is like following.

start
= tree

tree
= stat:statement? { return stat; }

statement
= stat:if_statement { return stat.toString(); }
/ stat:proc_statement { return stat.toString(); }

if_statement
= fac:if_factor 'Y' edge branch1:proc_statement '\n'
'N' '\n' edge branch2:statement
{ return branch2.toString().match(/if/) ?
"if (" + fac + ")\n\t" + branch1 + '\nelse ' + branch2
: 'if (' + fac + ')\n\t' + branch1 + '\nelse\n\t' + branch2;}

proc_statement
= fac1:proc_factor [-|]+ fac2:proc_statement { return fac1 + '.then(' + fac2.slice(0,fac2.length-1) + ');'; }
/ factor:proc_factor { return factor + ';'; }

proc_factor
= '[' procexp:expression ']' { return procexp; }
if_factor
= '<' ifexp:expression '>' { return ifexp; }

expression
= elem:element ' ' cdr:arguments { return elem.toString() + '(' +cdr + ')'; }
/ elem:element { return elem.toString(); }

arguments
= elem:element ',' arg:arguments { return elem.toString() + ',' + arg; }
/ elem:element { return elem.toString(); }

element
= characters:[a-zA-Z0-9-_."]+ { return characters.join('') }

edge
= symbols:[-|\n]+ { return symbols.join('') }

The parser can analyze like following sentences.

<hasTwitterAccount id>Y---[getTweet id]---[outputTweets]
N
|
|
<hasYoutubeAccount id>Y-----[youtube_search keyword,30]
N
|
|
|
|
|
[alert "nothing"]

This project is published on github. The link is this.

Programming recent post

  1. Install sbt 1.0.0 and run sample template

  2. [Machine Learning]Created docker image includ…

  3. [Node.js]How to write batch script with Node.…

  4. [Play][Scala]Develop Request Driven Batch Usi…

  5. [OpenCV][Ruby]Auto check web page design corr…

関連記事

PAGE TOP