{"content":{"id":1058,"title":"'Build Your Own Lisp' Solutions: 6.6","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Change the grammar to recognize decimal numbers such as 0.01, 5.21, or 10.2.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e\" \\\r\nnumber : /-?[0-9]+(.[0-9]+)*/ ; \\\r\noperator : 'add' | 'sub' | 'mul' | 'div'; \\\r\nexpr : \u0026lt;number\u003e | '(' \u0026lt;operator\u003e \u0026lt;expr\u003e+ ')' ; \\\r\nlispy : /^/ \u0026lt;operator\u003e \u0026lt;expr\u003e+ /$/ ; \\\r\n\"\u003c/code\u003e\u003c/pre\u003e\r\n\u003c/p\u003e","publication_date":"2020-01-04T00:00:00.000Z","created_at":"2019-12-10T00:09:33.000Z","updated_at":"2020-07-06T19:13:57.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=build_your_own_lisp\"\u003ebuild_your_own_lisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e"}

'Build Your Own Lisp' Solutions: 6.6

Q: Change the grammar to recognize decimal numbers such as 0.01, 5.21, or 10.2.

A:

"                                                   \
number   : /-?[0-9]+(.[0-9]+)*/ ;                   \
operator : 'add' | 'sub' | 'mul' | 'div';           \
expr     : <number> | '(' <operator> <expr>+ ')' ;  \
lispy    : /^/ <operator> <expr>+ /$/ ;             \
"