Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: RFC: Parsing with perl - Regexes and beyond

by blokhead (Monsignor)
on Apr 03, 2008 at 15:25 UTC ( #678184=note: print w/replies, xml ) Need Help??

in reply to RFC: Parsing with perl - Regexes and beyond

When talking about lexing, you say:
Parsing CFLs takes O(n) time.
This is a little misleading. LALR parsers take linear time (where the size of its lookup tables is taken to be a constant, since it is independent of the input string to be parsed), while recursive descent can in principle take exponential time.

I'm guessing you are thinking of the CKY algorithm, which does take cubic time. This algorithm's importance is strictly theoretical, since it can be used to efficiently parse completely arbitrary grammars. In practice, we exploit the extra structure possesed by most practical grammars to obtain faster parsing than CKY provides.

So I don't think lexing is a matter of running-time efficiency. Its benefit is that if you blindly parse a character stream without lexing, you would have a separate leaf in the parse tree for every character (including whitespace!). Tokens like /\d+/, if expressed directly in the context-free grammar and parsed one character at a time, would then lead to very unwieldy parse trees like this:

"123456" --> [1, [2, [3, [4, [5, 6]]]]]
Lexing "collects" all these nasty bottom sections of the parse tree, which would otherwise be everywhere, into chunks that reflect how the parse tree will be used. Of course this could also be cleaned up with special cases in the parser, but lexing separates out this step in an elegant way.

PS: "Chompsky" (with a p) is a name commonly given to pet dogs by linguists... either as an insult or homage to Chomsky, depending on which side of the great linguistic divide you fall ;)


Replies are listed 'Best First'.
Re^2: RFC: Parsing with perl - Regexes and beyond
by moritz (Cardinal) on Apr 03, 2008 at 16:03 UTC
    This is a little misleading.

    Well, it's the worst case runtime. In the normal case it's not that bad, but the fact remains that in general lexing is more efficient than parsing.

    You're right that most grammars can be parsed in linear time (actually many grammars have their current shape to facilitate parsing).

Re^2: RFC: Parsing with perl - Regexes and beyond
by dwm042 (Priest) on Apr 03, 2008 at 19:00 UTC
    When I was a teen, there was a chimpanzee named Nim Chimpsky that was far better known among my peers than any dog named Chompsky or any linguist named Chomsky.

    They used the animal for sign language experiments.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://678184]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-03-02 14:20 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (50 votes). Check out past polls.