[antlr-interest] Help with a production rule

Matthew Ford Matthew.Ford at forward.com.au
Sat Feb 9 12:23:24 PST 2002


Alex,

No I don't think you need massive lookahead.
You need only lookahead far enough to check for
WORD:WORD>
This assumes WORD cannot include COLON or LEFT_ANGLE or RIGHT_ANGEL


Here is some pseudo Antlr  (I have not tried to compile this)

In the parser
boolean start = true;
String startX, startY;



input
 : checkForDelimiters  // handles < and delimiters
 |  WORD
 |  COLON
 |  RIGHT_ANGLE
;

protected
checkForDelimiters
{ boolean foundDelimiter = false;
}
:  (la:LEFT_ANGLE  ((delimiter)=> delimiter {foundDelimiter=true)?  // does
this start a delimiter
   )  {
         if (!foundDelimiter) {
                 // add la to input
         }
       }
   ;

protected
delimiter:
// skip the LEFT_ANGLE as this is pickup in the calling rule
x:WORD COLON y:WORD RIGHT_ANGLE
  {
     if (start) {
         startingX = x.getText();
         startingY = y:getText()
         start = false;  // if you have nested <w:w> you will need a stack
and more checking
      } else {
        // check ending match
        if ( !startingX.equals(x1.getText()) ||
!startingY.equals(y1.getText()) ) {
                // no match
                // throw parseException or your own exception
        }
 }

matthew
----- Original Message -----
From: "Chuck Siska" <chuck.siska at nb.conexant.com>
To: <antlr-interest at yahoogroups.com>
Sent: Sunday, February 10, 2002 3:17 AM
Subject: Re: [antlr-interest] Help with a production rule


> alex --
>
> without massive lookahead (and its attendant slowdown) you'll
> want to register the starting symbol tuples in a symbol table
> and then give an error when you can finally determine that the
> corresponding ending tuple is missing (e.g., at the end of
> input/file, after 500 lines, at the end of some key outer
> symbol tuple).
>
> -- chuck
>
> alexworden wrote:
> >
> > Hi,
> >
> > Can anyone help me. I'm trying to write an ANTLR rule that will match
> > something like
> >
> > <x1:y2> blah blah blah </x2:y2>
> >
> > where x1==x2 and y1==y2 but I have not prior knowledge what these
> > tokens might be. e.g.
> >
> > <foo:bar>
> >  some other stuff
> > </foo:bar>
> >
> > I've tried something like:
> >
> >   : "<" x:WORD ":" y:WORD ">" MATCH_ALL "<" x.getText() ":" y.getText
> > () ">"
> >
> > but ANTLR doesn't like it. Does anyone know how to do this please?
> >
> > Thanks,
> >
> > Alex
>
> --
>                                            |\_/\_.-'""``:-._
> What is life without looking for           . . `; -._      )-;-,_`)
> the next cute little bug to play with?     v_,-    _  ),(,.\  ``-'
>                                           _.- _.,-_/ /  ((.'
> -- chuck.siska at conexant.com  `<}:..     ((,.-'   ((,/
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


 

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



More information about the antlr-interest mailing list