Java implements left recursion to eliminate Grammar

Source: Internet
Author: User

Java implements left recursion to eliminate Grammar

package com.siwanghu.syntaxanalyzer.bean;public class Grammar {    private String left;    private String right;    private int id;    private static int ID = 0;    public Grammar() {        super();        id = ID++;    }    public Grammar(String left, String right) {        super();        this.left = left;        this.right = right;        id = ID++;    }    public String getLeft() {        return left;    }    public void setLeft(String left) {        this.left = left.replace(" ", "");    }    public String getRight() {        return right;    }    public void setRight(String right) {        this.right = right.replace(" ", "");    }    public int getId() {        return id;    }              @Override    public String toString() {        return "Grammar [left=" + left + ", right=" + right + "]";    }}

Package com. siwanghu. syntaxanalyzer. algorithm; import java. util. arrayList; import java. util. using list; import java. util. list; import java. util. listIterator; import com. siwanghu. syntaxanalyzer. bean. grammar; public class Production {private List productions = new Functions List (); // generate private List symbols = new ArrayList (); // initial generation non-terminator private List nonTerminatingSymbol = new ArrayList (); // LL (1) grammar non-terminator priv Ate List terminatingSymbol = new ArrayList (); // LL (1) grammar Terminator public Production (List productions) {super (); this. produbol = productions; symbolProductions ();} public List getProductions () {return productions;} public List getSymbols () {return symbols;} public List getNonTerminatingSymbol () {return nonTerminatingSymbol ;} public List getTerminatingSymbol () {return terminatingSymbol;} public Void removeLeftRecursion () {for (int I = 0; I <symbols. size (); I ++) {for (int j = 0; j <I; j ++) {iterativeReplacement (symbols. get (I), symbols. get (j);} removeLeftRecursion (symbols. get (I);} no_or_is_terminatingSymbol ();} private void symbolProductions () {if (productions. size ()! = 0) {for (int I = 0; I <productions. size (); I ++) {if (! (ArrayList) symbols ). contains (productions. get (I ). getLeft (). charAt (0) {symbols. add (productions. get (I ). getLeft (). charAt (0) ;}}} private void no_or_is_terminatingSymbol () {for (int I = 0; I <productions. size (); I ++) {if (! (ArrayList) nonTerminatingSymbol ). contains (productions. get (I ). getLeft () {nonTerminatingSymbol. add (productions. get (I ). getLeft ();} if (productions. get (I ). getLeft () = productions. get (I ). getLeft (). charAt (0) + "'") {nonTerminatingSymbol. add (productions. get (I ). getLeft () ;}}for (int I = 0; I <productions. size (); I ++) {String temp = productions. get (I ). getRight (); temp = temp. replace ("epsilon "," # "); For (int j = 0; j <nonTerminatingSymbol. size (); j ++) {temp = temp. replaceAll (nonTerminatingSymbol. get (j), "");} temp = temp. replaceAll ("\\\\ |", ""); temp = temp. replaceAll ("'", ""); char [] chars = temp. toCharArray (); for (int k = 0; k <chars. length; k ++) {if (chars [k] = '#') {if (! TerminatingSymbol. contains ("epsilon") {terminatingSymbol. add ("epsilon") ;}} else {if (! TerminatingSymbol. contains (String. valueOf (chars [k]) {terminatingSymbol. add (String. valueOf (chars [k]) ;}}} private void iterativeReplacement (Character left, Character right) {ListIterator listIterator = productions. listIterator (); while (listIterator. hasNext () {String inRight = ""; Grammar grammar = listIterator. next (); if (grammar. getLeft (). equals (left. toString () {boolean isReplaceme Nt = false; String [] rights = grammar. getRight (). split ("\\\\|"); for (int I = 0; I <rights. length; I ++) {if (rights [I]. startsWith (right. toString () {isReplacement = true ;}} if (isReplacement) {ListIterator _ listIterator = productions. listIterator (); while (_ listIterator. hasNext () {Grammar _ grammar = _ listIterator. next (); if (_ grammar. getLeft (). equals (right. toString () {String [] _ rights = _ Grammar. getRight (). split ("\\\\|"); for (int I = 0; I <rights. length; I ++) {boolean isCheck = false; if (rights [I]. startsWith (right. toString () {isCheck = true; for (int j = 0; j <_ rights. length; j ++) {String temp = rights [I]; inRight + = (temp. replaceFirst (right. toString (), _ rights [j]) + "|") ;}} if (! IsCheck) {inRight + = (rights [I] + "|") ;}}} if (inRight. length ()! = 0) {listIterator. remove (); listIterator. add (new Grammar (left. toString (), inRight. substring (0, inRight. length ()-1); }}}} private void removeLeftRecursion (Character left) {ListIterator listIterator = productions. listIterator (); while (listIterator. hasNext () {Grammar grammar = listIterator. next (); if (grammar. getLeft (). equals (left. toString () {String [] rights = grammar. getRight (). spl It ("\\\\|"); boolean isLeftRecursion = false; for (int I = 0; I <rights. length; I ++) {if (rights [I]. startsWith (left. toString () {isLeftRecursion = true ;}} if (isLeftRecursion) {listIterator. remove (); String oneRight = "", twoRight = ""; for (int I = 0; I <rights. length; I ++) {if (! Rights [I]. startsWith (left. toString () {oneRight + = (rights [I]. concat (left. toString () + "'") + "|");} else {twoRight + = (rights [I]. replaceFirst (left. toString (),""). concat (left. toString () + "'") + "|") ;}} listIterator. add (new Grammar (left. toString (), oneRight. substring (0, oneRight. length ()-1); listIterator. add (new Grammar (left. toString () + "'", twoRight. concat ("epsilon"); }}}@ Override public String toString () {String temp = "non-Terminator:"; for (int I = 0; I <nonTerminatingSymbol. size (); I ++) {temp + = nonTerminatingSymbol. get (I) + "";} temp + = "Total:" + nonTerminatingSymbol. size (); temp + = "\ n Terminator:"; for (int I = 0; I <terminatingSymbol. size (); I ++) {temp + = terminatingSymbol. get (I) + "";} temp + = "Total:" + terminatingSymbol. size (); temp + = "\ n remove the left recursive Syntax: \ n"; for (int I = 0; I <productions. size (); I ++) {temp + = (productions. get (I) + "\ n") ;}return temp ;}}

 

package com.siwanghu.syntaxanalyzer.test;import java.util.LinkedList;import java.util.List;import com.siwanghu.syntaxanalyzer.algorithm.Production;import com.siwanghu.syntaxanalyzer.bean.Grammar;public class Test {    public static void main(String[] args) {        System.out.println("The Productions of G");        Grammar g1 = new Grammar("S", "Qc|c");        Grammar g2 = new Grammar("Q", "Rb|b");        Grammar g3 = new Grammar("R", "Sa|a");        List g_productions = new LinkedList();        g_productions.add(g3);        g_productions.add(g2);        g_productions.add(g1);        Production g_production = new Production(g_productions);        g_production.removeLeftRecursion();        System.out.print(g_production);        System.out.println("end G\\n");        System.out.println("The Productions of H");        Grammar h1 = new Grammar("E", "E+T|T");        Grammar h2 = new Grammar("T", "T*F|F");        Grammar h3 = new Grammar("F", "(E)|i");        List h_productions = new LinkedList();        h_productions.add(h1);        h_productions.add(h2);        h_productions.add(h3);        Production h_production = new Production(h_productions);        h_production.removeLeftRecursion();        System.out.print(h_production);        System.out.println("end H");    }

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.