Re: Recursive descent parsing and optimization, was Good practical language and OS agnostic text?

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Sun, 22 Apr 2012 18:18:30 +0200

          From comp.compilers

Related articles
Good practical language and OS agnostic text? compilers@is-not-my.name (2012-04-17)
Re: Good practical language and OS agnostic text? ulimakesacompiler@googlemail.com (Uli Kusterer) (2012-04-21)
Re: Good practical language and OS agnostic text? cr88192@hotmail.com (BGB) (2012-04-21)
Re: Recursive descent parsing and optimization, was Good practical lan bc@freeuk.com (BartC) (2012-04-22)
Re: Recursive descent parsing and optimization, was Good practical lan mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2012-04-22)
Re: Recursive descent parsing and optimization, was Good practical lan cr88192@hotmail.com (BGB) (2012-04-22)
Re: Recursive descent parsing and optimization, was Good practical lan bartc@freeuk.com (Bartc) (2012-04-23)
| List of all articles for this month |

From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Newsgroups: comp.compilers
Date: Sun, 22 Apr 2012 18:18:30 +0200
Organization: cbb software GmbH
References: 12-04-019 12-04-056 12-04-060 12-04-066
Keywords: parse
Posted-Date: 22 Apr 2012 15:43:49 EDT

On Sun, 22 Apr 2012 12:51:44 +0100, BartC wrote:


> "BGB" <cr88192@hotmail.com> 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


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.