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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:21:31+00:00 2026-05-20T11:21:31+00:00

I have been working on a project in which I take a file with

  • 0

I have been working on a project in which I take a file with Backus–Naur Form grammar notation and generate sentences with it. Here is the BNF file I am working off of:

<s>::=<np> <vp>
<np>::=<dp> <adjp> <n>|<pn>
<pn>::=John|Jane|Sally|Spot|Fred|Elmo
<adjp>::=<adj>|<adj> <adjp>
<adj>::=big|fat|green|wonderful|faulty|subliminal|pretentious
<dp>::=the|a 
<n>::=dog|cat|man|university|father|mother|child|television
<vp>::=<tv> <np>|<iv>
<tv>::=hit|honored|kissed|helped
<iv>::=died|collapsed|laughed|wept

Almost everything is working fine, with the exception of anytime the letter “a” is introduced via the rule set. When that happens, I recieve the following error:

Exception in thread “main”
java.lang.NullPointerException
at GrammarSolver.generate(GrammarSolver.java:95)
at GrammarSolver.generate(GrammarSolver.java:109)
at GrammarSolver.generate(GrammarSolver.java:116)
at GrammarSolver.generate(GrammarSolver.java:116)
at GrammarSolver.(GrammarSolver.java:51)
at GrammarTest.main(GrammarTest.java:19)

I have been trying to trace and locate the cause of this error, but have been unable to do so. So I am seeking the advice of someone with perhaps a bit more experience to show me where my bug is so I can understand what is causing it, and avoid replicating similar mistake in the future.

The code for my program is as follows:

import java.util.*;
import java.util.regex.*;

class GrammarSolver {

    //Create output variable for sentences
    String output = "";

    //Create a map for storing grammar
    SortedMap<String, String[]> rules = new TreeMap<String, String[]>();

    //Create a queue for managing sentences
    Queue<String> queue = new LinkedList<String>();

    /**
     * Constructor for GrammarSolver
     *
     * Accepts a List<String> then processes it splitting
     * BNF notation into a TreeMap so that "A ::= B" is
     * loaded into the tree so the key is A and the data
     * contained is B
     *
     * @param       grammar     List of Strings with a set of
     *                          grammar rules in BNF form.
     */
    public GrammarSolver(List<String> grammar){
        //Convert list to string
        String s = grammar.toString();

        //Split and clean
        String[] parts = s.split("::=|,");
        for(int i = 0; i < parts.length; i++){
            parts[i] = parts[i].trim();
            parts[i] = parts[i].replaceAll("\\[|]", "");
            //parts[i] = parts[i].replaceAll("[ \t]+", "");

        }
        //Load into TreeMap
        for(int i = 0; i < parts.length - 1; i+=2){
            String[] temp = parts[i+1].split("\\|");
            rules.put(parts[i], temp);
        }

        //Debug
        String[] test = generate("<s>", 2);
        System.out.println(test[0]);
        System.out.println(test[1]);
    }

    /**
     * Method to check if a certain non-terminal (such as <adj>
     * is present in the map.
     *
     * Accepts a String and returns true if said non-terminal is
     * in the map, and therefore a valid grammar. Returns false
     * otherwise.
     *
     * @param       symbol      The string that will be checked
     * @return      boolean     True if present, false if otherwise
     */
    public boolean grammarContains(String symbol){
        if(rules.keySet().toString().contains(symbol)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * Method to generate sentences based on BNF notation and
     * return them as strings.
     *
     * @param       symbol      The BNF symbol to be generated
     * @param       times       The number of sentences to be generated
     * @return      String      The generated sentence
     */
    public String[] generate(String symbol, int times){
        //Array for output
        String[] output = new String[times];

        for(int i = 0; i < times; i++){
            //Clear array for run
            output[i] = "";

            //Grab rules, and store in an array
            lString[] grammar = rules.get(symbol);

            //Generate random number and assign to var
            int rand = randomNumber(grammar.length);

            //Take chosen grammar and split into array
            String[] rules =  grammar[rand].toString().split("\\s");

            //Determine if the rule is terminal or not
            if(grammarContains(rules[0])){
                //System.out.println("grammar has more grammars");
                //Find if there is one or more conditions
                if(rules.length == 1){
                    String[] returnString = generate(rules[0], 1);
                    output[i] += returnString[0];
                    output[i] += " ";
                }else if(rules.length > 1){
                    for(int j = 0; j < rules.length; j++){
                        String[] returnString = generate(rules[j], 1);
                        output[i] += returnString[0];
                        output[i] += " ";
                    }
                }
            }else{
                String[] returnArr = new String[1];
                returnArr[0] = grammar[rand];;
                return returnArr;
            }
            output[i] = output[i].trim();
        }
        return output;
    }

    /**
     * Method to list all valid non-terminals for the current grammar
     *
     * @return      String      A listing of all valid non-terminals
     *                          contained in the current grammar that
     *                          can be used to generate words or
     *                          sentences.
     */
    String getSymbols(){
        return rules.keySet().toString();
    }

    public int randomNumber(int max){
        Random rand = new Random();
        int returnVal = rand.nextInt(max);
        return returnVal;
    }
}

and my test harness is as follows:

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

public class GrammarTest {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner console = new Scanner(System.in);
        System.out.println();

        // open grammar file
        Scanner input = new Scanner(new File("sentence.txt"));

        // read the grammar file and construct the grammar solver
        List<String> grammar = new ArrayList<String>();
        while (input.hasNextLine()) {
            String next = input.nextLine().trim();
            if (next.length() > 0)
                grammar.add(next);
        }
        GrammarSolver solver =
            new GrammarSolver(Collections.unmodifiableList(grammar));
    }

}

Any help or tips will be greatly appreciated;

Thanks!

EDIT: The lines 95, 106, and 116 correlate to

94 //generate random number and assign to var
95     int rand = randomNumber(grammar.length);
...
105//Find if there is one or more conditions
106    if(rules.length == 1){
...
115 for(int j = 0; j < rules.length; j++){
116    String[] returnString = generate(rules[j], 1);
  • 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-20T11:21:32+00:00Added an answer on May 20, 2026 at 11:21 am

    As a first step, I would make sure that

    String[] grammar = rules.get(symbol);

    doesn’t return null. That will eliminate the suspected expressions like “grammar.length” and “grammar[rand].toString()”. Next step would be to meticulously check all other dereferences for null.

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

Sidebar

Related Questions

I have been working on designing a file server that could take the load
I have been working on a project that has 2 interfaces - windows forms
I have been working on a Java project for a class for a while
I have been working on a web services related project for about the last
I have a project that I have been working on for a while, just
I have been working with Struts for some time, but for a project I
On a recent project I have been working on in C#/ASP.NET I have some
I am about to start a web project and have been working almost exclusively
I have recently been working with someone on a project that is very ajax
I've been working on a project in Delphi 7 where I wanted to have

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.