无法访问的代码相关错误

时间:2018-05-09 06:11:47

标签: java unreachable-code

我来自哪里:

我有一个BNF语法解算器的代码。

问题:

似乎最后一个错误是无法访问数组字符串的声明。

我尝试了什么:

我当前的方法中没有返回语句错误来自,所以我对错误的来源感到困惑。

import java.util.*;

public class GrammarSolver{
     private Map<String, List<String>> theMap;
     private Random random;

  public GrammarSolver(List<String> rules)
  {
    if (rules.size() == 0 || rules == null)
    {
       throw new IllegalArgumentException();
    }

    theMap = new TreeMap<String, List<String>>();
    random = new Random();

    for (String i : rules)
    {
       String[] info = i.split("::=");

       if (theMap.containsKey(info[0]));
       {
          throw new IllegalArgumentException();
       }

       String[] terminals = info[1].split("[|]");

       List<String> terminalList = new ArrayList<String>();
       for(String s : terminals)
       {
          String temp = s.trim();
          terminalList.add(temp);
       }

       theMap.put(info[0], terminalList);
    }
  }

   public boolean contains(String symbol)
   {
      if (symbol.isEmpty() || symbol == null)
      {
         throw new IllegalArgumentException();
      }
      return theMap.containsKey(symbol);
   }

   public Set<String> getSymbols()
   {
      return theMap.keySet();
   }

   public String generate(String symbol)
   {
      String finalSentence = genRecursion(symbol);
      return finalSentence.substring(0, finalSentence.length() - 1);
   }

   private String genRecursion(String symbol)
   {
      if (symbol == null || symbol.isEmpty())
     {
       throw new IllegalArgumentException();
     } 

    if (!theMap.containsKey(symbol))
    {
       return symbol;
    }

    List<String> terms = theMap.get(symbol);

    String finalSentence = "";

    String chooseRule = terms.get(random.nextInt(terms.size()));
    String[] arrayRules = chooseRule.split("[ \t]+");

    if (arrayRules.length > 1)
    {
       for (String i : arrayRules)
       {
          if (theMap.containsKey(i))
          {
             finalSentence = finalSentence + genRecursion(i);
          }
          else
          {
             finalSentence = finalSentence + i + " ";
          }
       }
    }
    else if (theMap.containsKey(chooseRule))
    {
       finalSentence = finalSentence + genRecursion(chooseRule);
    }
    else
    {
       finalSentence = finalSentence + genRecursion(chooseRule) + " ";
    }

    return finalSentence;
  }
}

1 个答案:

答案 0 :(得分:2)

因为这个(可能是错位的)分号:

public static Annotation[] getAnnotations(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws ...

if (theMap.containsKey(info[0])) ; { throw new IllegalArgumentException(); } 之后,throw语句只能运行,因为它不受条件限制。