From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: Make peg.el a built-in library? Date: Thu, 10 Nov 2022 10:15:23 +0200 Message-ID: <83pmdvrzs4.fsf@gnu.org> References: <875yvtbbn3.fsf@ericabrahamsen.net> <877d07a16u.fsf@localhost> <87tu3asg2r.fsf@ericabrahamsen.net> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9146"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: tomas@tuxteam.de Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Nov 10 09:16:14 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ot2jO-0002DQ-Mo for ged-emacs-devel@m.gmane-mx.org; Thu, 10 Nov 2022 09:16:14 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ot2il-0003HF-Lh; Thu, 10 Nov 2022 03:15:35 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ot2ic-0003F6-Ft for emacs-devel@gnu.org; Thu, 10 Nov 2022 03:15:31 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ot2ib-0000a5-Jm; Thu, 10 Nov 2022 03:15:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=mMEnd6q4eTIZdyAT4pZ/3I3I+p7O4vtfC8HPnn1TO1Y=; b=ED+GWaX2Aavk 6CzU8VA80d0hOAKgEhcZpL2wZXVoeaMXQEJZoOcZn5/9pgl7tYwtYSLyja8dMpVVqJ/nOPzVQjnWl xhoVWMYI40plsahp0hi+eG/6AZmEX8lrr1odOUNhvpR8+ggnoEPUMkaBf9bdTahAOCg5T89McCS6x P6TbXHf0QqCe9EUr1ThgvPwvsfLM40gepRGl+G95bKie1K1ZpiCqV4BP4jN1B3H7J+oeUhinpO4nz Il5yR6fn6N+A33QmPAo9K6I6+Cw7x4DtJGVTdvjfqBnvWLtizpug8jlmITkyYW+OhpdazpBNhbcet iAmG+yuCo5+CnG/UMr2v9A==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ot2iY-0002Ro-FA; Thu, 10 Nov 2022 03:15:25 -0500 In-Reply-To: (tomas@tuxteam.de) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:299462 Archived-At: > Date: Thu, 10 Nov 2022 06:25:55 +0100 > From: > > > Would someone like to tell me in 10 lines what job peg.el does? > > PEG (Parsing Expression Grammars [1]) is a grammar notation which can > be automatically translated into a parser (think regular expressions). The reference [1] was probably meant to be https://en.wikipedia.org/wiki/Parsing_expression_grammar or somesuch > The notation is actually similar to that of regexps. I believe you meant "similar to regular expressions in rx form"? > The main difference > is that the "alternative" operator is an "ordered" choice instead of an > ambiguous choice. To compensate for this, the notation provides for a > (potential) lookahead mechanism, which, in the naive implementation would > lead to exponential running time in the worst case. The canonical > implementation (nicknamed "packrat") addresses that by memoizing. > > Basically they can do what a recursive descent parser can, are thus > slightly more powerful than regexps. They lead to nice little grammars, > but they do take some practice to be useful. I think an example from peg.el will clarify the issue: ;; This file implements the macros `define-peg-rule', `with-peg-rules', and ;; `peg-parse' which parses the current buffer according to a PEG. ;; E.g. we can match integers with: ;; ;; (with-peg-rules ;; ((number sign digit (* digit)) ;; (sign (or "+" "-" "")) ;; (digit [0-9])) ;; (peg-run (peg number))) ;; or ;; (define-peg-rule digit () ;; [0-9]) ;; (peg-parse (number sign digit (* digit)) ;; (sign (or "+" "-" ""))) HTH