[antlr-interest] Follow-up on line numbers

John B. Brodie jbb at acm.org
Wed Aug 10 14:41:56 PDT 2011


Sorry for jumping in where i may not belong....

On Wed, 2011-08-10 at 16:09 -0400, David Smith wrote:
> Sam,
> I understood the answer, but we're talking novice here - if the front 
> end of my grammar looks like this:
> 
> prog:   body EOF!
>          ;
> 
> body    :
>          parts -> ^(BODY parts)
>          ;
> 
> parts   :
>          ( (stat
>             | ifStat
>             | switchStat
>             | whileStat
>             | forStat)   )*
>          ;
> 
> stat:
>          (ID OPENP actualParameters CLOSEP DOT ID GETS)
>           => str=ID OPENP actualParameters CLOSEP  DOT fld=ID GETS expr
>           -> ^(GETS ?????? ^(STRUCTA $str $fld actualParameters) expr)
>      |   (ID OPENP actualParameters CLOSEP DOT OPENP)
>           => str=ID OPENP actualParameters CLOSEP DOT OPENP var=ID 
> CLOSEP GETS expr
>           -> ^(GETS ???????? ^(STRUCTAV $str $var actualParameters) expr)
> // etc
>      ;
> how would I insert the line number at the ???????

so rather than calling $stat.tree.getLine() in your tree walker's action
code when you need the location of the stat rule; you want to include
the line number as an explicit node in the tree?

if the later (e.g. want an explicit node) then i think you can just
construct an imaginary token to hold the line number as its text.

something like (untested):

foo : (...)=> x=ID '(' ... ')' '.'  y=ID z='=' expr
    -> ^($z LINE[$x, Integer.toString($x.getLine())] ^(...) expr)
  ;

> 
> DMS
> Message: 13
> Date: Wed, 10 Aug 2011 10:02:22 -0400
> From: David Smith <david.smith at cc.gatech.edu>
> Subject: [antlr-interest] Access to line numbers
> To: antlr-interest at antlr.org
> Message-ID: <E1Qr9MO-00037I-B1 at elasmtp-curtail.atl.sa.earthlink.net>
> Content-Type: text/plain; charset="us-ascii"; format=flowed
> 
> It seems that the parse tree generated by the parser has a data field
> 'line' that seems to correspond to the source line when parsing.  How
> do i access that to include the line number in the tree generated by
> my grammar?
> 
> DMS
> 
> David M. Smith http://www.cc.gatech.edu/fac/David.Smith
> Georgia Institute of Technology, College of Computing
> Sent from my ASR-33 Teletype
> 
> 
> 
> ------------------------------
> 
> Message: 14
> Date: Wed, 10 Aug 2011 09:36:28 -0500
> From: "Sam Harwell" <sharwell at pixelminegames.com>
> Subject: Re: [antlr-interest] Access to line numbers
> To: "'David Smith'" <david.smith at cc.gatech.edu>,
>          <antlr-interest at antlr.org>
> Message-ID: <06e601cc576a$e4a48980$aded9c80$@pixelminegames.com>
> Content-Type: text/plain;       charset="us-ascii"
> 
> Both the Tree and Token interfaces have a method getLine().
> 
> Sam
> 
>  >From: antlr-interest-bounces at antlr.org
>  >[mailto:antlr-interest-bounces at antlr.org] On Behalf Of David Smith
>  >Sent: Wednesday, August 10, 2011 9:02 AM
>   ...snip...
>  >List: http://www.antlr.org/mailman/listinfo/antlr-interest
>  >Unsubscribe:
>  >http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
> 
> 
> David M. Smith http://www.cc.gatech.edu/fac/David.Smith
> Georgia Institute of Technology, College of Computing
> Sent from my ASR-33 Teletype 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address





More information about the antlr-interest mailing list