http://www.cs.ucla.edu/~awarth/ometa/
ç ãã¦å¾®å¦ãªäºãä½ãåºæ¥ãªãã®ã§è»½ã OMeta ã«ã¤ãã¦æ¸ãã¾ããOMeta ã¨ã¯ãhttp://lambda-the-ultimate.org/node/2477 ã«ãã£ã¦ä¸èºæåã«ãªã£ãã¡ã¿è¨èª - ããã°ã©ãã³ã°è¨èªãæ¸ãããã®ããã°ã©ãã³ã°è¨èª - ã§ããæããã¦åãªã PEG ãã¼ãµã¨ä¸ä½ã©ããéãã®ãç§ãããåãã£ã¦ããªãã®ã§ããã®ãããã®äºãåå¼·ãããã¨æãã¾ããOMeta ã«ã¯ã¹ã¯ã¤ã¼ã¯ããã¤ã¢ã³ã®ãã¤ãã®å®è£ ãããã¾ãããæè¿ Web ãã©ã¦ã¶ã§å®è¡åºæ¥ã Javascript çãå ¬éãããã®ã§ãã¡ãã試ãã¾ãã
OMeta ã§ä½ãåºæ¥ããï¼
http://www.cs.ucla.edu/~awarth/ometa/ometa-js/ ãéãã¨ãOMeta ã§ä½æããã Smalltalk è¨èªã®ãã¢ã§éã¶äºãåºæ¥ã¾ãããã®ä¾ã§ã¯ãOMeta 㯠Smalltalk ã½ã¼ã¹ã javascript ã«å¤æãã¦å®è¡ãã¦ãã¾ããä¾ãã° 1 + 2 * 3 ãé¸æã㦠[print it] ãã¿ã³ãæ¼ãã¦ã¿ã¦ãã ãããçãã® 6 ã表示ããã¾ããçããéãã£ã¦ï¼Smalltalk ã«ã¯æ¼ç®åã¨ããæ¦å¿µãç¡ãã+ ã * ã¯å¼æ°ãä¸ã¤åãã¡ãã»ã¼ã¸ã§ããjavascript çã«ç¡çããæ¸ã㨠( 1.+(2) ).*(3) ã®ããã«ãªãã¾ãã
ä½è«ã§ããããã®ãããªããã°ã©ã éçºç°å¢ã Smalltalk ã§ã¯ Workspace ã¨å¼ã³ã大å¤éè¦ãã¦ãã¾ãããããOMeta èªèº«ã¨ã¯ç¬ç«ãã¦ããã®ã§ããããããªãã®ã¢ããªã±ã¼ã·ã§ã³ã§ OMeta ã使ãããå ´åã« Workspace ãå«ããå¿ è¦ã¯ããã¾ããã
次ã«ã123 printString size ã print it ããã¨ã3 ã表示ããã¾ããããã¯ã123 ãæååã«ããç©('123')ã®ãµã¤ãº(3)ã¨ããæå³ã§ãããã®ããã«è£¸ã®ã·ã³ãã«ãç½®ããã¦ããã¨ãããã¯å¼æ°ãåããªãã¡ãã»ã¼ã¸ã¨è§£éããã¾ããããã javascript çã«æ¸ãã¨ã123.printString().size() ã«ãªãã¾ãã
ä¸ã¤é£ã°ã㦠self inform: 'hello', ' world' ãé¸æã㦠[do it] ãã¿ã³ãæ¼ãã¦ãã ããã"hello world" ã¨ããã¢ã©ã¼ãã表示ããã¾ããinform: ã®ããã«ã³ãã³ãã¤ããã·ã³ãã«ã¯ä¸ã¤ä»¥ä¸ã®å¼æ°ãåãã¡ãã»ã¼ã¸ã§ããä¾ã«ãã£ã¦ javascript çã«æ¸ãã¨ãself.inform( 'hello'.,('world') ) ã«ãªãã¾ããã³ã³ãã¯æååã®é£çµãè¡ãã¡ãã»ã¼ã¸ã§ãã
ã¨ãSmalltalk ã®ææ³ã«ã¤ãã¦æ¸ãåºãã¨ãããç¡ãã®ã§ãæ®ãã¯ãããã§èª¿ã¹ã¦ãã ãããOMeta ã§ã¯ãSmalltalk æãææ³ã«æ··ããã®ã§ãè¦ããæ¹ãè¯ãã§ãã
åç´ãªææ³
ãã¦ãããã§éè¦ãªã®ã¯ãSmalltalk è¨èªã§éã¶ã ãã§ã¯ãªããããªãèªèº«ã§ Smalltalk ãä½ãäºãåºæ¥ãäºã§ãããããã¦ã§ããã©ã¦ã¶ã®ä¸ã«ãSmalltalk ã ãã§ãªãã©ããªè¨èªã§ãä½ãäºãåºæ¥ã¾ããOMeta ã¯è¨èªããã¢ã®çºã®ãªã¼ã«ã¤ã³ã¯ã³ãã¼ã«ã¨ãã¦éçºããã¾ãããã¨ã¯ããæåãã Smalltalk ã¯é£ããããã®ã§åç´ãªä¾ãããããã¹ãããã¯ã¹ã®ä¸ãå ¨é¨åé¤ãã¦æ¬¡ã®æã試ãã¦ã¿ã¾ãããã
ometa M { ruleX ::= $x. ruleXS ::= <ruleX>+. }. M match: $x with: #ruleX. M matchAll: 'xxxxx' with: #ruleXS.
æåã® ometa ã¯ã©ã¹å { ææ³ } ã®å½¢ãææ³å®ç¾©ã§ãä½æãããææ³ã®å©ç¨ä¾ãç¶ãã¾ããææ³ã¯ãã«ã¼ã«å ::= ãã¿ã¼ã³ ã®ããã«æ¸ãã¾ããã«ã¼ã«åã¨ã¯é£ããè¨ãã¨éçµç«¯è¨å·ã¨ãå¼ã³ã¾ããããã¿ã¼ã³ã«ãããããæååãå¾ã§åç
§ããã®ã«ä½¿ãã¾ãããã®ä¾ã§ã¯ã"x" ã«ããããã ruleX ã¨ããææ³ããå¾ã§
å®éã«è§£æãå®è¡ãã match:with: ã¯ãå¼æ°ãä¸ã¤ã ããããããmatchAll:with: ã¯ã¹ããªã¼ã ã¨ã¿ãªãã¦ããããã¾ãããã ããjavascript ã®æ§è³ª(æåã¨æååãåºå¥ããªã)ããããçé¢ç®ã«å®è£ ããã¦ã¾ããã®ã§ãmatchAll:with: ã ãè¦ãã¦ããã¨è¯ãã¨æãã¾ãã
ãããã do it ãã¦ããä½ãèµ·ããã¾ãããä½ãèµ·ãããªãã®ã¯æ£ãã証æ ã試ãã«ä»¥ä¸ã do it ãã¦ãã©ãããæã«ã¨ã©ã¼ã«ãªãã試ãã¦ã¿ã¦ãã ããã
M matchAll: 'yyyyyy' with: #ruleXS. M matchAll: 'xyyyyy' with: #ruleXS.
'xyyyy' ã¾ã§ãããããã¦ãã¾ãæã PEG(解æ表ç¾ææ³)ã®ç¹å¾´ã§ããããã¯ã¾ãé¢é£ããç¹ããããã¾ãããææ³ã®è¨è¿°æ¹æ³ã«ã¯ãä»ã«å°åºè¦åã¨ããã®ãããã¾ããããã㯠PEG ã¨å ¨ãç°ãªã£ãèãã§ããå°åºè¦åã§å®ç¾©ãããææ³ã¨ã¯ãããéå§è¨å·ãã«ã¼ã«ã«åºã¥ãæ¸ãæãã¦è¡ãéã«ç¾ããå¯è½æ§ã®äºã§ããã ããæååã®å ¨ä½ããããã®å¯¾è±¡ã§ããããã PEG ã§ã¯ãæååã®å é ããããæ¡ä»¶ã«åºã¥æ¤æ»ãã¦è¡ãäºãææ³ã®å®ç¾©ã«ãªã£ã¦ãã¾ããã ããæååã®å ¨ä½ããããããããæã¯ãèªåã§ãã®ãããªææ³ãå®ç¾©ããå¿ è¦ãããã¾ãã
ãã®äºã¤ã®å ¨ãç°ãªã£ãææ³è¡¨ç¾ã®é¢ä¿ã¯é常ã«èå³æ·±ããããã¯ã§ãããçç¥ãã¾ãã
do it ã®ä»£ããã« print it ãæ¼ããæ¹ã¯ããªã«ãã表示ãããäºã«æ°ã¥ãã¨æãã¾ããããã®è©±ã¯ã¡ãã£ã¨é·ããªããããªã®ã§ä¸æ¦ä¼ã¿ã¾ãã
PEG ã試ã
PEG ã«ã¤ãã¦èªããã«è©±ãç¶ããã®ã¯é£ããããªã®ã§ããã㧠S å¼ã®ç°¡åãªãã¼ãµã¼ãä½ããªãã PEG ãè¦ãã¾ãããã¼ãµã¼ã¨ããã®ã¯ãæååãææ³ã©ããèªã¿è¾¼ãã§ä½ããè¿ãããã°ã©ã ã®äºã§ããã§ãæåã¯ãä½ãè¿ããã¯èããªãã§ãææ³ããã£ã¦ããã©ããã ããåé¡ã«ãã¾ã(åã£ã¦ãæã¯ä½ããããééã£ã¦ããã¨ã¨ã©ã¼ã«ãªãã¾ã)ãææ³ã¯ä»¥ä¸ã®éãã
- å¼ã®ä¾: (+ 3 (* 4 5))
- å ¨ä½ãã«ãã³ã§ããã
- ã«ãã³ã®ä¸ã¯ (æ¼ç®å ä½ã ä½ã)
- ä½ãã®ã¨ããã«ã¯ãæ°åãå¼ãå ¥ãã
- æ¼ç®å㯠+ - * / ã®ã©ããè¨å·ã
ã§ã¯ä¸çªç°¡åãããªæ¼ç®åããããã ã¢ããå¼ã«ãã¡ãã£ã¨ãã¤ãã¹ãã³ã¼ããæ¸ããªããããã¾ãããã
ometa SEXP { operator ::= '+' | '-' | '*' | '/'. } SEXP matchAll: '*' with: #operator. "æ¼ç®å ã¨ã¯ã+ - * / ã®ã©ãã"
å ã»ã©ã®ããã«ãSEXP ã¨ããææ³å(ãªãã§ããã)ãæå®ã㦠{} ã®ä¸ã«ãã«ã¼ã«å ::= ãã¿ã¼ã³.ãã®å½¢ã§ææ³ãè¨è¿°ãã¾ãã| ã¯ãé åºã¤ãé¸æãã表ãã+ - * / ã®ã©ãããç¾ããã operator ã¨ããã«ã¼ã«ã«ãããããã¨ã¿ãªããã¾ããé åºã¤ãã¨ããã®ã¯ãä¸ã¤ã§ããããããè¦ç´ ãè¦ã¤ããããã以ä¸æ¤ç´¢ãè¡ããªãã¨ããäºã§ããã¡ãªã¿ã« PEG ã§é åºã¤ãé¸æã¨ãã㨠/ ã®è¨å·ã使ãããã§ãããä½è ã®å¥½ã¿ã§(?) | ã«ãªã£ã¦ãã¾ããä»¥ä¸ ::= ã®è¡ã¯ ometa SEXP {} ã®ä¸ã«æ¸ã足ãã¦ãã£ã¦ãã ããã
digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'. number ::= <digit>+. SEXP matchAll: '+ 1 2' with: #digit. "æ°åã¨ã¯ã0 1 2 3 4 5 6 7 8 9 ã®ã©ããã§ãã" SEXP matchAll: '1234' with: #number. "æ°å¤ã¨ã¯ãæ°åãä¸ã¤ä»¥ä¸ä¸¦ãã ãã®"
次ã¯æ°åã§ããdigit ã¯æ°åä¸æåã«ãããããnumber 㯠+ ã®å¹æã§ç¶ã digit å ¨ã¦ãæ¤ç´¢ãã¾ããä¸åº¦å®ç¾©ããææ³ãä»ã®ææ³ã«åãè¾¼ãéã¯ããã®ããã« <> ã§å ã¿ã¾ãã
space ::= (' ' | '\n')+. SEXP matchAll: ' ' with: #space. "ç©ºç½ ã¨ã¯ã1ã¤ä»¥ä¸ã®ã ã"
ããã¯ãã«ãã³ã®ä¸ã®åèªãåããããã®ç©ºç½ãå®ç¾©ãã¾ããããã¦æå¾ã«ãã£ããå帰çãªå¼å ¨ä½ãå®ç¾©ãã¾ãã
inner ::= <operator> <space> <expr> <space> <expr>. expr ::= <number> | '(' <inner> ')'. SEXP matchAll: '+ 3 4' with: #inner. "ä¸èº«ã¨ã¯ãæ¼ç®åã¨å¼ã«ç©ºç½ãæãã ãã®" SEXP matchAll: '(+ 3 4)' with: #expr. "å¼ã¨ã¯ãæ°åãä¸èº«ãã«ãã³ã§å ãã ç©" SEXP matchAll: '(+ 3 (+ 4 5))' with: #expr. "å帰ãã¦ãã¦ãè¯ãã§ã"
ã¨è¨ãããã§ãããã¾ã§å ¨é¨ãçºãã¦æ¸ãã¾ããto it ããæã¯ãã³ã¡ã³ãã®ã¤ããã®ã¯ã©ã¼ãã¯é¸æããªãã§ä¸ããã
ometa SEXP { operator ::= '+' | '-' | '*' | '/'. digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'. number ::= <digit>+. space ::= (' ' | '\n')+. inner ::= <operator> <space> <expr> <space> <expr>. expr ::= <number> | '(' <inner> ')'. } SEXP matchAll: '*' with: #operator. "operator ã¨ã¯ã+ - * / ã®ã©ãã" SEXP matchAll: '7' with: #digit. "æ°åã¨ã¯ã0 1 2 3 4 5 6 7 8 9 ã®ã©ããã§ãã" SEXP matchAll: '1234' with: #number. "æ°å¤ã¨ã¯ãæ°åãä¸ã¤ä»¥ä¸ä¸¦ãã ãã®" SEXP matchAll: ' ' with: #space. "space ã¨ã¯ã1ã¤ä»¥ä¸ã®ç©ºç½" SEXP matchAll: '+ 3 4' with: #inner. "ä¸èº«ã¨ã¯ãæ¼ç®åã¨å¼ã«ç©ºç½ãæãã ãã®" SEXP matchAll: '(+ 3 4)' with: #expr. "å¼ã¨ã¯ãæ°åãä¸èº«ãã«ãã³ã§å ãã ç©" SEXP matchAll: '(+ 3 (+ 4 5))' with: #expr. "å帰ãã¦ãã¦ãè¯ãã§ã"
ãã®ãã¨é»åãä½ãäºå®ã
ãã¾ã: OMeta ã®ãµã³ãã«ããã¼ã«ã«ã§åããã
svn co http://jarrett.cs.ucla.edu/svn/ometa-js/ cd ometa-js svn co http://jarrett.cs.ucla.edu/ometa-js/projects/