Beispiel #1
0
        public List<List<String>> process(List<Postfix> postfix)
        {
            List<Postfix> stack = new List<Postfix>();
            List<Quadruple> quadruples = new List<Quadruple>();
            List<String> quadText = new List<String>();
            List<String> tacText = new List<string>();

            int countTemp = 0;
            for (int i = 0; i < postfix.Count; i++)
            {
                Operator op;
                try
                {
                    op = this.getOperator(postfix[i]);
                    if (op == null)
                    {
                        // not operator
                        stack.Add(postfix[i]);
                    }
                    else
                    {
                        // is operator
                        Quadruple tmp;
                        int lastIndex = stack.Count - 1;

                        if (op.Type == 2)
                        {
                            // a op b
                            String opr2 = stack[lastIndex].item;
                            String opr1 = stack[lastIndex - 1].item;
                            stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                            countTemp++;
                            String res = "temp" + countTemp;
                            tmp = new Quadruple(quadruples.Count + 1, op.FName, opr1, opr2, res);
                            stack.Add(new Postfix(res));
                            quadruples.Add(tmp);

                            String tac = (tmp.Result + " := " + tmp.Operand1 + " " + op.Name + " " + tmp.Operand2);

                            tacText.Add(tmp.No + ". " + tac + "\n");
                            quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");

                        }
                        else
                        {
                            if (op.Name == "=")
                            {
                                // a = b
                                String opr = stack[lastIndex].item;
                                String res = stack[lastIndex - 1].item;
                                stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                                tmp = new Quadruple(quadruples.Count + 1, op.FName, opr, "", res);
                                quadruples.Add(tmp);

                                String tac = (tmp.Result + " := " + tmp.Operand1);

                                tacText.Add(tmp.No + ". " + tac + "\n");
                                quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");

                            }
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            List<List<String>> output = new List<List<String>>();
            output.Add(tacText);
            output.Add(quadText);
            return output;
        }
Beispiel #2
0
        private bool processOneStep()
        {
            if (!isProcessing || processedItem >= postfix.Count)
            {
                btnNextProcess.Enabled = false;
                return false;
            }

            Operator op;
            try
            {
                op = getOperator(postfix[processedItem]);

                if (op == null)
                {
                    // not operator
                    stack.Add(postfix[processedItem].item);
                }
                else
                {
                    // is operator
                    Quadruple tmp;
                    int lastIndex = stack.Count - 1;

                    if (op.Type == 2)
                    {
                        // a op b
                        String opr2 = stack[lastIndex];
                        String opr1 = stack[lastIndex - 1];
                        stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                        countTemp++;
                        String res = "temp" + countTemp;
                        tmp = new Quadruple(quadruples.Count + 1, op.FName, opr1, opr2, res);
                        stack.Add(res);
                        quadruples.Add(tmp);

                        String tac = (tmp.Result + " := " + tmp.Operand1 + " " + op.Name + " " + tmp.Operand2);
                        lblStackOutput.Text = tac;

                        if (isFileProcessing)
                        {
                            tacText.Add(tmp.No + ". " + tac + "\n");
                            quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");
                        }
                    }
                    else
                    {
                        if (op.Name == "=")
                        {
                            // a = b
                            String opr = stack[lastIndex];
                            String res = stack[lastIndex - 1];
                            stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                            tmp = new Quadruple(quadruples.Count + 1, op.FName, opr, "", res);
                            quadruples.Add(tmp);

                            String tac = (tmp.Result + " := " + tmp.Operand1);
                            lblStackOutput.Text = tac;

                            if (isFileProcessing)
                            {
                                tacText.Add(tmp.No + ". " + tac + "\n");
                                quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error occured. Invalid input might be the cause of this.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            
            processedItem++;

            txtProcessedIndex.Text = Convert.ToString(processedItem);
            if (processedItem == postfix.Count)
            {
                if (stack.Count > 0)
                {
                    MessageBox.Show("Error occured. Invalid input might be the cause of this.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                stack.Clear();
                processedItem = 0;

                isProcessing = false;
                btnNextProcess.Enabled = false;
                btnClearInput.Enabled = true;
                btnDefaultInput.Enabled = true;
                btnNextInput.Enabled = true;
                proses.Enabled = false;
                btnProcessToFile.Enabled = false;

                lblProcessed.Visible = false;
                //txtProcessedIndex.Text = "";
                txtProcessedIndex.Visible = false;
            }
            showInput();
            showStack();
            showQuadruple();

            return true;
        }
Beispiel #3
0
        private bool processOneStep()
        {
            if (!isProcessing || processedItem >= postfix.Count)
            {
                btnNextProcess.Enabled = false;
                return(false);
            }

            Operator op;

            try
            {
                op = getOperator(postfix[processedItem]);

                if (op == null)
                {
                    // not operator
                    stack.Add(postfix[processedItem].item);
                }
                else
                {
                    // is operator
                    Quadruple tmp;
                    int       lastIndex = stack.Count - 1;

                    if (op.Type == 2)
                    {
                        // a op b
                        String opr2 = stack[lastIndex];
                        String opr1 = stack[lastIndex - 1];
                        stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                        countTemp++;
                        String res = "temp" + countTemp;
                        tmp = new Quadruple(quadruples.Count + 1, op.FName, opr1, opr2, res);
                        stack.Add(res);
                        quadruples.Add(tmp);

                        String tac = (tmp.Result + " := " + tmp.Operand1 + " " + op.Name + " " + tmp.Operand2);
                        lblStackOutput.Text = tac;

                        if (isFileProcessing)
                        {
                            tacText.Add(tmp.No + ". " + tac + "\n");
                            quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");
                        }
                    }
                    else
                    {
                        if (op.Name == "=")
                        {
                            // a = b
                            String opr = stack[lastIndex];
                            String res = stack[lastIndex - 1];
                            stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                            tmp = new Quadruple(quadruples.Count + 1, op.FName, opr, "", res);
                            quadruples.Add(tmp);

                            String tac = (tmp.Result + " := " + tmp.Operand1);
                            lblStackOutput.Text = tac;

                            if (isFileProcessing)
                            {
                                tacText.Add(tmp.No + ". " + tac + "\n");
                                quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error occured. Invalid input might be the cause of this.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            processedItem++;

            txtProcessedIndex.Text = Convert.ToString(processedItem);
            if (processedItem == postfix.Count)
            {
                if (stack.Count > 0)
                {
                    MessageBox.Show("Error occured. Invalid input might be the cause of this.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                stack.Clear();
                processedItem = 0;

                isProcessing             = false;
                btnNextProcess.Enabled   = false;
                btnClearInput.Enabled    = true;
                btnDefaultInput.Enabled  = true;
                btnNextInput.Enabled     = true;
                proses.Enabled           = false;
                btnProcessToFile.Enabled = false;

                lblProcessed.Visible = false;
                //txtProcessedIndex.Text = "";
                txtProcessedIndex.Visible = false;
            }
            showInput();
            showStack();
            showQuadruple();

            return(true);
        }
Beispiel #4
0
        public List <List <String> > process(List <Postfix> postfix)
        {
            List <Postfix>   stack      = new List <Postfix>();
            List <Quadruple> quadruples = new List <Quadruple>();
            List <String>    quadText   = new List <String>();
            List <String>    tacText    = new List <string>();

            int countTemp = 0;

            for (int i = 0; i < postfix.Count; i++)
            {
                Operator op;
                try
                {
                    op = this.getOperator(postfix[i]);
                    if (op == null)
                    {
                        // not operator
                        stack.Add(postfix[i]);
                    }
                    else
                    {
                        // is operator
                        Quadruple tmp;
                        int       lastIndex = stack.Count - 1;

                        if (op.Type == 2)
                        {
                            // a op b
                            String opr2 = stack[lastIndex].item;
                            String opr1 = stack[lastIndex - 1].item;
                            stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                            countTemp++;
                            String res = "temp" + countTemp;
                            tmp = new Quadruple(quadruples.Count + 1, op.FName, opr1, opr2, res);
                            stack.Add(new Postfix(res));
                            quadruples.Add(tmp);

                            String tac = (tmp.Result + " := " + tmp.Operand1 + " " + op.Name + " " + tmp.Operand2);

                            tacText.Add(tmp.No + ". " + tac + "\n");
                            quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");
                        }
                        else
                        {
                            if (op.Name == "=")
                            {
                                // a = b
                                String opr = stack[lastIndex].item;
                                String res = stack[lastIndex - 1].item;
                                stack.RemoveAt(lastIndex); stack.RemoveAt(lastIndex - 1);
                                tmp = new Quadruple(quadruples.Count + 1, op.FName, opr, "", res);
                                quadruples.Add(tmp);

                                String tac = (tmp.Result + " := " + tmp.Operand1);

                                tacText.Add(tmp.No + ". " + tac + "\n");
                                quadText.Add(tmp.No + ". " + tmp.Operator + "," + tmp.Operand1 + "," + tmp.Operand2 + "," + tmp.Result + "\n");
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            List <List <String> > output = new List <List <String> >();

            output.Add(tacText);
            output.Add(quadText);
            return(output);
        }