SlideShare una empresa de Scribd logo
1 de 37
PEG.js
- Javascript Parser Generator -
Hidetomo Suzuki
2013 / 5 / 30
Scope
Page 2
This MTG Target
Understand What’s PEG.js
Know How to Use PEG.js
Make New DSL and Parser
2013 / 5 / 30
Agenda
Page 3
Table of Contents
1.How to make parser
2. What’s PEG.js
3. Try to used to be PEG
4. Let’s make DSL
5.Applications
2013 / 5 / 30
Agenda
Page 4
Table of Contents
1.How to make parser
2. What’s PEG.js
3. Try to used to be PEG
4. Let’s make DSL
5.Applications
2013 / 5 / 30 Page 5
1. How to make parser
1. Lexical Analysis(字句解析)
2. Syntactic Parsing(構文解析)
3. Semantic Analysis(意味解析)
4. Intermidiate Code Generation(中間コード生成)
5. Code Optimization(コード最適化)
6. Code Generation(コード生成)
General Step of Processing with Compiler
Parser
2013 / 5 / 30 Page 6
1. How to make parser
1. Lexical Analysis(字句解析)
2. Syntactic Parsing(構文解析)
3. Semantic Analysis(意味解析)
Analysis for Make Parser
Make strings(minimum unit of string has semantic) from characters
Make tree structure from strings which result of Lexical Analysis
Type Check
※ A string of Lexical Analysis is called “Token”.
2013 / 5 / 30 Page 7
1. How to make parser
1. Lexical Analysis
position_x = position_x + 2.0 * time
 Identifier : position_x
 Substitution Symbol : =
 Identifier : position_x
 Addition Symbol : +
 Number : 2.0
 Multiple Symbol : *
 Identifier : time
2013 / 5 / 30 Page 8
1. How to make parser
2. Syntactic Parsing
 Identifier : position_x Substitution Symbol : =
 Identifier : position_x Addition Symbol : +
 Number : 2.0 Multiple Symbol : *
 Identifier : time
Substitution Symbol
Identifier
position_x
Expression
ExpressionExpression
Expression Expression
Identifier
position_x
Number
2.0
Identifier
time
=
+
*
2013 / 5 / 30 Page 9
1. How to make parser
3. Semantic Analysis
Substitution Symbol
Identifier
position_x
Expression
ExpressionExpression
Expression Expression
Identifier
position_x
Number Identifier
=
+
 Correct Case : Real Number * Integer Number
 Wrong Case : Real Number * Function Pointer
2.0 time
*
2013 / 5 / 30
Agenda
Page 10
Table of Contents
1.How to make parser
2. What’s PEG.js
3. Try to used to be PEG
4. Let’s make DSL
5.Applications
2013 / 5 / 30 Here comes
your footer 
2. What‘s PEG.js?
 Parser Generator for JavaScript
 Use PEG
(Parsing Expression Grammar)
 Easy to try with the web page
