Simple peg examples

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple peg examples

Glenn edgar

I was trying to use peg module.  I could not figure it out.  Are there simple complete programs. 

Thanks

On Jun 7, 2012 12:42 AM, "Paul K" <[hidden email]> wrote:
> By deployment, I assume you mean an install? I figure that CMake can
> be made to copy the bundles and documentation to a separate dir that
> could be turned into dmg. However, I've never done that before, but it
> looks fairly easy. There is still a lot to do to make good bundles
> though.

Right; this is what I want to do (in terms of turning into dmg). I
want to make it easy for MacOS users to install my wxLua-based
application. It includes one/two executables and a bunch of .lua files
and related resources. This may be relevant, although old discussion:
http://www.mail-archive.com/wxlua-users@.../msg00360.html

> documentation. I guess they all go into subdirectories of a
> wxLua-X.Y.Z subdirectory in Applications? I have to look into how
> other people have done it... unless you already know.

No, I don't know yet, but I'm going to figure it out.

Paul.

On Wed, Jun 6, 2012 at 9:04 PM, John Labenski <[hidden email]> wrote:
> On Wed, Jun 6, 2012 at 2:39 AM, Paul K <[hidden email]> wrote:
>> Hi John,
>>
>> Thank you for the help; I've managed to build wxLua on MacOS using
>> CMake, but ended up specifying i386 architecture as it seems like 2.8
>> wxwidgets require 32bit builds.
>>
>> Is there an easy way to package this application for MacOS deployment?
>> Let's say I want to deploy wxLuaEdit.
>
> By deployment, I assume you mean an install? I figure that CMake can
> be made to copy the bundles and documentation to a separate dir that
> could be turned into dmg. However, I've never done that before, but it
> looks fairly easy. There is still a lot to do to make good bundles
> though.
>
> I'm also not sure how best to deploy it. OSX programs seem to
> typically have a single bundle that you copy to the Applications dir,
> but wxLua has multiple applications, sample Lua programs, and
> documentation. I guess they all go into subdirectories of a
> wxLua-X.Y.Z subdirectory in Applications? I have to look into how
> other people have done it... unless you already know.
>
> Regards,
>    John
>

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

steve donovan
On Mon, Jun 11, 2012 at 4:32 AM, Glenn Edgar <[hidden email]> wrote:
> I was trying to use peg module.  I could not figure it out.  Are there
> simple complete programs.

Best place is the Wiki:

http://lua-users.org/wiki/LpegRecipes

Otherwise, sit with the manual and work out simple examples using the
interactive prompt.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Philippe Lhoste
In reply to this post by Glenn edgar
On 11/06/2012 04:32, Glenn Edgar wrote:
> I was trying to use peg module.  I could not figure it out.  Are there simple complete
>  programs.

Beside what Steve indicated, it is worth looking inside this mailing list archive, people
have posted good stuff here.
And, of course, study the examples given by the reference, they are gems.

> On Jun 7, 2012 12:42 AM, "Paul K" <[hidden email]
> <mailto:[hidden email]>> wrote:
Stuff totally unrelated to this discussion...

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

steve donovan
In reply to this post by Glenn edgar
On Mon, Jun 11, 2012 at 4:32 AM, Glenn Edgar <[hidden email]> wrote:
> I was trying to use peg module.  I could not figure it out.  Are there
> simple complete programs.

I have just added a tutorial to the Wiki:

http://lua-users.org/wiki/LpegTutorial

Just a first draft, and I must fully disclose that I Am Not an Expert.
 Please edit at will.  The example at the end is the complete program
I used to convert the Markdown of the article into Lua wiki markup.
(Due to escaping problems I was unable to mention the magic '{{{' in
code examples, so I edited it to be '[[['.  The conversion program has
bugs, as well, e.g. any mention of '> ' causes the blockquote logic to
kick in)

There's also a fairly old article by Gavin Wraith

http://www.wra1th.plus.com/lua/peg/PEG.html

Although he uses RiscLua syntax: mentally substitute 'function' for
'\' and 'return' for '==>'.

And the extremely cool notation 'local P,C in lpeg' is short for the
clumsy 'local P,C = lpeg.P, lpeg.C'

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Gavin Wraith
In message
<CA+ZMS_se879Ov1TxWnHSaTjR2joM3ZSg6dZnG3Ht1W=[hidden email]>
you wrote:

