[antlr-interest] Unfolding of loops with antlr?

Jon Schewe jpschewe at mtu.net
Fri Dec 21 12:00:12 PST 2007


I figured it out.  Can someone let me know if I'm doing something
inherently bad here by mucking with the input stream directly and
calling the rule method directly?
loop
@init {
  Map<String, Integer> loopScope = new HashMap<String, Integer>();
  pushScope(loopScope);
}
:
^(
  'LOOP'
  ^(index=ID lb=numint ub=numint) {null == getValue($index.text) }? {
      System.out.println("Do loop with index: " + $index.text + " from "
+ $lb.value + " to " + $ub.value);
      // save the place we are at just before the constraints
      final int marker = ((CommonTreeNodeStream)input).mark();
      for(int counter=$lb.value; counter <= $ub.value; ++counter) {
        loopScope.put($index.text, counter);
        // jump to just before the constraints
        ((CommonTreeNodeStream)input).rewind(marker);
        constraints();
      }
  }
  //constraints
) /*{
    System.out.println("End of loop");
    for(int i=$lb.value; i<=$ub.value; ++i) {
      loopScope.put($ID.text, i);
      constraints(); // invoke constraints rule
    }
  }*/
;
finally {
  // remove loop scope
  popScope();
}

Jon Schewe wrote:
> I've got something I'd like to do with antlr and I'm not sure it's
> possible.  I want to unfold loops with antlr inside the parser.  The
> idea is this, say I've got an input file like below.  Now antlr has
> given me the ability to write nice rules to parse this language and to
> do various actions on each rule.  What I'd really like to do is to be
> able to tell antlr to repeat a rule action on the same input stream
> multiple times while I change some data outside.  Another option would
> be for me to be able to duplicate a set of tokens during parsing.  Say
> if I know the set of tokens between LOOP and END, could I tell antlr to
> duplicate that set of tokens 10 times (since the loop goes from 1 to 10)
> with an extra token added in to tell me the loop index during the
> initial parse and then let the tree parser just walk the tree?
>
> I'm sure I could hand the AST off to some custom method to do it, but
> that seems like I'm duplicating what antlr is already doing in the tree
> parser.
>
> Params
> i 5;
>
> Variables
> x[i];
> y;
>
> Constraints
> LOOP index 1...10:
>   loop[index]:
>   f[index] = 10 * x[index];
> END
>
> simple:
> g = 5 * y;
>
>   

-- 
Jon Schewe | http://mtu.net/~jpschewe
If you see an attachment named signature.asc, this is my digital
signature.
See http://www.gnupg.org for more information.

For I am convinced that neither death nor life, neither angels 
nor demons, neither the present nor the future, nor any 
powers, neither height nor depth, nor anything else in all 
creation, will be able to separate us from the love of God that 
is in Christ Jesus our Lord. - Romans 8:38-39



More information about the antlr-interest mailing list