Write a program that simulates a small programming language (SPL) with subprograms. The language has the following EBNF:

<program> ::= begin <statement> | <statement_sequence> end;
<statement_sequence> ::= <statement>; <statement_sequence>;
<statement> ::= <subprogram_invocation> | <other_statement>
<subprogram_invocation> ::= invoke <subprogram_identifier>
<other_statement> ::= increment <variable> | decrement <variable> |
output <variable> | stop_sub
<subprogram_identifier> ::= A | B | C
<variable> ::= X | Y | Z
Assumptions:
Every program automatically consists of environments for the main program and subprogram A, B, and C (even if not used). Each environment contains space for variables X, Y, and Z which are initialized to zero.

class Environment_Type
{
int X, Y, Z = 0;
}
Environment_Type Main, A, B, C = new Environment_Type ();
Sample Program
begin
increment X;
output X;
invoke A;
output X;
invoke B;
increment X;
increment X;
output X;
stop_sub;
output X;
stop_sub;
end;
It is erroneous to end a program when executing a subprogram.
Miscellaneous
program reads a text file

I have started and my next task is to check the length of the string and print the last character. This is the code i have so far

// create a fileScan Scanner for a file, Simple.txt
         Scanner fileScan = new Scanner(new File("simple.txt"));
         String inputString = "";
         String commandString;
      
         inputString =  fileScan.next();
         System.out.println (inputString);
         if (inputString.equals ("begin"))
         {
            System.out.println ("Parsing begin");
         	
                        
            
                    
            // check whether the fileScan has the next item (i.e., next line)
            while (fileScan.hasNext())	// check if there is any data to read
            {
               inputString =  fileScan.next();
               System.out.println (inputString);
               if (inputString.equals ("output"))
               {
                  System.out.print ("Parsing output  ");
                  commandString = fileScan.next ();
                  System.out.println ("The Command is: " + commandString); 
                  
               }
               else
                  if (inputString.equals ("invoke"))
                  {
                     System.out.print ("Parsing invoke  ");
                     commandString = fileScan.next ();
                     System.out.println ("The Command is: " + commandString); 
                  }
                  else
                     if (inputString.equals ("increment"))
                     {
                        System.out.print ("Parsing increment  ");
                        commandString = fileScan.next ();
                        System.out.println ("The Command is: " + commandString); 
                     }
                     else
                        if (inputString.equals ("stop_sub;"))
                        {
                           System.out.print ("Parsing stop_sub  ");
                           commandString = fileScan.next ();
                           System.out.println ("The Command is: " + commandString); 
                        }
                        else
                           if (inputString.equals ("decrement"))
                           {
                              System.out.print ("Parsing decrement  ");
                              commandString = fileScan.next ();
                              System.out.println ("The Command is: " + commandString); 
                           }
                           
            }
                     
                      
         }
            
         else//if information is not found in file then the program is false
            System.out.println ("Bad program");
      
      }
   }

this is the output

begin
Parsing begin
increment
Parsing increment The Command is: X;
output
Parsing output The Command is: X;
invoke
Parsing invoke The Command is: A;
output
Parsing output The Command is: X;
invoke
Parsing invoke The Command is: B;
increment
Parsing increment The Command is: X;
increment
Parsing increment The Command is: X;
output
Parsing output The Command is: X;
stop_sub;
Parsing stop_sub The Command is: output(this is where the problem is, I have started and my next task is to check the length of the string and print the last character. because the line should read:parsing stop_sub; and on the nextline output...and so on)
X;
stop_sub;
Parsing stop_sub The Command is: end;


Can anyone help please

Ezzaral commented: By 11 posts you should be able to use code tags. -2

Recommended Answers

All 6 Replies

if I understand correctly, what you want to do now is to get the length out of a String and print its last character:

public void printLastChar(String in){
  if (in != null && in.length() != 0){
     // there 's no use in printing what's not there, so don't print if there's a null value or 
    // an empty String passed on
    System.out.println(in.substring(in.length()-1, in.length());
  }
}

the problem looks to be this inside of stop_sub

commandString = fileScan.next ();

there is no command with it, therfore you are reading "output" inside of the stop_sub section

thank you so much, I checked it and that was the problem.

Thats looks write because I saw an example earlier but im not sure where to post it

I have considered your advice and this is what i have now

     // create a fileScan Scanner for a file, Simple.txt
     Scanner fileScan = new Scanner(new File("simple.txt"));
     String inputString;
     String commandString;

     inputString =  fileScan.next();
     System.out.println (inputString);
     if (inputString.equals ("begin"))
     {
        System.out.println ("Parsing begin");




        // check whether the fileScan has the next item (i.e., next line)
        while (fileScan.hasNext())  // check if there is any data to read
        {
           inputString =  fileScan.next();
           if (inputString.charAt(inputString.length()-1)==';')//
           {
              System.out.println (inputString);
              if (inputString.equals ("output"))
              {
                 System.out.print ("Parsing output  ");
                 commandString = fileScan.next ();
                 System.out.println ("The Command is: " + commandString); 

              }
              else
                 if (inputString.equals ("invoke"))
                 {
                    System.out.print ("Parsing invoke  ");
                    commandString = fileScan.next ();
                    System.out.println ("The Command is: " + commandString); 
                 }
                 else
                    if (inputString.equals ("increment"))
                    {
                       System.out.print ("Parsing increment  ");
                       commandString = fileScan.next ();
                       System.out.println ("The Command is: " + commandString); 
                    }
                    else
                       if (inputString.equals ("stop_sub;"))
                       {
                          System.out.println ("Parsing stop_sub  ");

                       }
                       else
                          if (inputString.equals ("decrement"))
                          {
                             System.out.print ("Parsing decrement  ");
                             commandString = fileScan.next ();
                             System.out.println ("The Command is: " + commandString); 
                          }
                          else
                             if (inputString.equals ("end;"))
                             {
                                System.out.println ("Parsing end ");

                             }

           }        
        }


     }

     else//if information is not found in file then the program is false
        System.out.println ("Bad program");

  }
}      

the output is this

begin
Parsing begin
X;
X;
A;
X;
B;
X;
X;
X;
stop_sub;
Parsing stop_sub  
X;
stop_sub;
Parsing stop_sub  
end;
Parsing end 

Ok, so you are meaning its correct then?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.