E→TA
A→+TA | є
T→FB
B→*FB | є
F→(E) | id
Parse the string:
1) id + id * id
2) (id + id) * (id + id)
3) (id + id * id)
Program:
import java.util.Scanner;
public class Prac4 {
static String given;
static char l;
static int i=0;
static public void main(String[] r){
Scanner in = new Scanner(System.in);
String s = in.nextLine();
given = s;
l=given.charAt(i);
i++;
E();
if(l=='$'){
System.out.println("Successful");
}else{
System.out.println("Error in given string");
}
}
static void E(){
T();
A();
}
static void T(){
F();
B();
}
static void A(){
if(l=='+'){
match('+');
T();
A();
}
else{
}
}
static void F(){
if(l=='('){
match('(');
E();
match(')');
}else if(l=='i'){
match('i');
match('d');
}else{
System.out.println("Error1");
System.exit(0);
}
}
static void B(){
if(l=='*'){
match('*');
F();
B();
}
else{
}
}
static void match(char t){
if(l==t){
l=given.charAt(i);
i++;
if(i>given.length()){
System.out.println("Error");
}
}
else{
System.out.println("Error");
}
}
}
No comments:
Post a Comment