From: Brian Hurt <bhurt@spnz.org>
To: skaller <skaller@users.sourceforge.net>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Context Free Grammars?
Date: Fri, 13 Aug 2004 09:20:37 -0500 (CDT) [thread overview]
Message-ID: <Pine.LNX.4.44.0408130912420.4282-100000@localhost.localdomain> (raw)
In-Reply-To: <1092374548.29139.86.camel@pelican.wigram>
On 13 Aug 2004, skaller wrote:
> Perhaps someone can explain why this actually works ..??
Right recursion "works". The problem is that LALR parsers can do
something akin to tail recursion with left recursion that they can't do
with right recursion. Say, for example, you have the rules:
mul_expr: mul_expr '*' term
| mul_expr '/' term
| mul_expr MOD term
| term
term: NUMBER
Here, mul_expr is "left recursive"- the "recursive" call to mul_expr is on
the left hand side of the expression. Now, with an LALR parser, no mater
how many multiplications you had, it'd use constant stack space. Each
time it'd see a new multiplication term, it'd immediately collapse back
down to a mul_expr. An equally valid way to parse this would be right
recursively:
mul_expr: term '*' mul_expr
| term '/' mul_expr
| term MOD mul_expr
| term
The problem here is that if you had N multiplications in a row, it'd take
O(N) stack spaces on the parse tree to parse them all. This would still
work- right up until you hit the point where you have so many
multiplication terms that you blow stack.
It works, and there are times when it's the correct thing to use, but if
you have the choice between left recursive and right recursive grammars,
pick left recursive for LALR parsers.
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2004-08-13 14:12 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-08-12 17:18 David McClain
2004-08-12 18:02 ` Joshua Smith
2004-08-12 18:14 ` David McClain
2004-08-12 19:25 ` Paul Snively
2004-08-12 21:47 ` Erik de Castro Lopo
2004-08-13 5:22 ` skaller
2004-08-13 5:59 ` David Brown
2004-08-13 14:20 ` Brian Hurt [this message]
2004-08-13 4:45 ` skaller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.44.0408130912420.4282-100000@localhost.localdomain \
--to=bhurt@spnz.org \
--cc=caml-list@inria.fr \
--cc=skaller@users.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox