Question

NEED THIS SOON. Recursive Descent Parsing Consider the following BNF grammar: A -> I = E...

NEED THIS SOON.

Recursive Descent Parsing

Consider the following BNF grammar:

A -> I = E
E -> P O P | P
O -> + | - | * | / | **
P -> I | L | UI | UL | (E)
U -> + | - | !
I -> C | CI
C -> a | b | ... | y | z
L -> D | DL
D -> 0 | 1 | ... | 8 | 9
        

Using the technique described in class implement a recursive descent parser that recognizes strings in this language. Input should be from a file called input.txt and output should be to a file called output.txt. An example session might look like this (these strings are not necessarily in the language):

The string "a=a+b-c*d" is in the language.
The string "a=a//b++c" is not in the language.
        

You must implement the project in JAVA. Implementations that do not include a solution in both languages will, at best, receive half credit. To simplify things you will not have to handle whitespace when parsing the string, i.e. " " and similiar are illegal characters in this language. All strings should read from a file called "input.txt" and written to a file called "output.txt".

BOTH CODE NEEDS TO BE CORRECT AND NEEDS TO ACCEPT AN INPUT FILE AND NEED AN OUTPUT FILE. ONLY NEED JAVA CODE

ALSO PLEASE TYPE IT OUT.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class RecursiveDescent
{
   static int ptr;
   static char[] input;
   public static void main(String[] args)
   {
       String str="";
       BufferedReader br = null;
       FileReader fr = null;
       try {

           fr = new FileReader("D:\\New folder\\string.txt");
           br = new BufferedReader(fr);
           br = new BufferedReader(new FileReader("D:\\New folder\\string.txt"));
           str= br.readLine();
           System.out.println("The Given string is"+str);
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
       input = str.toCharArray();
       if(input.length < 2)
       {
           System.out.println("The input string is invalid.");
           System.exit(0);
       }
       ptr = 0;
       boolean isValid = E();
       if((isValid) & (ptr == input.length))
       {
           System.out.println("The input string is valid.");
       }
       else
       {
           System.out.println("The input string is invalid.");
       }
   }
  
   static boolean E()
   {
       int fallback = ptr;
       if(input[ptr++] != '|')
       {
           ptr = fallback;
           return false;
       }
       if(P() == false)
       {
           ptr = fallback;
           return false;
       }
       if(O() == false)
       {
           ptr = fallback;
           return false;
       }
      
       return true;
   }
  
   private static boolean O()
   {
       int fallback = ptr;
       if(input[ptr]!= '+')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '|')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '*')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '/')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '-')
       {
           ptr = fallback;
           return false;
       }
       return true;
   }
   static boolean P()
   {
       int fallback = ptr;
       if(input[ptr]!= '|')
       {
           ptr = fallback;
           return false;
       }
       if(I()==false)
       {
           ptr = fallback;
           return false;
       }
       if(L()==false)
       {
           ptr = fallback;
           return false;
       }
       if(U()==false)
       {
           ptr = fallback;
           return false;
       }
       else
       {
           if(input[ptr++] != '(')
           {
               ptr = fallback;
               return false;
           }
           if(E() == false)
           {
               ptr = fallback;
               return false;
           }
           if(input[ptr++] != ')')
           {
               ptr = fallback;
               return false;
           }
           return true;
       }
   }
   private static boolean L()
   {
       int fallback=ptr;
       if(input[ptr]!= '|')
       {
           ptr = fallback;
           return false;
       }
       if(D() == false)
       {
           ptr = fallback;
           return false;
       }
       return false;
   }
   static boolean D()
   {
       int fallback = ptr;
       int value = (int)input[ptr];
       if(value >= 65 && value <= 90)
       {
           ptr = fallback;
           return true;
       }
       return false;
   }
   private static boolean U()
   {
       int fallback = ptr;
       if(input[ptr]!= '+')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '|')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '-')
       {
           ptr = fallback;
           return false;
       }
       if(input[ptr]!= '!')
       {
           ptr = fallback;
           return false;
       }
       return true;
   }

   private static boolean I()
   {
       int fallback = ptr;
       if(input[ptr]!= '|')
       {
           ptr = fallback;
           return false;
       }
       if(C()==false)
       {
           ptr = fallback;
           return false;
       }
       return true;
   }
   private static boolean C()
   {
       int fallback = ptr;
       if(input[ptr]>='a' &&input[ptr]<='z')
       {
           ptr = fallback;
           return true;
       }
       return false;
   }

}

Add a comment
Know the answer?
Add Answer to:
NEED THIS SOON. Recursive Descent Parsing Consider the following BNF grammar: A -> I = E...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursive Descent Parsing Consider the following BNF grammar: A -> I = E E -> P...

    Recursive Descent Parsing Consider the following BNF grammar: A -> I = E E -> P O P | P O -> + | - | * | / | ** P -> I | L | UI | UL | (E) U -> + | - | ! I -> C | CI C -> a | b | ... | y | z L -> D | DL D -> 0 | 1 | ... | 8 |...

  • Please write a recursive descent parser (including a lexical analyzer) for the following EBNF in C....

    Please write a recursive descent parser (including a lexical analyzer) for the following EBNF in C. Your program codes should be runnable. <exprs> -> <expr>; {<expr>;} <expr> -> <term> { (+ | -) <term> } <term> -> <factor> { (*|/ ) <factor> } <factor> -> <exp> {^ <exp>} <exp> -> id | int_lit | real_lit | (<expr>) where, ^ indicates the power operation, id is a legal identifier name, int_lit represents any positive integer number, and real_lit represents any positive...

  • please provide good explanation. Consider the following grammar for variable and class declarations in Java: <Decl>...

    please provide good explanation. Consider the following grammar for variable and class declarations in Java: <Decl> -> <VarDecl> | <ClassDecl> <VarDecl> -> <Modifiers> <Type> <VarDec> SEM <ClassDecl> -> <Modifiers> CLASS ID LBRACE <DeclList> RBRACE <DeclList> -> <Decl> | <DeclList> <Decl> <VarDec> -> ID | ID ASSIGN <Exp> | <VarDec> COMMA ID | <VarDec> COMMA ID ASSIGN <Exp> Indicate any problems in this grammar that prevent it from being parsed by a recursive-descent parser with one token lookahead. You can simply...

  • Please help me with the coding for LL(1)!! The given grammar was: P → PL |...

    Please help me with the coding for LL(1)!! The given grammar was: P → PL | L L → N; | M; | C N → print E M → print "W" W → TW | ε C → if E {P} | if E {P} else {P} E → (EOE) | V (note: this has a variable O) O → + | - | * V → 0 | 1 | 2 | 3 (note: this has a terminal...

  • java find and replace code pls help me We write code that can find and replace...

    java find and replace code pls help me We write code that can find and replace in a given text file. The code you write should take the parameters as command line arguments: java FindReplace -i <input file> -f "<find-string>" -r "<replace-string> -o <output file> *question mark(?) can be used instead of any character. "?al" string an be sal ,kal,val *In addition, a certain set of characters can be given in brackets.( "kng[a,b,f,d,s]ne" string an be kngane,hngbne,kangfne,kangdne,kangsne So, all you...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • I need to construct a c++ class that defines a parser. This parser will be able...

    I need to construct a c++ class that defines a parser. This parser will be able to break up a string into tokens. A token is a distinct string of text – it can be a word, a symbol, or a combination of these. Your parser will eliminate whitespace and break string snippets into individual tokens (where single-character tokens are defined at runtime). Your parser should behave as follows: 1) Whitespace (spaces, tabs, and new lines) is used only to...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

  • Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And...

    Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters.   For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT