Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You were given the following grammar: E ->TE E-> +TE\' I e T-FT T\"- *FT\' I e F

ID: 3907524 • Letter: Y

Question

You were given the following grammar: E ->TE E-> +TE' I e T-FT T"- *FT' I e F-> (E) I id You used this grammar and created the FIRST the FOLLOW sets and constructed the parse table, shown below, in Lab 3. id E->TE E->TE E'-> eps E'-> eps +TE' T->FT T->FT T-*FT T'-> eps T'->eps epsilon F-> id You then parsed the string id id * id using the parse table and a stack. In this lab, you will implement the LL(1) parsing algorithm in Java to parse the following input: id + id * id If the algorithm parses this string, it will output "PARSED", otherwise it will report an error.

Explanation / Answer

The code for top down parser is below:
I have used E1 & T1 instead of E'.
I have given necessary comments along with code.
Need to give grammmer as input from commmand line.

import java.io.*;
import java.util.*;

public class ParseE0 {

    static StringTokenizer st;
    static String curr;

    /** read the next token into curr */
    static void next() {
   try {
        curr=st.nextToken().intern();
        // use of intern() allows us to check equality with ==.
   } catch( NoSuchElementException e) {
        curr=null;
   }
    }

    static void error(String msg) {
   System.err.println(msg);
   System.exit(-1);
    }

    static void parseE() {
   // E -> T E1
   parseT();
   parseE1();
    }

    static void parseE1() {
   // E1 -> * T E1 | epsilon
   if (curr=="+") {
        next();
        parseT();
        parseE1();
   } else if(curr==")" || curr=="$" ) {
   } else {
        error("Unexpected :"+curr);
   }
    }

    static void parseT() {
   // T -> F T1
   parseF();
   parseT1();
    }

    static void parseT1() {
   // T1 -> * F T1 | epsilon
   if (curr=="*") {
        next();
        parseF();
        parseT1();
   } else if(curr=="+" || curr==")" || curr=="$") {
   } else {
        error("Unexpected :"+curr);
   }
    }
  
    static void parseF() {
   // F -> ( E ) | a
   if( curr=="(") {
        next();
        parseE();
        if(curr==")") {
       next();
        } else {
       error (") expected.");
        }
   } else if(curr=="a") {
        next();
   } else {
        error("Unexpected :"+curr);
   }
    }

    public static void main(String args []) throws IOException {
   BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
   String line=in.readLine();
   st = new StringTokenizer(line+" $");
   next();
   parseE();
   if(curr=="$") {
        System.out.println("OK ");
   } else {
        error("End expected");
   }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote