Re: Designing a language in which a class can define (not just overload) operators

Derk Gwen <derkgwen@HotPOP.com>
9 Sep 2003 22:57:20 -0400

          From comp.compilers

Related articles
Designing a language in which a class can define (not just overload) news@chunk.com.au (Richard Cartwright) (2003-08-20)
Re: Designing a language in which a class can define (not just overloa joshualevy@yahoo.com (2003-08-23)
Re: Designing a language in which a class can define (not just overloa tmk@netvision.net.il (2003-08-23)
Re: Designing a language in which a class can define (not just overloa rkrayhawk@aol.com (2003-09-04)
Re: Designing a language in which a class can define (not just overloa vidar@hokstad.name (2003-09-04)
Re: Designing a language in which a class can define (not just overloa nmm1@cus.cam.ac.uk (2003-09-09)
Re: Designing a language in which a class can define (not just overloa derkgwen@HotPOP.com (Derk Gwen) (2003-09-09)
| List of all articles for this month |

From: Derk Gwen <derkgwen@HotPOP.com>
Newsgroups: comp.compilers
Date: 9 Sep 2003 22:57:20 -0400
Organization: Quick STOP Groceries
References: 03-08-062 03-08-083
Keywords: parse, OOP
Posted-Date: 09 Sep 2003 22:57:20 EDT

# tmk@netvision.net.il (Michael Tiomkin) wrote in message news:03-08-083...
# > [Write your parser with generic expression rules like this:
# >
# > expr: expr OP5 expr /* precedence 5 operator */
# >
# > Then when you define a new operator at level 5, have the lexer return
# > an OP5 for it with semantic info so the parser can figure out which
# > operator it was. I'm pretty sure this trick was used in extensible
# > languages before 1980. -John]
#
# Unfortunately, this will need 100 rules for 100 priorities.


For some languages, you can assign two numbers to each terminal, its
left and right priorities, with 0 as the minimum, and 100 as the
maximum. The lexical phase does it thing and attaches the priorities
to each token. It adds a begin file and end file brackets.


Things like numbers and identifiers have priority (100,100).
Prefix operators have (100,x<100).
Infix operators have (x<100,y<100).
Left precedence has x>=y, right has x<y.
Open brackets have (100,0). Close brackets have (0,100).


The parser has a operator and operand stack, the operator stack
initially a start of file token, and the operand stack empty. The
parser then cycles through
if operator stack top right priority<100
and next token left priority<100
and next token can be a prefix operator (100,x)
then change the next token priority to (100,x)
if (operator stack top right priority<100) = (next token priority<100)
then it is an error
while operator stack top right priority>=next lexeme left priority
pop the operator and push it on operand stack
reduce the top n elements of the operand stack
if the popped operator priorities are
(100,100), n = 1
(100,x<100), n = 2
(x<100,100), n = 2
(x<100,y<100), n = 3
if the popped operator left priority=0
then exit this loop
push the next lexeme on to the operator stack


--
Derk Gwen http://derkgwen.250free.com/html/index.html


Post a followup to this message

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