Outline of PEG.js
http://pegjs.majda.cz/
2013 / 5 / 30 Here comes
your footer 
2. What‘s PEG.js?
PEG(Parsing Expression Grammar) is one of grammar for artificial language.
Focus point is how input will be analyzed.
What‘s PEG
Ex) Simple Expression Grammar which Has Four Arithmetic Operations
expression <- addexp
addexp <- multiexp (“+” multiexp / “-” multiexp)*
multiexp <- number (“*” number / “/” number)*
number <- [0-9]+
Accept : 2*3+6/2-5*3 -> multiexp + multiexp – multiexp -> ・・・
Decline : (1+1)*3+6/2-(7-2)*3 -> ?*3+6/2-?*3 -> can’t recognize…
2013 / 5 / 30 Here comes
your footer 
2. What‘s PEG.js?
• Nothing Confusion
• Different : “a b / a”, “a / a b”
• Easy to use compared with CFG
• Don’t need to implement
scanner (Lexical Analyzer)
• Don’t Express Left Recursive
PEG vs CFG(Context Free Grammar)
• Confusion Existing
• Same : “a b | a”, “a | a b”
• Need to implement scanner
• Can Express Left Recursive
PEG CFG
2013 / 5 / 30 Page 14
2. What‘s PEG.js?
1. Make Grammar with PEG
2. Generate Parser with “pegjs” Command
3. Use the Parser Loaded as a Library
Step of Make Parser with PEG.js
vim filename.pegjs
Pegjs filename.pegjs parser.js
<script src=“./parser.js” type=“text/javascript”></script>
<script type=“text/javascript”>
parser.parse(“something”);
2013 / 5 / 30 Page 15
2. What‘s PEG.js?
Install PEG.js
# Install Pythonbrew
$ curl -kL http://xrl.us/pythonbrewinstall | bash
$ vim ~/.bash_profile
[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc
$ source ~/.bashrc
$ pythonbrew install 2.7.2
$ pythonbrew switch 2.7.2
# Install Node
$ git clone git://github.com/creationix/nvm.git ~/.nvm
$ nvm install v0.8.7
$ source ~/.nvm/nvm.sh
$ nvm use v0.8.7
$ vim ~/.npmrc
$ registry = http://registry.npmjs.org/
2013 / 5 / 30 Page 16
2. What‘s PEG.js?
1. Make Grammar with PEG
2. Generate Parser with “pegjs” Command
3. Use the Parser Loaded as a Library
Let’s use PEG.js a little bit
$ vim ffirst.pegjs
start = addexp
addexp = integer ("+" integer / "-" integer)*
integer = [0-9]+
$ ./node_modules/pegjs/bin/pegjs first.pegjs parser.js
$ vim parser.js
1st line: module.export = … -> var parser = …
$ vim index.html
<script type=“text/javascript” src=“./parser.js”></script>
<script type=“text/javascript”>
alert(parser.parse(“1+2+3-5”));
</script>
2013 / 5 / 30
Agenda
Page 17
Table of Contents
1.How to make parser
2. What’s PEG.js
3. Try to used to be PEG
4. Let’s make DSL
5.Applications
2013 / 5 / 30 Page 18
3.Try to used to be PEG
PEG.js Online Version (http://pegjs.majda.cz/online)
2013 / 5 / 30 Page 19
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : integer)
Grammar
Test Input
Test Input Result
start = integer
integer = digits:[0-9]+ { return parseInt(digits.join(“”), 10); }
123456789
123456789
2013 / 5 / 30 Page 20
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : add two integer)
Grammar
Test Input
Test Input Result
start = target1:integer "+" target2:integer { return target1+target2; }
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
81+19
100
2013 / 5 / 30 Page 21
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : add and minus two integer)
Grammar
Test Input
Test Input Result
start
= target1:integer "+" target2:integer { return target1+target2; }
/ target1:integer "-" target2:integer { return target1-target2; }
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
100-19
81
2013 / 5 / 30 Page 22
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : add and minus integers)
Grammar
Test Input
Test Input Result
start = expression
expression
= ope1:integer "+" ope2:expression { return ope1+ope2; }
/ ope1:integer "-" ope2:expression { return ope1-ope2; }
/ ope:integer { return ope; }
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
1+2+3+4+5-6+7-8+9-10
9
2013 / 5 / 30 Page 23
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : four arithmetic)
Grammar
Test Input
Test Input Result
start = expression
expression
= ope1:integer "+" ope2:expression { return ope1+ope2; }
/ ope1:integer "-" ope2:expression { return ope1-ope2; }
/ ope1:integer "*" ope2:expression { return ope1*ope2; }
/ ope1:integer "/" ope2:expression { return ope1/ope2; }
/ ope:integer { return ope; }
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
2*3/2
3
2013 / 5 / 30 Page 24
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : four arithmetic with priority)
Grammar
Test Input
Test Input Result
start = expression
expression = ope1:multiple "+" ope2:expression { return ope1+ope2; }
/ ope1:multiple "-" ope2:expression { return ope1-ope2; }
/ ope:multiple { return ope; }
multiple = ope1:integer "*" ope2:multiple { return ope1*ope2; }
/ ope1:integer "/" ope2:multiple { return ope1/ope2; }
/ ope:integer { return ope; }
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
2*3/2+8/4
5
2013 / 5 / 30 Page 25
3. Try to used to be PEG
Very Simple PEG.js Grammar(calc : four arithmetic with priority)
Grammar
Test Input
Test Input Result
start = expression
expression = ope1:multiple "+" ope2:expression { return ope1+ope2; }
/ ope1:multiple "-" ope2:expression { return ope1-ope2; }
/ ope:multiple { return ope; }
multiple = ope1:bracket "*" ope2:multiple { return ope1*ope2; }
/ ope1:bracket "/" ope2:multiple { return ope1/ope2; }
/ ope:bracket { return ope; }
bracket = "(" exp:expression ")" {return exp; }
/ ope:integer {return ope; }
integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); }
(7+3)*3/2+8/(6-2)
17
2013 / 5 / 30
Agenda
Page 26
Table of Contents
1.How to make parser
2. What’s PEG.js
3. Try to used to be PEG
4. Let’s make DSL
5.Applications
2013 / 5 / 30 Page 27
4. Let‘s make DSL
Config File Format
Parser Result
Web Page Image
Target
<LogicalExpression?>Y---[function param]
N---[function param]
if (isLogicalExpression) function(param);
else function(param);
Ex)
<Hydea?>Y---[show ‘hydea’]
N---[show ‘hydeb’]
Ex)
if (isHydea) show(‘hydea’);
else show(‘hydeb’);
URL: ex.com/?p=hydea URL: ex.com/?p=hydeb
2013 / 5 / 30 Page 28
4. Let‘s make DSL
Just write over specification
Make Grammar
start = "<" logic "?>Y---[" function " " param
"]nN---[" function " " param "]"
logic = [a-zA-Z]+
function = [a-zA-Z]+
param = "'" [a-zA-Z]+ "'"
2013 / 5 / 30 Page 29
4. Let‘s make DSL
Refactoring a little bit
Make Grammar
start = "<" identifier "?>Y---[" identifier " " param
"]nN---[" identifier " " param "]"
param = "'" identifier "'"
identifier = [a-zA-Z]+
2013 / 5 / 30 Page 30
4. Let‘s make DSL
Finish to make param and identifier part
Make Grammar
start = "<" identifier "?>Y---[" identifier " " param
"]nN---[" identifier " " param "]"
param
= "'" id:identifier "'"
{return "'" + id + "'"; }
identifier
= chars:[a-zA-Z]+
{return chars.join(""); }
2013 / 5 / 30 Page 31
4. Let‘s make DSL
Organize Function Part
Make Grammar
start = "<" identifier "?>Y---[" function
"]nN---[" function "]"
function
= id:identifier " " param:param
{return id + "(" + param + ")"; }
param
= "'" id:identifier "'"
{return "'" + id + "'"; }
identifier
= chars:[a-zA-Z]+
{return chars.join(""); }
2013 / 5 / 30 Page 32
4. Let‘s make DSL
Finish to make
Make Grammar
start
= "<" id:identifier "?>Y---[" f1:function
"]nN---[" f2:function "]"
{return "if (is" + id + ") " + f1 + ";nelse " + f2 + ";"; }
function
= id:identifier " " param:param
{return id + "(" + param + ")"; }
param
= "'" id:identifier "'"
{return "'" + id + "'"; }
identifier
= chars:[a-zA-Z]+
{return chars.join(""); }
2013 / 5 / 30
Agenda
Page 33
Table of Contents
1.How to make parser
2. What’s PEG.js
3. Try to used to be PEG
4. Let’s make DSL
5.Applications
2013 / 5 / 30 Page 34
5. Application
Show My Demo(Demo’s grammar)
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 + ")nt" + branch1 + 'nelse ' + branch2
: 'if (' + fac + ')nt' + branch1 + 'nelsent' + 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('') }
2013 / 5 / 30 Page 35
5. Application
Recommendation
Conf1 Conf2 Conf3 ・・・
Recommendation
Engine
Choose a config file
2013 / 5 / 30
Reference
Page 36
Famous Book for Compiler
2013 / 5 / 30
Thank you
Page 37
Thank you for your listening