> On Mon, Jun 11, 2012 at 4:32 AM, Glenn Edgar <[hidden email]> wrote:
> > I was trying to use peg module.
> ...............
> There's also a fairly old article by Gavin Wraith
>
> http://www.wra1th.plus.com/lua/peg/PEG.html
>
> Although he uses RiscLua syntax: mentally substitute 'function' for
> '\' and 'return' for '==>'.

That should be '=>'.
 
> And the extremely cool notation 'local P,C in lpeg' is short for the
> clumsy 'local P,C = lpeg.P, lpeg.C'

It is cool. It is not as widely applicable as Peter Shook's original
patch, being only useable after the keyword 'local'. However, it does
encourage the use of local variables and I find it very convenient.

On the topic of lpeg I am full of admiration and frustration.
I am amazed that nobody (as far as I know) used an abstract datatype
for patterns years ago. Conflating patterns with strings has been a
big source of unreadability and exploding escape-sequences.
My frustration is my own fault for not practicing with lpeg enough.
I still find it hard to write the patterns and captures that I really
want. I also find it hard to express error-trapping smoothly with
lpeg. But the fault is with me, not lpeg. I suspect that human
psychology is to blame; we relegate to the sidelines, and so overlook,
the outcomes that we do not want, and this explains why we often
need so many shots before we get the right bug-trapping when we
try to write down a pattern.

--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

steve donovan
On Tue, Jun 12, 2012 at 1:59 PM, Gavin Wraith <[hidden email]> wrote:
> It is cool. It is not as widely applicable as Peter Shook's original
> patch, being only useable after the keyword 'local'. However, it does
> encourage the use of local variables and I find it very convenient.

Which is precisely why it's one of the few syntax tweaks I would
whole-heartedly +1.

> On the topic of lpeg I am full of admiration and frustration.

I've resisted for years but now realize that it is a most amazing
piece of technology.  I was prompted to write a tutorial because I
hoped that I could record my learning experience in a way that others
might find useful.

> Conflating patterns with strings has been a
> big source of unreadability and exploding escape-sequences.

Absolutely - no more 'magic' is a point that you do make. Being able
to _compose_ patterns makes it possible to approach string pattern
matching as a programmer, not as a cut 'n paste activity.

It's a new language. The tutorial aims to introduce the vocabulary in
a more gentle fashion, in a very interactive way. I still remember my
headaches with regular expressions and even Lua string patterns, and
then the light dawned. But non-trivial regular expressions still make
my head hurt to read.

Still not entirely sure how to write about grammars without scaring
the children!

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Tony Finch
In reply to this post by Gavin Wraith
Gavin Wraith <[hidden email]> wrote:
>
> On the topic of lpeg I am full of admiration and frustration.
> I am amazed that nobody (as far as I know) used an abstract datatype
> for patterns years ago.

SNOBOL.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Humber: North 4 or 5, backing northwest 3 or 4 later. Slight or moderate.
Showers. Good.

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Roberto Ierusalimschy
> Gavin Wraith <[hidden email]> wrote:
> >
> > On the topic of lpeg I am full of admiration and frustration.
> > I am amazed that nobody (as far as I know) used an abstract datatype
> > for patterns years ago.
>
> SNOBOL.

Which was the inspiration for LPeg.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Tony Finch
Roberto Ierusalimschy <[hidden email]> wrote:
> > Gavin Wraith <[hidden email]> wrote:
> > >
> > > On the topic of lpeg I am full of admiration and frustration.
> > > I am amazed that nobody (as far as I know) used an abstract datatype
> > > for patterns years ago.
> >
> > SNOBOL.
>
> Which was the inspiration for LPeg.

Also I forgot to mention the rich literature on parser combinators in
functional programming languages. PEGs came from Bryan Ford's work on
parser combinators for packrat parsers.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Cromarty, Forth, Tyne, Dogger: North or northwest 4 or 5, occasionally 3
later. Moderate, occasionally rough in Cromarty and Dogger. Showers. Good.

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Nils Nordman
In reply to this post by steve donovan
On Jun 12, 2012 1:13 PM, "steve donovan" <[hidden email]> wrote:

>
> I have just added a tutorial to the Wiki:
>
> http://lua-users.org/wiki/LpegTutorial

I've missed such a document myself when reading up on Lpeg. Very nice work Steve, thanks!

--
Nils Nordman <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

