Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.iecc.com!nerds-end From: "Dmitry A. Kazakov" Newsgroups: comp.compilers Subject: Re: Recursive descent parsing and optimization, was Good practical language and OS agnostic text? Date: Sun, 22 Apr 2012 18:18:30 +0200 Organization: cbb software GmbH Lines: 54 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <12-04-069@comp.compilers> References: <12-04-019@comp.compilers> <12-04-056@comp.compilers> <12-04-060@comp.compilers> <12-04-066@comp.compilers> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: news.iecc.com X-Trace: leila.iecc.com 1335123829 72172 64.57.183.58 (22 Apr 2012 19:43:49 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Sun, 22 Apr 2012 19:43:49 +0000 (UTC) Keywords: parse Posted-Date: 22 Apr 2012 15:43:49 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:612 On Sun, 22 Apr 2012 12:51:44 +0100, BartC wrote: > "BGB" wrote in message >> On 4/21/2012 2:22 AM, Uli Kusterer wrote: > >> although I use recursive descent, the above sounds different from what I >> usually do. > >> ReadStatement: >> checks for and handles vaious statement types >> calls ReadExpression >> >> ReadExpression: >> (actually, this is a tower of functions, one for each precedence >> level, working from lowest to highest precedence) > > Sounds like it's influenced by the C grammar, which defines expressions > using something like 13 or 17 layers of precedence. Rather by an attempt to describe precedence using grammar means. > Beyond about 3-4 levels, I found that unmanageable. For expression syntax, I > don't use any precedence in the grammar at all; I have precedence as an > attribute of an operator, and an expression can be parsed with a single > function. Yes, there is a very simple technique using two stacks, one for operands another for operations. > Or rather two: readfactor(priority), and readterm(). Readfactor() deals with > the binary operators linking successive terms, while readterm() does all > the real work (since my syntax doesn't distinguish between expressions and > statements, that's quite a big workload). Actually operations have two priorities; left and right. When left < right you have left to right association. When right > left, it becomes right to left. Some operators may have these priorities sufficiently different. For example the assignment operator. If your unlucky languages allows it, then A+B = C+D better be A+(B=(C+D)). That would require the following order of partial priorities: LP("=") << LP( "+") < RP("+") << RP("=") > Instead I read them as I go along, but with provision for a one-symbol > look-ahead. Same here. Except that I have one token look-ahead. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de