Más contenido relacionado

La actualidad más candente

ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...Istanbul Tech Talks
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Guillaume Laforge
 
Eric Lafortune - ProGuard and DexGuard for optimization and protection
Eric Lafortune - ProGuard and DexGuard for optimization and protectionEric Lafortune - ProGuard and DexGuard for optimization and protection
Eric Lafortune - ProGuard and DexGuard for optimization and protectionGuardSquare
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011 Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011 Jonas Brømsø
 
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKEric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKGuardSquare
 
tictactoe groovy
tictactoe groovytictactoe groovy
tictactoe groovyPaul King
 
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策kwatch
 
Thoughts On Learning A New Programming Language
Thoughts On Learning A New Programming LanguageThoughts On Learning A New Programming Language
Thoughts On Learning A New Programming LanguagePatricia Aas
 
2016年のPerl (Long version)
2016年のPerl (Long version)2016年のPerl (Long version)
2016年のPerl (Long version)charsbar
 
C# 6.0 - What?! C# is being updated?
C# 6.0 - What?! C# is being updated?C# 6.0 - What?! C# is being updated?
C# 6.0 - What?! C# is being updated?Filip Ekberg
 
ProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and TricksProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and Tricksnetomi
 
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...Tsundere Chen
 
groovy transforms
groovy transformsgroovy transforms
groovy transformsPaul King
 
