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 7679471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:53:05+00:00 2026-05-31T17:53:05+00:00

I am looking to write some pseudo-code of a recursive descent parser. Now, I

  • 0

I am looking to write some pseudo-code of a recursive descent parser. Now, I have no experience with this type of coding. I have read some examples online but they only work on grammar that uses mathematical expressions. Here is the grammar I am basing the parser on.

S -> if E then S | if E then S else S | begin S L | print E

L -> end | ; S L

E -> i

I must write methods S(), L() and E() and return some error messages, but the tutorials I have found online have not helped a lot. Can anyone point me in the right direction and give me some examples?.

I would like to write it in C# or Java syntax, since it is easier for me to relate.


Update

public void S() {
    if (currentToken == "if") {
        getNextToken();
        E();

        if (currentToken == "then") {
            getNextToken();
            S();

            if (currentToken == "else") {
                getNextToken();
                S();
                Return;
            }
        } else {
            throw new IllegalTokenException("Procedure S() expected a 'then' token " + "but received: " + currentToken);
        } else if (currentToken == "begin") {
            getNextToken();
            S();
            L();
            return;
        } else if (currentToken == "print") {
            getNextToken();
            E();
            return;
        } else {
            throw new IllegalTokenException("Procedure S() expected an 'if' or 'then' or else or begin or print  token " + "but received: " + currentToken);
        }
    }
}


public void L() {
    if (currentToken == "end") {
        getNextToken();
        return;
    } else if (currentToken == ";") {
        getNextToken();
        S();
        L();
        return;
    } else {
        throw new IllegalTokenException("Procedure L() expected an 'end' or ';' token " + "but received: " + currentToken);
    }
}


public void E() {
    if (currentToken == "i") {
        getNextToken();
        return;
    } else {
        throw new IllegalTokenException("Procedure E() expected an 'i' token " + "but received: " + currentToken);
    }
}
  • 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-31T17:53:07+00:00Added an answer on May 31, 2026 at 5:53 pm

    Basically in recursive descent parsing each non-terminal in the grammar is translated into a procedure, then inside each procedure you check to see if the current token you are looking at matches what you would expect to see on the right hand side of the non-terminal symbol corresponding to the procedure, if it does then continue applying the production, if it doesn’t then you have encountered an error and must take some action.

    So in your case as you mentioned above you will have procedures: S(), L(), and E(), I will give an example of how L() could be implemented and then you can try and do S() and E() on your own.

    It is also important to note that you will need some other program to tokenize the input for you, then you can just ask that program for the next token from your input.

    /**
     * This procedure corresponds to the L non-terminal
     * L -> 'end'
     * L -> ';' S L
     */ 
    public void L()
    {
       if(currentToken == 'end')
       {
          //we found an 'end' token, get the next token in the input stream
          //Notice, there are no other non-terminals or terminals after 
          //'end' in this production so all we do now is return
          //Note: that here we return to the procedure that called L()
          getNextToken();
          return; 
       } 
       else if(currentToken == ';')
       {
          //we found an ';', get the next token in the input stream
          getNextToken();
          //Notice that there are two more non-terminal symbols after ';'
          //in this production, S and L; all we have to do is call those
          //procedures in the order they appear in the production, then return
          S();
          L();
          return;
       }
       else
       {
          //The currentToken is not either an 'end' token or a ';' token 
          //This is an error, raise some exception
          throw new IllegalTokenException(
              "Procedure L() expected an 'end' or ';' token "+
              "but received: " + currentToken);
       }
    }
    

    Now you try S() and E(), and post back.

    Also as Kristopher points out your grammar has something called a dangling else, meaing that you have a production that starts with the same thing up to a point:

    S -> if E then S 
    S -> if E then S else S
    

    So this begs the question if your parser sees an ‘if’ token, which production should it choose to process the input? The answer is it has no idea which one to choose because unlike humans the compiler can’t look ahead into the input stream to search for an ‘else’ token. This is a simple problem to fix by applying a rule known as Left-Factoring, very similar to how you would factor an algebra problem.

    All you have to do is create a new non-terminal symbol S’ (S-prime) whose right hand side will hold the pieces of the productions that aren’t common, so your S productions no becomes:

    S  -> if E then S S'
    S' -> else S 
    S' -> e   
    (e is used here to denote the empty string, which basically means there is no   
     input seen)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking into this for some hours now and can't figure it
I am looking to write some C# code for linux/windows/mac/any other platform, and am
I'm looking to write some very timing sensitive code on an LPC1343 where I
I'm looking to write some Groovy code to perform tasks inside of install anywhere
I'm an avid vim user and have started to write some SQL code recently.
I have the pleasure to write some code that moves around stuff in an
I'm a newbie to Python and I'm looking at using it to write some
I am testing some weird-looking CSS code that I wrote (I'm using a mix
I'm trying to write some abstract code for searching through a list of similar
I am just looking to write some annotation which can execute at runtime, before

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.