{"id":542,"date":"2014-01-08T05:56:46","date_gmt":"2014-01-07T20:56:46","guid":{"rendered":"http:\/\/owmya.com\/wordpress\/?p=542"},"modified":"2014-06-27T16:54:32","modified_gmt":"2014-06-27T07:54:32","slug":"esrap-peg-%e3%83%91%e3%83%83%e3%82%b1%e3%83%bc%e3%82%b8%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b-%e3%82%aa%e3%83%9e%e3%82%b1","status":"publish","type":"post","link":"https:\/\/owmya.com\/wordpress\/?p=542","title":{"rendered":"esrap-peg \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b  \u30aa\u30de\u30b1"},"content":{"rendered":"<h2>PEG \u5b9a\u7fa9<\/h2>\n<p>\u5909\u6570\u5165\u308c\u3066\u307f\u305f\u3053\u3068\u4ee5\u5916\u306f\u3001<a title=\"esrap-peg \u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b\" href=\"https:\/\/owmya.com\/wordpress\/?p=524\">\u524d\u56de<\/a>\u306e\u3082\u306e\u3068\u540c\u3058\u3002\u30a8\u30e9\u30fc\u51e6\u7406\u306f\u3044\u3044\u304b\u3052\u3093\u3067\u3059\u3002\u3044\u308d\u3044\u308d\u307e\u305a\u3044\u3068\u3053\u308d\u304c\u3042\u308b\u306e\u306f\u308f\u304b\u3063\u3066\u3044\u308b\u3051\u308c\u3069\u3001\u9aa8\u5b50\u306e\u5206\u304b\u308a\u3084\u3059\u3055\u3067\u306f\u3053\u3093\u306a\u3082\u306e\u306e\u65b9\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u304b\u3082\u30fb\u30fb\u30fb<\/p>\n<pre><code>statement &lt;- spaces (definition \/ expression) spaces\r\n\r\ndefinition &lt;- identifier spaces define spaces expression\r\n\r\nexpression &lt;- (expression spaces add spaces multiplicative_expression)\r\n    \/ (expression spaces sub spaces multiplicative_expression)\r\n    \/ multiplicative_expression\r\n\r\nmultiplicative_expression &lt;-\r\n    (multiplicative_expression spaces mult spaces primary_expression)\r\n    \/ (multiplicative_expression spaces div spaces primary_expression)\r\n    \/ (multiplicative_expression spaces mod spaces primary_expression)\r\n    \/ primary_expression\r\n\r\nprimary_expression &lt;- constant\r\n    \/ identifier\r\n    \/ ('(' spaces expression spaces ')')\r\n\r\nidentifier &lt;- identifier_start identifier_continuas*\r\n\r\nidentifier_continuas &lt;- alphabet \/ underscore \/ digit\r\n\r\nidentifier_start &lt;- alphabet \/ underscore\r\n\r\nconstant &lt;- integer_constant\r\n\r\ninteger_constant &lt;- decimal_integer_constant\r\n\r\ndecimal_integer_constant &lt;- sign_part? digit+\r\n\r\nsign_part &lt;- '+' \/ '-'\r\n\r\n## Lex!\r\n\r\nalphabet &lt;- [a-zA-Z]\r\n\r\ndigit &lt;- [0-9]\r\n\r\nunderscore &lt;- '_'\r\n\r\noperator &lt;- add \/ sub \/ mult \/ div \/ mod \/ define\r\n\r\n# operator\r\n\r\nadd &lt;- '+'\r\n\r\nsub &lt;- '-'\r\n\r\nmult &lt;- '*'\r\n\r\ndiv &lt;- '\/'\r\n\r\nmod &lt;- '%'\r\n\r\ndefine &lt;- '='\r\n\r\n#\r\n\r\nspaces &lt;- [ \\t]*\r\n\r\neol &lt;- '\\r\\n' \/ '\\n' \/ '\\r' \/ eof\r\n\r\neof &lt;- !.\r\n\r\n<\/code><\/pre>\n<h2>Lisp \u30b3\u30fc\u30c9<\/h2>\n<pre><code>(defvar id-table)\r\n\r\n(defun clean-identifier-table ()\r\n  (setf id-table (make-hash-table :test #'equal)))\r\n\r\n(clean-identifier-table)\r\n\r\n(peg-compile\r\n  (parse-peg-file \"foo.peg\"))\r\n\r\n(defun val_digits (x &amp;optional (y 0))\r\n  (cond ((not x) y)\r\n        ('t (val_digits (cdr x) (+ (* 10 y) (parse-integer (cadr (car x))))))))\r\n\r\n(def-peg-fun statement (x) (ast-eval (second x)))\r\n\r\n(defun id-cont-key (x)\r\n  (if (car x)\r\n      (concatenate 'string (cadr (cadr (car x))) (id-cont-key (cdr x)))))\r\n\r\n(defun id-key (x)\r\n  (concatenate 'string (cadr (cadr (car x))) (id-cont-key (cadr x))))\r\n\r\n(defun id-value (x) (fifth x))\r\n\r\n(def-peg-fun identifier (x)\r\n  (let* ((id-str (id-key x)) (res (gethash id-str id-table)))\r\n    (if res\r\n        (ast-eval res)\r\n        (format 't \"Undefined identifier: ~S\" id-str))))\r\n\r\n(def-peg-fun definition (x)\r\n  (setf (gethash (id-key (cadr (car x))) id-table) (id-value x)))\r\n\r\n(def-peg-fun expression (x)\r\n  (cond ((atom (car x)) (ast-eval x)) ; x == MULTIPLICATIVE_EXPRESSION\r\n        ((eq 'add (car (third x))) (+ (ast-eval (car x)) (ast-eval (fifth x))))\r\n        ((eq 'sub (car (third x))) (- (ast-eval (car x)) (ast-eval (fifth x))))\r\n        ('t (break))))\r\n\r\n(def-peg-fun multiplicative_expression (x)\r\n  (cond ((atom (car x)) (ast-eval x)) ; x == PRIMARY_EXPRESSION\r\n        ((eq 'mult (car (third x))) (* (ast-eval (car x)) (ast-eval (fifth x))))\r\n        ((eq 'div (car (third x))) (\/ (ast-eval (car x)) (ast-eval (fifth x))))\r\n        ((eq 'mod (car (third x))) (mod (ast-eval (car x)) (ast-eval (fifth x))))\r\n        ('t (progn (format 't \"MULTIPLICATIVE EXPRESSION ERROR: ~A\" (third x))))))\r\n\r\n(def-peg-fun primary_expression (x)\r\n  (cond ((eq 'constant (car x)) (ast-eval x))\r\n        ((eq 'identifier (car x)) (ast-eval x))\r\n        ('t (ast-eval (third x))))) ; open-r-brachet spaces EXPRESSION spaces close-r-bracket\r\n\r\n(def-peg-fun constant (x) (ast-eval x))\r\n\r\n(def-peg-fun integer_constant (x) (ast-eval x))\r\n\r\n(def-peg-fun decimal_integer_constant (x)\r\n  (cond ((not (car x)) (val_digits (cadr x)))\r\n        ((equal (cadr (car x)) \"-\") (- (val_digits (cadr x))))\r\n        ('t (val_digits (cadr x)))))<\/code><\/pre>\n<h2>\u3053\u3053\u3067\u306e\u30b3\u30fc\u30c9\u306e\u30e9\u30a4\u30bb\u30f3\u30b9\u306b\u3064\u3044\u3066<\/h2>\n<p>\u4e0a\u8a18\u3001PEG \u306a\u3089\u3073\u306b Common Lisp \u306e\u30b3\u30fc\u30c9\u306f<a title=\"\u3053\u306e\u30b5\u30a4\u30c8\u3067\u63d0\u793a\u3057\u3066\u3044\u308b\u30b3\u30fc\u30c9\u306e\u57fa\u672c License\" href=\"https:\/\/owmya.com\/wordpress\/?page_id=560\">\u3053\u306e\u30b5\u30a4\u30c8\u3067\u63d0\u793a\u3057\u3066\u3044\u308b\u30b3\u30fc\u30c9\u306e\u57fa\u672c License<\/a> \u306b\u63b2\u3052\u308b\u3082\u306e\u306b\u5f93\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PEG \u5b9a\u7fa9 \u5909\u6570\u5165\u308c\u3066\u307f\u305f\u3053\u3068\u4ee5\u5916\u306f\u3001\u524d\u56de\u306e\u3082\u306e\u3068\u540c\u3058\u3002\u30a8\u30e9\u30fc\u51e6\u7406\u306f\u3044\u3044\u304b\u3052\u3093\u3067\u3059\u3002\u3044\u308d\u3044\u308d\u307e\u305a\u3044\u3068\u3053\u308d\u304c\u3042\u308b\u306e\u306f\u308f\u304b\u3063\u3066\u3044\u308b\u3051\u308c\u3069\u3001\u9aa8\u5b50\u306e\u5206\u304b\u308a\u3084\u3059\u3055\u3067\u306f\u3053\u3093\u306a\u3082\u306e\u306e\u65b9\u304c\u308f\u304b\u308a\u3084\u3059\u3044\u304b\u3082\u30fb\u30fb\u30fb statement &#038; &hellip; <a href=\"https:\/\/owmya.com\/wordpress\/?p=542\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,8,16,19,4],"tags":[17],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-lisp","category-8","category-16","category-19","category-develop_and_design","tag-common-lisp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=542"}],"version-history":[{"count":4,"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions\/563"}],"wp:attachment":[{"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/owmya.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}