Construire son JDK en 10 étapes
Construire son JDK en 10 étapesConstruire son JDK en 10 étapes
Construire son JDK en 10 étapesJosé Paumard
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2José Paumard
 

La actualidad más candente (20)

ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Eric Lafortune - ProGuard and DexGuard for optimization and protection
Eric Lafortune - ProGuard and DexGuard for optimization and protectionEric Lafortune - ProGuard and DexGuard for optimization and protection
Eric Lafortune - ProGuard and DexGuard for optimization and protection
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011 Using Jenkins for Continuous Integration of Perl components OSD2011
Using Jenkins for Continuous Integration of Perl components OSD2011
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
C# Is The Future
C# Is The FutureC# Is The Future
C# Is The Future
 
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKEric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
 
tictactoe groovy
tictactoe groovytictactoe groovy
tictactoe groovy
 
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
What is wrong on Test::More? / Test::Moreが抱える問題点とその解決策
 
Thoughts On Learning A New Programming Language
Thoughts On Learning A New Programming LanguageThoughts On Learning A New Programming Language
Thoughts On Learning A New Programming Language
 
2016年のPerl (Long version)
2016年のPerl (Long version)2016年のPerl (Long version)
2016年のPerl (Long version)
 
C# 6.0 - What?! C# is being updated?
C# 6.0 - What?! C# is being updated?C# 6.0 - What?! C# is being updated?
C# 6.0 - What?! C# is being updated?
 
Golang for OO Programmers
Golang for OO ProgrammersGolang for OO Programmers
Golang for OO Programmers
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
ProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and TricksProGuard / DexGuard Tips and Tricks
ProGuard / DexGuard Tips and Tricks
 
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
 
groovy transforms
groovy transformsgroovy transforms
groovy transforms
 
Construire son JDK en 10 étapes
Construire son JDK en 10 étapesConstruire son JDK en 10 étapes
Construire son JDK en 10 étapes
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 

Destacado

何となく勉強した気分になれるパーサ入門
何となく勉強した気分になれるパーサ入門何となく勉強した気分になれるパーサ入門
何となく勉強した気分になれるパーサ入門masayoshi takahashi
 
20141115_node_school_festival_lt
20141115_node_school_festival_lt20141115_node_school_festival_lt
20141115_node_school_festival_ltzuqqhi 2
 
20140706 zuqqhi2-lsm-v1
20140706 zuqqhi2-lsm-v120140706 zuqqhi2-lsm-v1
20140706 zuqqhi2-lsm-v1zuqqhi 2
 
ANTLR-ANother Tool for Language Recognition
ANTLR-ANother Tool for Language RecognitionANTLR-ANother Tool for Language Recognition
ANTLR-ANother Tool for Language Recognitionelliando dias
 
拡張性のあるPEGパーサの実装
拡張性のあるPEGパーサの実装拡張性のあるPEGパーサの実装
拡張性のあるPEGパーサの実装masato
 
Goで言語処理系(の途中まで)を作ろう
Goで言語処理系(の途中まで)を作ろうGoで言語処理系(の途中まで)を作ろう
Goで言語処理系(の途中まで)を作ろうEsehara Shigeo
 
Racc でおてがる構文解析
Racc でおてがる構文解析Racc でおてがる構文解析
Racc でおてがる構文解析morphine57
 
PEGの回文っぽいExpression
PEGの回文っぽいExpressionPEGの回文っぽいExpression
PEGの回文っぽいExpressionSosuke MORIGUCHI
 
新卒で即戦力なエンジニアになる
新卒で即戦力なエンジニアになる新卒で即戦力なエンジニアになる
新卒で即戦力なエンジニアになるShota Okutsu
 
L-1グランプリ "D言語"
L-1グランプリ "D言語"L-1グランプリ "D言語"
L-1グランプリ "D言語"det coder
 
Parsing Left Recursive PEG
Parsing Left Recursive PEGParsing Left Recursive PEG
Parsing Left Recursive PEGTakayuki Goto
 
"Programming Hive" Reading #1
"Programming Hive" Reading #1"Programming Hive" Reading #1
"Programming Hive" Reading #1moai kids
 
正しいマインドマップの使い方・描き方
正しいマインドマップの使い方・描き方正しいマインドマップの使い方・描き方
正しいマインドマップの使い方・描き方webcampusschoo
 
GoCon 2015 Summer GoのASTをいじくって新しいツールを作る
GoCon 2015 Summer GoのASTをいじくって新しいツールを作るGoCon 2015 Summer GoのASTをいじくって新しいツールを作る
GoCon 2015 Summer GoのASTをいじくって新しいツールを作るMasahiro Wakame
 
競技プログラミング頻出アルゴリズム攻略
競技プログラミング頻出アルゴリズム攻略競技プログラミング頻出アルゴリズム攻略
競技プログラミング頻出アルゴリズム攻略K Moneto
 
青空文庫テキストフォーマットについて (aozorahack)
青空文庫テキストフォーマットについて (aozorahack)青空文庫テキストフォーマットについて (aozorahack)
青空文庫テキストフォーマットについて (aozorahack)masayoshi takahashi
 

Destacado (17)

何となく勉強した気分になれるパーサ入門
何となく勉強した気分になれるパーサ入門何となく勉強した気分になれるパーサ入門
何となく勉強した気分になれるパーサ入門
 
20141115_node_school_festival_lt
20141115_node_school_festival_lt20141115_node_school_festival_lt
20141115_node_school_festival_lt
 
20140706 zuqqhi2-lsm-v1
20140706 zuqqhi2-lsm-v120140706 zuqqhi2-lsm-v1
20140706 zuqqhi2-lsm-v1
 
ANTLR-ANother Tool for Language Recognition
ANTLR-ANother Tool for Language RecognitionANTLR-ANother Tool for Language Recognition
ANTLR-ANother Tool for Language Recognition
 
拡張性のあるPEGパーサの実装
拡張性のあるPEGパーサの実装拡張性のあるPEGパーサの実装
拡張性のあるPEGパーサの実装
 
Goで言語処理系(の途中まで)を作ろう
Goで言語処理系(の途中まで)を作ろうGoで言語処理系(の途中まで)を作ろう
Goで言語処理系(の途中まで)を作ろう
 
Racc でおてがる構文解析
Racc でおてがる構文解析Racc でおてがる構文解析
Racc でおてがる構文解析
 
PEGの回文っぽいExpression
PEGの回文っぽいExpressionPEGの回文っぽいExpression
PEGの回文っぽいExpression
 
新卒で即戦力なエンジニアになる
新卒で即戦力なエンジニアになる新卒で即戦力なエンジニアになる
新卒で即戦力なエンジニアになる
 
L-1グランプリ "D言語"
L-1グランプリ "D言語"L-1グランプリ "D言語"
L-1グランプリ "D言語"
 
Parsing Left Recursive PEG
Parsing Left Recursive PEGParsing Left Recursive PEG
Parsing Left Recursive PEG
 
Introduction to PEG
Introduction to PEGIntroduction to PEG
Introduction to PEG
 
"Programming Hive" Reading #1
"Programming Hive" Reading #1"Programming Hive" Reading #1
"Programming Hive" Reading #1
 
正しいマインドマップの使い方・描き方
正しいマインドマップの使い方・描き方正しいマインドマップの使い方・描き方
正しいマインドマップの使い方・描き方
 
GoCon 2015 Summer GoのASTをいじくって新しいツールを作る
GoCon 2015 Summer GoのASTをいじくって新しいツールを作るGoCon 2015 Summer GoのASTをいじくって新しいツールを作る
GoCon 2015 Summer GoのASTをいじくって新しいツールを作る
 
競技プログラミング頻出アルゴリズム攻略
競技プログラミング頻出アルゴリズム攻略競技プログラミング頻出アルゴリズム攻略
競技プログラミング頻出アルゴリズム攻略
 
青空文庫テキストフォーマットについて (aozorahack)
青空文庫テキストフォーマットについて (aozorahack)青空文庫テキストフォーマットについて (aozorahack)
青空文庫テキストフォーマットについて (aozorahack)
 

Similar a 20130530-PEGjs

Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Wsloffenauer
 
Ecmascript 2015 – best of new features()
Ecmascript 2015 – best of new features()Ecmascript 2015 – best of new features()
Ecmascript 2015 – best of new features()Miłosz Sobczak
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Jonathan Felch
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perlmegakott
 
GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For BeginnersMatt Passell
 
Groovy and Grails talk
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talkdesistartups
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Robert Stern
 
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume LaforgeGroovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume LaforgeGuillaume Laforge
 
Roundarch Isobar Java script coding standards
Roundarch Isobar Java script coding standardsRoundarch Isobar Java script coding standards
Roundarch Isobar Java script coding standardsyoav-netcraft
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Peter Maas
 