William Ahern
In reply to this post by steve donovan
On Tue, Jun 12, 2012 at 01:13:21PM +0200, steve donovan wrote:
> On Mon, Jun 11, 2012 at 4:32 AM, Glenn Edgar <[hidden email]> wrote:
> > I was trying to use peg module.  I could not figure it out.  Are there
> > simple complete programs.
>
> I have just added a tutorial to the Wiki:
>
> http://lua-users.org/wiki/LpegTutorial
>

Nice.

Part of the problem with PEGs, I think, is that people approach PEGs like
souped-up regular expressions. The problem is compounded the more experience
one has with regexes.

I was tweaking the LPeg Leg grammar. The author did a fine job of
translating the left-recursive elements to right-recursive. Except the final
solution used a nasty hack for prefixexp / Name disambigutation. The code
uses a global variable and a match-time assertion to make sure the correct
choice is followed.

The proper, pure PEG solution is a negative lookahead assertion on the set
of keywords in the grammar. And so that's what I ended up doing; and in fact
the entire grammar could be made much simpler once you exclude the keywords
from the set of allowable identifiers, although in this case I didn't
refactor things much.

Almost every single PEG and LPeg example I've seen treats lookahead
assertions as something equivalent to the ^ operator in Perl regexes; a
simple way to invert a character set. But lookahead assertions are much more
powerful than that, and in fact critical to using PEGs properly for complex
grammars. But experience with regular expressions is often a hindrance to
seeing the proper solution, even when it's staring you right in the face.

Not sure how to concisely describe the utility of lookahead assertions in a
tutorial format, though.



Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Andrew Starks
On Tue, Jun 12, 2012 at 4:04 PM, William Ahern
<[hidden email]> wrote:

> On Tue, Jun 12, 2012 at 01:13:21PM +0200, steve donovan wrote:
>> On Mon, Jun 11, 2012 at 4:32 AM, Glenn Edgar <[hidden email]> wrote:
>> > I was trying to use peg module.  I could not figure it out.  Are there
>> > simple complete programs.
>>
>> I have just added a tutorial to the Wiki:
>>
>> http://lua-users.org/wiki/LpegTutorial
>>
>
> Nice.
>
> Part of the problem with PEGs, I think, is that people approach PEGs like
> souped-up regular expressions. The problem is compounded the more experience
> one has with regexes.
...snip...
> Not sure how to concisely describe the utility of lookahead assertions in a
> tutorial format, though.
>
>
>

I might suggest that you have until November to figure it out.

Personally, I'd love to sit in on a presentation on this topic. :)


Best Regards,

Andrew Starks

Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Philippe Lhoste
In reply to this post by Tony Finch
On 12/06/2012 16:42, Tony Finch wrote:

> Roberto Ierusalimschy<[hidden email]>  wrote:
>>> Gavin Wraith<[hidden email]>  wrote:
>>>>
>>>> On the topic of lpeg I am full of admiration and frustration.
>>>> I am amazed that nobody (as far as I know) used an abstract datatype
>>>> for patterns years ago.
>>>
>>> SNOBOL.
>>
>> Which was the inspiration for LPeg.
>
> Also I forgot to mention the rich literature on parser combinators in
> functional programming languages. PEGs came from Bryan Ford's work on
> parser combinators for packrat parsers.

Scala has them, although a bit weak, according to those having used them.
The Parboiled library, usable from Java and Scala, implements them too, and seems quite
powerful. I haven't really tried it yet, but I plan to do it.
Of course, there are PEG implementations in lot of languages, but not necessarily as first
class objects.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

Re: Simple peg examples

Blake Sweeney
Quoting Philippe Lhoste (2012-06-13 04:04:36)

>On 12/06/2012 16:42, Tony Finch wrote:
>> Roberto Ierusalimschy<[hidden email]>  wrote:
>>>> Gavin Wraith<[hidden email]>  wrote:
>>>>>
>>>>> On the topic of lpeg I am full of admiration and frustration.
>>>>> I am amazed that nobody (as far as I know) used an abstract datatype
>>>>> for patterns years ago.
>>>>
>>>> SNOBOL.
>>>
>>> Which was the inspiration for LPeg.
>>
>> Also I forgot to mention the rich literature on parser combinators in
>> functional programming languages. PEGs came from Bryan Ford's work on
>> parser combinators for packrat parsers.
>
>Scala has them, although a bit weak, according to those having used them.
>The Parboiled library, usable from Java and Scala, implements them too, and seems quite
>powerful. I haven't really tried it yet, but I plan to do it.
>Of course, there are PEG implementations in lot of languages, but not necessarily as first
>class objects.

Ruby's parslet seems similar too.

http://kschiess.github.com/parslet/