From: Marco Antoniotti
Subject: Re: Parsing a List with infix-operators
Date: 
Message-ID: <QEQOc.24$Vn5.7240@typhoon.nyu.edu>
The INFIX package by Mark Kantrowitz in the AI.Repository is your friend.

Cheers

Marco




Stefan Ram wrote:
>   Given a list with infix notation:
> 
> ( 1 + 2 * 3 )
> 
>   There are several methods how to convert such a list to
>   Cambridge notation with the result:
> 
> ( add 1 ( multiply 2 3 ))
> 
>   Beside all of the classical ways to parse a sequence of tokens
>   (like a recursively descending parser), I think about of going
>   through the list several times with one loop for each level of
>   priority.
> 
>   An early loop would try to match the "pattern" x * y and
>   replace it by ( multiply x y ), giving:
> 
> ( 1 + ( multiply 2 3 ))
> 
>   To implement associativity, such a loop would proceed either
>   from the start to the end (left-associative) or from the end
>   to the start (right-associative).
> 
>   The loop for "+" would happen later to implement the lower
>   priority of "+" and finally create
> 
> ( add 1 ( multiply 2 3 ))
> 
>   (Conversion will be done recursively: Before looping, all
>   sublists will have been converted, so that nested sublists
>   always are already converted, when encountered in the loop.)
> 
>   This might be less run-time-efficient than a classical parser,
>   but I wonder whether it would be correct or does this approach
>   contain any mistakes? 
>