[antlr-interest] Changing types and text in the parser ...

Anthony Youngman Anthony.Youngman at ECA-International.com
Wed May 19 08:09:05 PDT 2004


Thanks. I'll have to play. I suspect it'll be harder than it looks,
though, thanks to the position of my parentheses :-)

Cheers,
Wol 

-----Original Message-----
From: Ric Klaren [mailto:klaren at cs.utwente.nl] 
Sent: 19 May 2004 16:05
To: antlr-interest at yahoogroups.com
Subject: Re: [antlr-interest] Changing types and text in the parser ...

On Wed, May 19, 2004 at 03:52:21PM +0100, Anthony Youngman wrote:
> Simple little problem, just I don't know how to do it, and it doesn't
> work like it does in the lexer ...
> 
> gotost : (GOTO^ (INT { $setType( IDENT); } | IDENT) (COLON | {
> $append(":"); } ) )
> // if int, we want to change the type to ident. If no colon, then
append
> it to text.
> 	;
> 
> GOTO can be followed by either an INT or an IDENT, and then an
optional
> colon. (And I think my grammar is slightly wrong here too ...)
> 
> What I want to end up with is GOTO^ IDENT, where the text of IDENT has
a
> trailing colon.
> 
> Actually, I've just realised I'm well off down the wrong track here, I
> can just trap all of this in the treeparser, but (a) I would still
like
> to know how to do it, and (b) for Ter - the manual covers lexing and
> parsing as being pretty much the same thing. I would have thought
things
> like this are a major difference that should be documented and I can't
> find it in the lexing chapter...

In the parser you should be able to do something like:

parser_rule: 
	t1:TOKEN1 { t1.setText("sometext"); t1.setType(MY_TYPE); }
	t2:TOKEN2 ;	

'Problems' may of course arise if the tree is built before you change
the
attributes, which may lead to confusion... So you may have to patch the
generated tree as well (or play a bit with the action code position and
check the generated code to see if it does the right thing)

Cheers,

Ric
--
-----+++++*****************************************************+++++++++
-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++
-------
 Why don't we just invite them to dinner and massacre them all when
they're
  drunk? You heard the man. There's seven hundred thousand of them. Ah?
..
           So it'd have to be something simple with pasta, then.
                 From: Interesting Times by Terry Pratchet



 
Yahoo! Groups Links



 





****************************************************************************

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 8272 5300, Hong Kong + 852 2121 2388, London +44 (0)20 7351 5000 and New York +1 212 582 2333.

****************************************************************************



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

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



More information about the antlr-interest mailing list