Formatting ForThe Masses
Formatting ForThe MassesFormatting ForThe Masses
Formatting ForThe MassesHolger Schill
 
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...Paris Open Source Summit
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
Scripting Oracle Develop 2007
Scripting Oracle Develop 2007Scripting Oracle Develop 2007
Scripting Oracle Develop 2007Tugdual Grall
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesTobias Oetiker
 

Similar a 20130530-PEGjs (20)

Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
Ecmascript 2015 – best of new features()
Ecmascript 2015 – best of new features()Ecmascript 2015 – best of new features()
Ecmascript 2015 – best of new features()
 
Groovy
GroovyGroovy
Groovy
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For Beginners
 
Groovy and Grails talk
Groovy and Grails talkGroovy and Grails talk
Groovy and Grails talk
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume LaforgeGroovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
 
Roundarch Isobar Java script coding standards
Roundarch Isobar Java script coding standardsRoundarch Isobar Java script coding standards
Roundarch Isobar Java script coding standards
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
 
Formatting ForThe Masses
Formatting ForThe MassesFormatting ForThe Masses
Formatting ForThe Masses
 
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Scripting Oracle Develop 2007
Scripting Oracle Develop 2007Scripting Oracle Develop 2007
Scripting Oracle Develop 2007
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial Slides
 

Último

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 

