Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 741983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:41:43+00:00 2026-05-14T08:41:43+00:00

I want to parser a simple matlab-like for-loop, using ANTLR. The loop is like

  • 0

I want to parser a simple matlab-like for-loop, using ANTLR.

The loop is like :

for i=1:8
    y(i) = a(i) + i;
end

I want to parse the loop and parse 8 times the y(i) = a(i) + i statement, in order to do some actions on each statement.

My rule is as follows (actions are described in C#) :

forloop
@init
    {
        string c = "";
        int mark = input.Mark();
    }
@after
    {
        if (constants[c] < $i2.value) {
            SetConst(c, constants[c] + 1);
            input.Rewind(mark);
        }
    }
: 'for' IDENT '=' i1=constant ':' i2=constant NEWLINE
    {
        c = $IDENT.text;
        if (!IsConst(c)) {
            AddConst(c, $i1.value);
        }
    }
  statements?
  'end'
;

Actually, when ANTLR parses the statements rule, it triggers some actions. So, here, I tell ANTLR that i is a constant which value is 1 for starters, and then I want to reiterate the statements parsing, while incrementing my i constant.

To reiterate, I use input.Mark() and input.Rewind(), but it does not work as I expect, and some error is raised by ANTLR, telling me that some “NEWLINE” tokens are not present at the ‘for’ keyword.

How can I handle loop-parsing if I want to trigger some actions until the loop is over ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:41:44+00:00Added an answer on May 14, 2026 at 8:41 am

    I found the solution.
    Actually, input.Rewind() does not act like a goto, as I first expected. It just simply restores the input buffer to a previous state, defined by input.Mark().

    So, when reaching the end of my for-loop, if the condition was still true, I reinjected the whole for-loop into the input buffer.

    BUT ! In another rule, I told ANTLR that a for-loop MUST be followed by a NEWLINE. And in my case, through the reinjection, the first-pass-for-loop was immediatly followed by the second-pass-for-loop, resulting in this construction :

    for i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    endfor i=1:8
        y(i) = a(i) + i;
    end
    

    Of course, since no NEWLINE followed the first 7th loops, ANTLR reported an error.

    The solution was to simply tell ANTLR that a for-loop needs not to be followed by a NEWLINE. It works like a charm, but I’m not that much satisfied by this result…

    The final for-loop rule looks like (this is just a code cleanup) :

    forloop
        @init {int mark = input.Mark();}
        : 'for' IDENT '=' i1=constant ':' i2=constant NEWLINE
            {
                string c = $IDENT.text;
                if (!IsConst(c)) {
                    AddConst(c, $i1.value);
                }
            }
          statements? 'end'
            {
                if (constants[c] < $i2.value) {
                    SetConst(c, constants[c] + 1);
                    input.Rewind(mark);
                }
            }
        ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Visit history is merely a collection of visits in the… May 14, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer You need to use the selectAnnotation message on MKMapView: -… May 14, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer Anticipate and invest in change. Many businesses seem to think… May 14, 2026 at 7:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.