parsing - Find the first and follow set of a given grammar in java -


i have been given problem complete, , algorithms find first , follow, problem cant quite find data structure implement find these sets.

import java.util.stack;  public class firstfollowset {  private final string[] term_tokens = { "begin", "end", ";", "if", "then", "else", "fi", "i", "=", "+", "-", "*",         "/", "(", ")", "const" }; private final static string[] non_term_tokens = { "start", "prog", "block", "body", "s", "e", "t", "f" }; private static rulestack rules; private stack<string> firstset; private stack<string> followset;  private boolean is_terminal(string str) {     boolean test = false;     (int = 0; < term_tokens.length; i++) {         if (str.equals(term_tokens[i]))             test = true;     }      return test; }  private boolean is_non_term(string str){     for(int = 0; < non_term_tokens.length; i++)     {         if(str.equals(non_term_tokens[i]))         {             return true;         }      }     return false; }  private class rule{     string def, token;      public rule()     {         def = "";         token = "";     }      public rule(string d, string t)     {         def = d;         token = t;     }      public string getdef() {         return def;     }      public string gettoken() {         return token;     }      public string tostring()     {         string str = "";         str+= token + " " + def + '\n';         return str;     } }  public class rulestack{     stack<rule> rules;      public rulestack(string grammar)     {         if(grammar.equals("g1"));         {             rules = new stack();             rule rule = new rule("prog", "start");             rules.push(rule);             rule = new rule("block #", "prog");             rules.push(rule);             rule = new rule("begin body end", "block");             rules.push(rule);             rule = new rule("begin s end", "body");             rules.push(rule);             rule = new rule("body ; s", "body");             rules.push(rule);             rule = new rule("s", "body");             rules.push(rule);             rule = new rule("if e s else s fi", "s");             rules.push(rule);             rule = new rule("if e else s fi", "s");             rules.push(rule);             rule = new rule("i = e", "s");             rules.push(rule);             rule = new rule("block", "s");             rules.push(rule);             rule = new rule("e + t", "e");             rules.push(rule);             rule = new rule("e * t", "e");             rules.push(rule);             rule = new rule("t", "e");             rules.push(rule);             rule = new rule("t * f", "t");             rules.push(rule);             rule = new rule("t / f", "t");             rules.push(rule);             rule = new rule("f", "t");             rules.push(rule);             rule = new rule("const", "f");             rules.push(rule);             rule = new rule("( e )", "f");             rules.push(rule);         }     }  }  public firstfollowset() {     rules = new rulestack("g1");     firstset = new stack();     followset = new stack(); }  public string findfirstset(string str, stack<string> used) {        if(used.contains(str))     {         return null;     }     string firsttoken = "";     string win = "";     if(str.indexof(" ") != -1)         firsttoken = str.substring(0, str.indexof(" "));     else         firsttoken = str;     if(is_terminal(firsttoken))     {         if(!(firstset.contains(firsttoken)))             win = firsttoken;             if(win.equals("") != true)                 firstset.push(win);     }      else if(is_non_term(firsttoken) && !(used.contains(firsttoken)))     {         used.push(firsttoken);         if(firsttoken.equals("lambda"))         {             if(!(firstset.contains(firsttoken)))             win = firsttoken;         }         else         {             rulestack rules = new rulestack("g1");             while(rules.rules.isempty() != true)             {                 rule winner = rules.rules.pop();                 if(winner.token.equals(firsttoken))                 {                     string test = findfirstset(winner.def, used);                     if(!(test.equals("lambda")))                     {                         if(!(firstset.contains(test)))                         win = test;                     }                 }             }         }     }      return win; }  public string findfollowset(string str) {     if(str.equals("s"))     {         followset.push("$");     }      for(int = 0; < non_term_tokens.length; i++)     {         if(str.contains(non_term_tokens[i]))         {             int index = str.indexof(non_term_tokens[i]);             stack<string> used = new stack();             firstfollowset test = new firstfollowset();             if(index > 0 && index < str.length()-1)             {                 test.findfirstset(str, used);                 while(test.firstset.isempty() != true)                 {                     string token = firstset.pop();                     if(!(token.equals("lambda")))                         test.followset.push(token);                 }             }              else if(index > 0 && index == str.length()-1)             {              }         }     } }  public static void main(string[] args) {     firstfollowset test = new firstfollowset();     stack<string> used = new stack();     test.findfirstset("s", used);     while(test.firstset.isempty() != true)     {         string str = test.firstset.pop();         system.out.println(str);     } } 

}

this code have far, , find first set works fine, findfollowset method i'm not quite sure how implement. idea can seem come making stack each non-terminal symbol, apply algorithm, , add each terminal symbol returned set belongs to. method feel more work necessary.

if has ever solved problem, or has seen way solve problem know sort of data structure used , how algorithm implemented said structure.

thank taking time read this, , appreciate feedback given.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -