السلام عليكم
عندي مشكلة في هذا الكود
وهو إنه يحول من Infix إلى Postfix
طبعا هذا مثال على Input
http://www.m5zn.com/img/?img=1c7802ea56d005d.png
المشكلة عندي هي عندما أضع أقواس
مثلا
http://www.m5zn.com/img/?img=da5b98c6fdd065c.png
فإن الناتج يكون خاطئ
يستدعي error Method
وهذا كودي
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SyntaxDirectedTranslantion {
public static void main(String[] args) throws IOException {
SyntaxDirectedTranslantion objectSysntax = new SyntaxDirectedTranslantion();
objectSysntax.mainMethod(); //Calls the function expr()
}
int lookahead;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public void mainMethod() throws IOException //main function
{
lookahead = br.read();
expr(); //Calls the function expr()
if ((char) lookahead == '$') //Checks to see if the entire input has been read
{
System.out.print("\n Postfix form is as above:");
//To wait for the user to read the message
} else {
error();
}
}
public void expr() throws IOException {
term();
exprprime();
}
public void exprprime() throws IOException {
if ((char) lookahead == '+') {
match('+');
term();
System.out.print('+');
exprprime();
} else if ((char) lookahead == '-') {
match('-');
term();
System.out.print('-');
exprprime();
} else; //Null statement for ^-production in the grammar
}
private void term() throws IOException {
factor();
termprime();
}
private void factor() throws IOException {
if (lookahead >= 48 && lookahead <= 57) {
System.out.print((char) lookahead);
match(lookahead);
// }
// else {
if ((char) lookahead == '(') {
match('(');
expr();
match(')');
}
} else {
error();
}
}
private void termprime() throws IOException {
if ((char) lookahead == '*') {
match('*');
term();
System.out.print('*');
termprime();
} else if ((char) lookahead == '/') {
match('/');
term();
System.out.print('/');
termprime();
}
}
private void match(int t) throws IOException {
if (lookahead == t) {
lookahead = (char) br.read();
} else {
System.out.println("ERROR");
error();
}
}
private void error() {
System.out.print("\nSyntax Error....");
}
}