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

  • SEARCH
  • Home
  • 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 8457479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:49:08+00:00 2026-06-10T12:49:08+00:00

I’m trying to implement a parser by directly reading a treeWalker and implementing the

  • 0

I’m trying to implement a parser by directly reading a treeWalker and implementing the commands needed for the compiler on the fly. So if I have a command like:

 statement
            :
            ^('WRITE' expression) 
            { 
                //Here is the command that is created by my Tree Parser
                ch.emitRO("OUT",0,0,0,"write out the value of ac");
                //and then I handle it in my other classes
            }
;

I want it to write OUT 0,0,0; to a file. That’s my grammar.

I have a problem though with the loop section in my grammar it is:

'WHILE'^ expression 'DO' stat_seq 'ENDDO' 

and in the tree parser:

doWhileStatement
:
^('WHILE' expression 'DO' stat_seq 'ENDDO')
;

What I want to do is directly parse the code from the while loop into the commands I need. I came up with this solution but it doesn’t work:

doWhileStatement
        :
        ^('WHILE' e=expression head='DO'
            {
                int loopHead =((CommonTree) head).getTokenStartIndex();

            }

            stat_seq

            {
                if ($e.result==1) {
                     input.seek(loopHead);
                     doWhileStatement();
                }
            }
         'ENDDO')
; 

for the record here are some of the other commands I’ve written:
(ignore the code written in brackets, it’s for the generation of the commands in a text file.)

    stat_seq
            :
            (statement)+
            ;
statement
        :
        ^(':=' ID e=expression) { variables.put($ID.text,e); }
        | ^('WRITE' expression) 
        { 
            ch.emitRM("LDC",ac,$expression.result,0,"pass the expression value to the ac reg");
            ch.emitRO("OUT",ac,0,0,"write out the value of ac");
        }
        | ^('READ' ID)
        {
            ch.emitRO("IN",ac,0,0,"read value");
        }
        | ^('IF' expression 'THEN'
        {
            ch.emitRM("LDC",ac1,$expression.result,0,"pass the expression result to the ac reg");
            int savedLoc1 = ch.emitSkip(1);
        }
        sseq1=stat_seq  
        'ELSE'
        {
            int savedLoc2 = ch.emitSkip(1);
            ch.emitBackup(savedLoc1);
            ch.emitRM("JEQ",ac1,savedLoc2+1,0,"skip as many places as needed depending on the expression");
            ch.emitRestore();
        }
         sseq2=stat_seq
         {
            int savedLoc3 = ch.emitSkip(0);
            ch.emitBackup(savedLoc2);
            ch.emitRM("LDC",PC_REG,savedLoc3,0,"skip for the else command");
            ch.emitRestore();
         }

          'ENDIF')
        | doWhileStatement
        ;

Any help would be appreciated, thank you

  • 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-06-10T12:49:09+00:00Added an answer on June 10, 2026 at 12:49 pm

    I found it for everyone who has the same problem I did it like this and it’s working:

    ^('WHILE'  
    
            {int c = input.index();}
            expression   
    
            {int s=input.index();}
            .* )// .* is a sequence of statements
    
            {
            int next = input.index(); // index of node following WHILE
            input.seek(c);
            match(input, Token.DOWN, null); 
            pushFollow(FOLLOW_expression_in_statement339);
            int condition = expression();
    
            state._fsp--;            
            //there is a problem here
            //expression() seemed to be reading from the grammar file and I couldn't
            //get it to read from the tree walker rule somehow
            //It printed something like no viable alt at input 'DOWN'
            //I googled it and found this mistake
            // So I copied the code from the normal while statement
            // And pasted it here and it works like a charm
            // Normally there should only be int condition = expression()   
    
            while ( condition == 1 ) {
                input.seek(s);
                stat_seq();//stat_seq is a sequence of statements: (statement ';')+
                input.seek(c);
    
                match(input, Token.DOWN, null); //Copied value from EvaluatorWalker.java
                //cause couldn't find another way to do it
                pushFollow(FOLLOW_expression_in_statement339);
                condition = expression();
    
                state._fsp--;
                System.out.println("condition:"+condition + " i:"+ variables.get("i"));
            }
            input.seek(next);
         }
    

    I wrote the problem at the comments of my code. If anyone can help me out and answer this for me how to do it I would be grateful. It’s so weird that there is nearly no feedback on a correct way to implement loops within a tree grammar on the fly.

    Regards,
    Alex

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.