Último (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 

20130530-PEGjs

  • 1. PEG.js - Javascript Parser Generator - Hidetomo Suzuki
  • 2. 2013 / 5 / 30 Scope Page 2 This MTG Target Understand What’s PEG.js Know How to Use PEG.js Make New DSL and Parser
  • 3. 2013 / 5 / 30 Agenda Page 3 Table of Contents 1.How to make parser 2. What’s PEG.js 3. Try to used to be PEG 4. Let’s make DSL 5.Applications
  • 4. 2013 / 5 / 30 Agenda Page 4 Table of Contents 1.How to make parser 2. What’s PEG.js 3. Try to used to be PEG 4. Let’s make DSL 5.Applications
  • 5. 2013 / 5 / 30 Page 5 1. How to make parser 1. Lexical Analysis(字句解析) 2. Syntactic Parsing(構文解析) 3. Semantic Analysis(意味解析) 4. Intermidiate Code Generation(中間コード生成) 5. Code Optimization(コード最適化) 6. Code Generation(コード生成) General Step of Processing with Compiler Parser
  • 6. 2013 / 5 / 30 Page 6 1. How to make parser 1. Lexical Analysis(字句解析) 2. Syntactic Parsing(構文解析) 3. Semantic Analysis(意味解析) Analysis for Make Parser Make strings(minimum unit of string has semantic) from characters Make tree structure from strings which result of Lexical Analysis Type Check ※ A string of Lexical Analysis is called “Token”.
  • 7. 2013 / 5 / 30 Page 7 1. How to make parser 1. Lexical Analysis position_x = position_x + 2.0 * time  Identifier : position_x  Substitution Symbol : =  Identifier : position_x  Addition Symbol : +  Number : 2.0  Multiple Symbol : *  Identifier : time
  • 8. 2013 / 5 / 30 Page 8 1. How to make parser 2. Syntactic Parsing  Identifier : position_x Substitution Symbol : =  Identifier : position_x Addition Symbol : +  Number : 2.0 Multiple Symbol : *  Identifier : time Substitution Symbol Identifier position_x Expression ExpressionExpression Expression Expression Identifier position_x Number 2.0 Identifier time = + *
  • 9. 2013 / 5 / 30 Page 9 1. How to make parser 3. Semantic Analysis Substitution Symbol Identifier position_x Expression ExpressionExpression Expression Expression Identifier position_x Number Identifier = +  Correct Case : Real Number * Integer Number  Wrong Case : Real Number * Function Pointer 2.0 time *
  • 10. 2013 / 5 / 30 Agenda Page 10 Table of Contents 1.How to make parser 2. What’s PEG.js 3. Try to used to be PEG 4. Let’s make DSL 5.Applications
  • 11. 2013 / 5 / 30 Here comes your footer  2. What‘s PEG.js?  Parser Generator for JavaScript  Use PEG (Parsing Expression Grammar)  Easy to try with the web page Outline of PEG.js http://pegjs.majda.cz/
  • 12. 2013 / 5 / 30 Here comes your footer  2. What‘s PEG.js? PEG(Parsing Expression Grammar) is one of grammar for artificial language. Focus point is how input will be analyzed. What‘s PEG Ex) Simple Expression Grammar which Has Four Arithmetic Operations expression <- addexp addexp <- multiexp (“+” multiexp / “-” multiexp)* multiexp <- number (“*” number / “/” number)* number <- [0-9]+ Accept : 2*3+6/2-5*3 -> multiexp + multiexp – multiexp -> ・・・ Decline : (1+1)*3+6/2-(7-2)*3 -> ?*3+6/2-?*3 -> can’t recognize…
  • 13. 2013 / 5 / 30 Here comes your footer  2. What‘s PEG.js? • Nothing Confusion • Different : “a b / a”, “a / a b” • Easy to use compared with CFG • Don’t need to implement scanner (Lexical Analyzer) • Don’t Express Left Recursive PEG vs CFG(Context Free Grammar) • Confusion Existing • Same : “a b | a”, “a | a b” • Need to implement scanner • Can Express Left Recursive PEG CFG
  • 14. 2013 / 5 / 30 Page 14 2. What‘s PEG.js? 1. Make Grammar with PEG 2. Generate Parser with “pegjs” Command 3. Use the Parser Loaded as a Library Step of Make Parser with PEG.js vim filename.pegjs Pegjs filename.pegjs parser.js <script src=“./parser.js” type=“text/javascript”></script> <script type=“text/javascript”> parser.parse(“something”);
  • 15. 2013 / 5 / 30 Page 15 2. What‘s PEG.js? Install PEG.js # Install Pythonbrew $ curl -kL http://xrl.us/pythonbrewinstall | bash $ vim ~/.bash_profile [[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc $ source ~/.bashrc $ pythonbrew install 2.7.2 $ pythonbrew switch 2.7.2 # Install Node $ git clone git://github.com/creationix/nvm.git ~/.nvm $ nvm install v0.8.7 $ source ~/.nvm/nvm.sh $ nvm use v0.8.7 $ vim ~/.npmrc $ registry = http://registry.npmjs.org/
  • 16. 2013 / 5 / 30 Page 16 2. What‘s PEG.js? 1. Make Grammar with PEG 2. Generate Parser with “pegjs” Command 3. Use the Parser Loaded as a Library Let’s use PEG.js a little bit $ vim ffirst.pegjs start = addexp addexp = integer ("+" integer / "-" integer)* integer = [0-9]+ $ ./node_modules/pegjs/bin/pegjs first.pegjs parser.js $ vim parser.js 1st line: module.export = … -> var parser = … $ vim index.html <script type=“text/javascript” src=“./parser.js”></script> <script type=“text/javascript”> alert(parser.parse(“1+2+3-5”)); </script>
  • 17. 2013 / 5 / 30 Agenda Page 17 Table of Contents 1.How to make parser 2. What’s PEG.js 3. Try to used to be PEG 4. Let’s make DSL 5.Applications
  • 18. 2013 / 5 / 30 Page 18 3.Try to used to be PEG PEG.js Online Version (http://pegjs.majda.cz/online)
  • 19. 2013 / 5 / 30 Page 19 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : integer) Grammar Test Input Test Input Result start = integer integer = digits:[0-9]+ { return parseInt(digits.join(“”), 10); } 123456789 123456789
  • 20. 2013 / 5 / 30 Page 20 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : add two integer) Grammar Test Input Test Input Result start = target1:integer "+" target2:integer { return target1+target2; } integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); } 81+19 100
  • 21. 2013 / 5 / 30 Page 21 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : add and minus two integer) Grammar Test Input Test Input Result start = target1:integer "+" target2:integer { return target1+target2; } / target1:integer "-" target2:integer { return target1-target2; } integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); } 100-19 81
  • 22. 2013 / 5 / 30 Page 22 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : add and minus integers) Grammar Test Input Test Input Result start = expression expression = ope1:integer "+" ope2:expression { return ope1+ope2; } / ope1:integer "-" ope2:expression { return ope1-ope2; } / ope:integer { return ope; } integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); } 1+2+3+4+5-6+7-8+9-10 9
  • 23. 2013 / 5 / 30 Page 23 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : four arithmetic) Grammar Test Input Test Input Result start = expression expression = ope1:integer "+" ope2:expression { return ope1+ope2; } / ope1:integer "-" ope2:expression { return ope1-ope2; } / ope1:integer "*" ope2:expression { return ope1*ope2; } / ope1:integer "/" ope2:expression { return ope1/ope2; } / ope:integer { return ope; } integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); } 2*3/2 3
  • 24. 2013 / 5 / 30 Page 24 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : four arithmetic with priority) Grammar Test Input Test Input Result start = expression expression = ope1:multiple "+" ope2:expression { return ope1+ope2; } / ope1:multiple "-" ope2:expression { return ope1-ope2; } / ope:multiple { return ope; } multiple = ope1:integer "*" ope2:multiple { return ope1*ope2; } / ope1:integer "/" ope2:multiple { return ope1/ope2; } / ope:integer { return ope; } integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); } 2*3/2+8/4 5
  • 25. 2013 / 5 / 30 Page 25 3. Try to used to be PEG Very Simple PEG.js Grammar(calc : four arithmetic with priority) Grammar Test Input Test Input Result start = expression expression = ope1:multiple "+" ope2:expression { return ope1+ope2; } / ope1:multiple "-" ope2:expression { return ope1-ope2; } / ope:multiple { return ope; } multiple = ope1:bracket "*" ope2:multiple { return ope1*ope2; } / ope1:bracket "/" ope2:multiple { return ope1/ope2; } / ope:bracket { return ope; } bracket = "(" exp:expression ")" {return exp; } / ope:integer {return ope; } integer = digits:[0-9]+ { return parseInt(digits.join(""), 10); } (7+3)*3/2+8/(6-2) 17
  • 26. 2013 / 5 / 30 Agenda Page 26 Table of Contents 1.How to make parser 2. What’s PEG.js 3. Try to used to be PEG 4. Let’s make DSL 5.Applications
  • 27. 2013 / 5 / 30 Page 27 4. Let‘s make DSL Config File Format Parser Result Web Page Image Target <LogicalExpression?>Y---[function param] N---[function param] if (isLogicalExpression) function(param); else function(param); Ex) <Hydea?>Y---[show ‘hydea’] N---[show ‘hydeb’] Ex) if (isHydea) show(‘hydea’); else show(‘hydeb’); URL: ex.com/?p=hydea URL: ex.com/?p=hydeb
  • 28. 2013 / 5 / 30 Page 28 4. Let‘s make DSL Just write over specification Make Grammar start = "<" logic "?>Y---[" function " " param "]nN---[" function " " param "]" logic = [a-zA-Z]+ function = [a-zA-Z]+ param = "'" [a-zA-Z]+ "'"
  • 29. 2013 / 5 / 30 Page 29 4. Let‘s make DSL Refactoring a little bit Make Grammar start = "<" identifier "?>Y---[" identifier " " param "]nN---[" identifier " " param "]" param = "'" identifier "'" identifier = [a-zA-Z]+
  • 30. 2013 / 5 / 30 Page 30 4. Let‘s make DSL Finish to make param and identifier part Make Grammar start = "<" identifier "?>Y---[" identifier " " param "]nN---[" identifier " " param "]" param = "'" id:identifier "'" {return "'" + id + "'"; } identifier = chars:[a-zA-Z]+ {return chars.join(""); }
  • 31. 2013 / 5 / 30 Page 31 4. Let‘s make DSL Organize Function Part Make Grammar start = "<" identifier "?>Y---[" function "]nN---[" function "]" function = id:identifier " " param:param {return id + "(" + param + ")"; } param = "'" id:identifier "'" {return "'" + id + "'"; } identifier = chars:[a-zA-Z]+ {return chars.join(""); }
  • 32. 2013 / 5 / 30 Page 32 4. Let‘s make DSL Finish to make Make Grammar start = "<" id:identifier "?>Y---[" f1:function "]nN---[" f2:function "]" {return "if (is" + id + ") " + f1 + ";nelse " + f2 + ";"; } function = id:identifier " " param:param {return id + "(" + param + ")"; } param = "'" id:identifier "'" {return "'" + id + "'"; } identifier = chars:[a-zA-Z]+ {return chars.join(""); }
  • 33. 2013 / 5 / 30 Agenda Page 33 Table of Contents 1.How to make parser 2. What’s PEG.js 3. Try to used to be PEG 4. Let’s make DSL 5.Applications
  • 34. 2013 / 5 / 30 Page 34 5. Application Show My Demo(Demo’s grammar) 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 + ")nt" + branch1 + 'nelse ' + branch2 : 'if (' + fac + ')nt' + branch1 + 'nelsent' + 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('') }
  • 35. 2013 / 5 / 30 Page 35 5. Application Recommendation Conf1 Conf2 Conf3 ・・・ Recommendation Engine Choose a config file
  • 36. 2013 / 5 / 30 Reference Page 36 Famous Book for Compiler
  • 37. 2013 / 5 / 30 Thank you Page 37 Thank you for your listening