[antlr-interest] Treewalking : non-determinism error

mzukowski at yci.com mzukowski at yci.com
Wed Apr 2 08:02:18 PST 2003


I was assuming that you wanted to emit code that was for human consumption.
What are you making, an interpreter?  Postfix should be easier than infix.
The real question is if you want to bother with a tree parser for it.
Printing a tree postfix is a simple textbook excersize--assuming you create
the proper tree (which I heartily advocate.)

Monty

-----Original Message-----
From: Anthony Youngman [mailto:Anthony.Youngman at ECA-International.com]
Sent: Wednesday, April 02, 2003 12:43 AM
To: 'antlr-interest at yahoogroups.com'
Subject: RE: [antlr-interest] Treewalking : non-determinism error


Thanks Ric. Works a treat (I hope). 
By the way, Monty, why if I'm outputting code would I want to have my PRINT
before my EXPR? If my PRINT comes first, surely it hasn't yet got an EXPR to
print?
Okay okay, I know everybody thinks in infix, but postfix or rpn is a damn
sight easier to handle. Can I give you some advice? Namely it doesn't pay to
jump to conclusions :-) You're dealing with a guru here (just that he knows
bugger-all about Java or Antlr :-)
My next difficult task will be making the compiler chuck everything out IN
POSTFIX. 
Cheers, 
Wol 
-----Original Message----- 
From: Ric Klaren [mailto:klaren at cs.utwente.nl] 
Sent: 01 April 2003 15:40 
To: antlr-interest at yahoogroups.com 
Subject: Re: [antlr-interest] Treewalking : non-determinism error 


Hi, 
On Tue, Apr 01, 2003 at 11:56:28AM +0100, Anthony Youngman wrote: 
> I think I know what the problem is, I just don't know how to fix it ... 
> 
> In my main parser, I have the following two rules: 
> 
> printst : (pr:"PRINT"^ expr (COLONPRINT)? {System.out.println(pr+" print 
> ");} ); 
> expr : ( catexpr | logicexpr ) ; 
> 
> This seems to be fine - the traces are printing what I expect. 
> 
> In my treewalker I then have the rule: 
> 
> printst 
>       : #(PRINT expr COLONPRINT) {System.out.println("PRNT");} 
>       | #(PRINT expr) {System.out.println("OUTP");} 
>       ; 
Provided I don't have a coffee shortage or overdose ;) 
Tree parsers only use a lookahead of 1 so the above rule will indeed give 
nondeterminism. One way to fix it might be: 
printst 
        : ( #(PRINT expr COLONPRINT) ) => #(PRINT expr COLONPRINT)
{System.out.println("PRNT");} 
        | #(PRINT expr) {System.out.println("OUTP");} 
        ; 
Alternatively: 
printst { bool colon = false; } 
        : #(PRINT expr ( COLONPRINT { colon = true; } )? ) 
                { 
                        if( colon ) 
                                System.out.println("PRNT"); 
                        else 
                                System.out.println("OUTP"); 
                } 
        ; 
Cheers, 
Ric 
-- 
-----+++++*****************************************************+++++++++----
--- 
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ---- 
-----+++++*****************************************************+++++++++----
--- 
  "You know how to use that thing?" [pointing to the sword] 
  "Sure.. The pointy end goes into the other guy." 
  --- The Mask of Zorro 


 
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



This transmission is intended for the named recipient only. It may contain
private and confidential information. If this has come to you in error you
must not act on anything disclosed in it, nor must you copy it, modify it,
disseminate it in any way, or show it to anyone. Please e-mail the sender to
inform us of the transmission error or telephone ECA International
immediately and delete the e-mail from your information system.
Telephone numbers for ECA International offices are: Sydney +61 (0)2 9911
7799, Hong Kong + 852 2121 2388, London +44 (0)20 7351 5000 and New York +1
212 582 2333.

Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list