help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to use parsing expressing grammar


From: Xah Lee
Subject: Re: how to use parsing expressing grammar
Date: Thu, 18 Dec 2008 01:24:10 -0800 (PST)
User-agent: G2/1.0

On Dec 17, 7:43 pm, Kevin Rodgers <kevin.d.rodg...@gmail.com> wrote:
> Xah Lee wrote:
> > There are 2 parsing expression grammars in elisp.
>
> >     *http://www.emacswiki.org/cgi-bin/wiki/ParserCompiler, (2008) by
> > Mike Mattie.
> >     *http://www.emacswiki.org/emacs/ParsingExpressionGrammars(2008)
> > by Helmut Eller.
>
> > The second one seems simpler, and i'm trying to learn it as a regex
> > replacement, but don't know how to use it.
>
> > Could anyone give concrete example in the following scenario?
>
> > For example, on my website i have things like:
>
> >  <hr>
> >  <p>Related essays:</p>
> >  <ul>
> >  <li><a href="someFilePath1">SomeTitleString2</a> someSring1</li>
> >  <li><a href="someFilePath2">SomeTitleString2</a> someSring2</li>
> >  ...
> >  </ul>
>
> > Suppose i want to change them to:
>
> >  <hr>
> >  <p>See also:</p>
> >  <p>
> >  <a href="someFilePath1">SomeTitleString1</a> someSring1<br>
> >  <a href="someFilePath2">SomeTitleString2</a> someSring2<br>
> >  ...
> >  </p>
>
> > How do i do it?
>
> Choose the right tool for the job:
>
> sed -e 's|^<p>Related essays:</p>$|<p>See also:</p>|' -e
> '/^<ul>$/,/^<\/ul>/{s|^<\(/*\)ul>$|<\1p>|;s|^<li>||;s|</li>$|<br>|;}'
>
> And since this is help-gnu-em...@gnu.org:
>
> C-x h
> C-u M-| sed -e ...

I'm mainly interested in learning about parsing expression grammar,
because its power is a order of magnitude higher than regex. In
practice, this just means it's the next generation of regex.

the sed code won't work in general... because as soon as you have some
other chars or slight variation, it stops working. You'll need to code
up a lot variations than conditional expressions. All the ~4000 html
pages on my website are valid html4. Even at the level of strictness
of valid xml, regex simply can't work.

if i wanted to, could've used Perl, which i'm a master, which is far
more powerful than sed. Even though emacs regex is less powerful than
perl, but in my opinion, the fact that you can move cursor about
freely due to elisp's buffer datatype, its power as a text processing
lang beats Perl despite Perl's more powerful regex. But still, they
all inferior to parsing expression grammar.

i think parsing expression grammar is so important that it should be
core of emacs soon, perhaps coded in C.

PS for those interested in PEG, Helmut Eller has given answer in
comp.emacs, here:
http://groups.google.com/group/comp.emacs/browse_frm/thread/acb19cb47f2e632f

See also:

• Text Processing: Elisp vs Perl
  http://xahlee.org/emacs/elisp_text_processing_lang.html

• Pattern Matching vs Lexical Grammar Specification
  http://xahlee.org/cmaci/notation/pattern_matching_vs_pattern_spec.html

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]