Advertisement
csaki

Fordítóprogramok táblázatos elemző

Nov 10th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace tablazatos_elemzo
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         Stack<string> stack = new Stack<string>();
  16.         bool ures_e = false;
  17.         static string szalag = "i + i * i";
  18.         string[] splittedSzalag = szalag.Split(' ');
  19.  
  20.         string[][] matrix = new string[][]
  21.         {
  22.             new string[] {"error", "+", "*", "(", ")", "i", "#"},
  23.             new string[] {"E", "error", "error", "(TE', 1)", "error", "(TE', 1)", "error"},
  24.             new string[] {"E'", "(+TE', 2)", "error", "error", "(ε, 3)", "error", "(ε, 3)"},
  25.             new string[] {"T", "error", "error", "(FT',4)", "error", "(FT',4)", "error"},
  26.             new string[] {"T'", "(ε, 6)", "(*FT', 5)", "error", "(ε, 6)", "error", "(ε, 6)"},
  27.             new string[] {"F", "error", "error", "((E), 7)", "error", "(i, 8)", "error"},
  28.             new string[] {"+", "pop", "error", "error", "error", "error", "error"},
  29.             new string[] {"*", "error", "pop", "error", "error", "error", "error"},
  30.             new string[] {"(", "error", "error", "pop", "error", "error", "error"},
  31.             new string[] {")", "error", "error", "error", "pop", "error", "error"},
  32.             new string[] {"i", "error", "error", "error", "error", "pop", "error"},
  33.             new string[] {"#", "error", "error", "error", "error", "error", "elfogad"},
  34.         };
  35.  
  36.         public Form1()
  37.         {
  38.             InitializeComponent();
  39.         }
  40.  
  41.         private void Form1_Load(object sender, EventArgs e)
  42.         {
  43.             stack.Push("E");
  44.  
  45.             for (int i = 0; i < matrix[0].Length; i++)
  46.             {
  47.                 dataGridView1.Columns.Add(i.ToString(), matrix[0][i]);
  48.             }
  49.  
  50.             for (
  51.                 int i = 1; i < matrix.GetLength(0); i++)
  52.             {
  53.                 dataGridView1.Rows.Add(matrix[i]);
  54.             }
  55.         }
  56.  
  57.         private void button1_Click(object sender, EventArgs e)
  58.         {
  59.             label1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString(); //aktuális cella sorazonosítóját adja meg
  60.         }
  61.  
  62.  
  63.         private void button2_Click(object sender, EventArgs e)
  64.         {
  65.             int index = 0;
  66.             while (index < szalag.Length)
  67.             {
  68.                 string aktElem = stack.Peek();
  69.                 int oszlopIndex = 1;
  70.                 int sorIndex = 0;
  71.                 string szabalyokSorszama = "";
  72.  
  73.                 while (oszlopIndex < dataGridView1.Columns.Count - 1 && dataGridView1.Columns[oszlopIndex].HeaderText != splittedSzalag[index])
  74.                 {
  75.                     oszlopIndex++;
  76.                 }
  77.                 while (sorIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[sorIndex].Cells[0].Value.ToString() != aktElem)
  78.                 {
  79.                     sorIndex++;
  80.                 }
  81.                 TextboxFill(String.Format(">>> Szalag index: {0}\r\n>>> Oszlop index: {1}\r\n>>> Sor index: {2}", index, oszlopIndex, sorIndex));
  82.                 string adat = dataGridView1.Rows[sorIndex].Cells[oszlopIndex].Value.ToString();
  83.                 string tisztaAdat = "error";
  84.                 bool szabalyVolt = false;
  85.  
  86.                 switch (adat)
  87.                 {
  88.                     case "error":
  89.                         TextboxFill(adat);
  90.                         return;
  91.                     case "elfogad":
  92.                         TextboxFill(adat);
  93.                         break;
  94.                     case "pop":
  95.                         stack.Pop();
  96.                         index++;
  97.                         TextboxFill(adat);
  98.                         break;
  99.                     default:
  100.                         tisztaAdat = adat.Substring(adat.IndexOf("(") + 1, adat.IndexOf(",")-1);
  101.                         szabalyokSorszama += adat.Substring(adat.IndexOf(",") + 1, 1);
  102.                         szabalyVolt = true;
  103.                         TextboxFill(adat);
  104.                         break;
  105.                 }
  106.  
  107.                 if (szabalyVolt)
  108.                 {
  109.                     stack.Pop();
  110.                     if (tisztaAdat != "ε")
  111.                     {
  112.                         for (int i = tisztaAdat.Length - 1; i >= 0; i--)
  113.                         {
  114.                             if (tisztaAdat[i].ToString() == "'")
  115.                             {
  116.                                 stack.Push(tisztaAdat[i-1].ToString() + tisztaAdat[i].ToString());
  117.                             }
  118.                             else stack.Push(tisztaAdat[i].ToString());
  119.                         }
  120.                     }
  121.                 }
  122.  
  123.             }
  124.         }
  125.  
  126.         private void kiszed()
  127.         {
  128.             // ide kell majd beletenni a feljebbi részt
  129.         }
  130.  
  131.         void TextboxFill(string msg)
  132.         {
  133.             if (InvokeRequired)
  134.             {
  135.                 Invoke(new Action(() => textBox1.Text += msg + "\r\n"));
  136.             }
  137.             else textBox1.Text += msg + "\r\n";
  138.         }
  139.  
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement