Write a Program to Implement Recursive Decent Parser. - java program
Aim:
Write a Program to
Implement Recursive Decent Parser for the given grammar:
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:
package dharmik_cd4;
import java.util.Scanner;
/**
*
* @author dharmik
*/
public class Dharmik_CD4 {
static String
given;
static char l;
static int i=0;
public static void
main(String[] args) {
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");
}
}
}
Output:
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment