Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!news.iecc.com!nerds-end From: "Bartc" Newsgroups: comp.compilers Subject: Re: Recursive descent parsing and optimization, was Good practical language and OS agnostic text? Date: Mon, 23 Apr 2012 10:59:20 +0100 Organization: virginmedia.com Lines: 37 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <12-04-078@comp.compilers> References: <12-04-019@comp.compilers> <12-04-056@comp.compilers> <12-04-060@comp.compilers> <12-04-066@comp.compilers> <12-04-069@comp.compilers> NNTP-Posting-Host: news.iecc.com X-Trace: leila.iecc.com 1335282946 60276 64.57.183.58 (24 Apr 2012 15:55:46 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Tue, 24 Apr 2012 15:55:46 +0000 (UTC) Keywords: parse, comment Posted-Date: 24 Apr 2012 11:55:46 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:621 "Dmitry A. Kazakov" wrote in message > On Sun, 22 Apr 2012 12:51:44 +0100, BartC wrote: >> 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: My scheme doesn't work tidily when some operators have right-to-left association. Fortunately I only have two such operators, assignment and power (:= and **), and some extra logic takes care of that. At least, the results are what you would expect. However, when I do something like A+B+:=C+D, then I might need a bit more logic to deal with that, to avoid parentheses.. -- Bartc [We theory weenies would use an operator precedence parser. It'd be about 12 lines of code and a tiny lookup table, and can easily handle arbitrary precedence and associativity. See the Wikipedia article for details and pseudocode. The original Ritchie C compiler used recursive descent for most of the language, and operator precedence for the expressions. It was two passes and fit in 24K bytes of RAM. -John]