我正在为中缀表示法编写一个解析器。
在 if 语句中我声明了变量 newchild。否则我希望它抛出异常。但是当我超出范围时,编译器不再知道该变量。
我不能在 if 语句之前声明它,因为根据我们所处的情况,该变量被分配了不同的数据类型。
我可以做什么来解决这个问题?
public class ParserForInfixNotation {
public Node parse(List<String> tokenList) {
Stack<String> myStack = new Stack<String>();
int i =1;
while(i <= tokenList.size()){ //wir gehen alle Eintraege in der Liste durch
if(Character.isDigit(tokenList.get(i).charAt(1))){
int value = Integer.parseInt(tokenList.get(i)); //falls der Eintrag eine Zahl ist, wird ein neuer Leaf erstellt
Leaf res = new Leaf(value);
}
else if(tokenList.get(i) == "("){ // falls der Eintrag eine Klammer ist, wird geschaut, ob in der Klammer ein Unary oder Binary OpNode definiert ist
if (tokenList.get(i+1) == "-") {
// Fall Unary
int j = i+1;
//am Liebsten ein rekursiver Aufruf auf parser mit nem TeilString des Oberen Strings, der genau den naechsten Node beschreibt
int anzahlklammern = 0;
boolean end = false;
if((Character.isDigit(tokenList.get(j).charAt(1))) || (tokenList.get(j+1) == ")")){
Leaf newchild = new Leaf(Integer.parseInt(tokenList.get(j)));
}
else if(tokenList.get(j) == "("){
while(!end){
if(tokenList.get(j) == ")" && anzahlklammern == 1){
end = true;
}
else if(tokenList.get(j) == ")" && j > i+3){ //die Klammer muss mindestens 2 Stellen enthalten
anzahlklammern--;
j++;
}
else if(tokenList.get(j) == "("){
anzahlklammern++;
j++;
}
else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
j++;
}
else{
throw new IllegalArgumentException();
}
}
List<String> neu = new ArrayList<>();
for (int l = i+2; l<j;l++){
neu.add(tokenList.get(l));
}
Node newchild = parse(neu);
}
else {
throw new IllegalArgumentException();
}
UnaryOpNode res = new UnaryOpNode('-',newchild);
}
else if((tokenList.get(i+1) == "(") || (Character.isDigit(tokenList.get(i+1).charAt(1)))){ //Fall Binary
if (Character.isDigit(tokenList.get(i+1).charAt(1)) && (tokenList.get(i+2) == "+" || tokenList.get(i+2) == "*")){
Leaf newchildleft = new Leaf(Integer.parseInt(tokenList.get(i+1)));
if(tokenList.get(i+2) == "+"){
Character operator = '+';
}
else if(tokenList.get(i+2) == "*"){
Character operator = '*';
}
int j = i+3;
if(Character.isDigit(tokenList.get(j).charAt(1))){
Leaf newchildright = new Leaf(Integer.parseInt(tokenList.get(j)));
}
else if(tokenList.get(j) == "("){
boolean end = false;
int anzahlklammern =0 ;
while(!end){
if(tokenList.get(j) == ")" && anzahlklammern == 1){
end = true;
}
else if(tokenList.get(j) == ")" && j > i+5){ //die Klammer muss mindestens 2 Stellen enthalten
anzahlklammern--;
j++;
}
else if(tokenList.get(j) == "("){
anzahlklammern++;
j++;
}
else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
j++;
}
else{
throw new IllegalArgumentException();
}
}
List<String> neu = new ArrayList<>();
for (int l = i+4; l<j;l++){
neu.add(tokenList.get(l));
}
Node newrightchild = parse(neu);
}
else{
throw new IllegalArgumentException();
}
}
else if(tokenList.get(i+1) == "("){
int j= i+1;
boolean end = false;
int anzahlklammern =0 ;
while(!end){
if(tokenList.get(j) == ")" && anzahlklammern == 1){
end = true;
}
else if(tokenList.get(j) == ")" && j > i+3){ //die Klammer muss mindestens 2 Stellen enthalten
anzahlklammern--;
j++;
}
else if(tokenList.get(j) == "("){
anzahlklammern++;
j++;
}
else if ((Character.isDigit(tokenList.get(j).charAt(1))) || tokenList.get(j) == "+" || tokenList.get(j) == "*" || tokenList.get(j) == "-"){
j++;
}
else{
throw new IllegalArgumentException();
}
}
List<String> neu = new ArrayList<>();
for (int l = i+2; l<j;l++){
neu.add(tokenList.get(l));
}
Node newleftchild = parse(neu);
if(tokenList.get(j+1) == "+"){
Character operator = '+';
}
else if(tokenList.get(j+1) == "*"){
Character operator = '*';
}
else{
throw new IllegalArgumentException();
}
if(tokenList.get(j+2)== "("){
int k= j+3;
end = false;
anzahlklammern =0 ;
while(!end){
if(tokenList.get(k) == ")" && anzahlklammern == 1){
end = true;
}
else if(tokenList.get(k) == ")" && k > j+5){ //die Klammer muss mindestens 2 Stellen enthalten
anzahlklammern--;
k++;
}
else if(tokenList.get(k) == "("){
anzahlklammern++;
k++;
}
else if ((Character.isDigit(tokenList.get(k).charAt(1))) || tokenList.get(k) == "+" || tokenList.get(k) == "*" || tokenList.get(k) == "-"){
k++;
}
else{
throw new IllegalArgumentException();
}
}
List<String> neu2 = new ArrayList<>();
for (int l = j+4; l<k;l++){
neu.add(tokenList.get(l));
}
Node newrightchild = parse(neu2);
}
else if(Character.isDigit(tokenList.get(j+2).charAt(1))){
Leaf newrightchild = new Leaf(Integer.parseInt(tokenList.get(j+2)));
}
else{
throw new IllegalArgumentException();
}
}
BinaryOpNode res = new BinaryOpNode(operator, newleftchild, newrightchild);
}
else{
throw new IllegalArgumentException();
}
}
else{
throw new IllegalArgumentException();
}
}
return res;
}
这是关于scope。变量的作用域是它所在的块declared,以及其中的块。该块是否是某个块的块并不重要if
语句、其他语句或只是为了定义范围而放在那里的块。
你正在经历这样的情况:
{
Leaf leaf = new Leaf();
}
doSomethingWith(leaf); // compiler error - there is no `leaf` in this scope.
您可以使用以下方法修复它:
Leaf leaf;
{
leaf = new Leaf();
}
doSomethingWith(leaf);
如果有可能分配给leaf
不会发生——例如,如果它在if
块,然后你会得到一个编译器错误说variable leaf may not have been initialized
。您可以通过首先初始化一些后备值来解决此问题。它通常为空:
Leaf leaf = null;
if(...) {
leaf = new Leaf();
}
doSomethingWith(leaf);
但现在你的代码必须应对以下可能性:leaf == null
- 这会导致代码要么过于复杂,要么脆弱。找到避免分配 null 的方法,或者如果不能的话,隔离易受攻击的代码块,以便该范围之外的任何内容都不需要处理 null 变量的可能性。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)