// Concat.java 

/** A regular expression consisting of the concatenation of two
 *  other regular expressions.
 *  @author <a href="mailto:bergmann@rowan.edu"> Seth Bergmann </a>
 *  @author <a href="mailto:gspyo@jersey.net"> Greg Safko </a>
 */
public class Concat extends Expr
{  

   /** Constructor for a concatenation.
    *  @param l The left operand of the concatenation.
    *  @param r The right operand of the concatenation.
    */
   public Concat (Expr l, Expr r)
   {	
   		super (l.reduce(),r.reduce()); 
   }

 
    
  /** Return the concatenation reduce based on the rules of 
   * regular expressions
   */
   public Expr reduce()
   {
   	// if a star is concatenated with a similar star, return the star
   	if(this.getLeft() instanceof Star)
   		if (this.getRight() instanceof Star)
   			if (this.getRight().reduce().equals(this.getLeft().reduce()))
   				return this.getLeft().reduce();
    // If an expression is concatenated with a Null, return Null
		if(this.getLeft() instanceof Empty)
			return this.getLeft().reduce();
		if(this.getRight() instanceof Empty)
			return this.getRight().reduce();
	// If an expression is concatenated with an epsilon, return the expression
		if(this.getLeft() instanceof Epsilon)
			return this.getRight().reduce();
		if(this.getRight() instanceof Epsilon)
			return this.getLeft().reduce();
			
		if(this.getLeft() instanceof Star)
			if(this.getRight() instanceof Star)
				if(this.getLeft().equals(this.getRight()))
					return this.getLeft().reduce();
 
   	return this;		
   }
   
   
   /** Return the concatenation in the form of a string which
    *  people can understand.
    */
   public String toString ()
   // Insert the parentheses, and the concat operator "."
   {  	
 		return "(" + this.getLeft().reduce().toString() + "." + this.getRight().reduce().toString() + ")";
   }

}

