compiler programming in c# report

19

Upload: ahmed-alkibsi

Post on 30-Jun-2015

719 views

Category:

Education


5 download

DESCRIPTION

compiler programming in c# report

TRANSCRIPT

Page 1: Compiler programming in c# report
Page 2: Compiler programming in c# report

تتكون لغة البرمجة من مجموعة من الرموز التي ستخدم في وصف " Programming Languages" لغات البرمجه

هي الطريقة الوحيدة التي تتم بواسطتها إعطاء االوامر للحاسوب . العمليات التي يطلب المستخدم من الحاسوب إنجازها

نشاء الجديد منها يأخذ ليقوم بإنجاز المهمات والوظائف المحددة هذا هو ما جعل االهتمام بلغات البرمجة وتطويرها وإ

.حيزا كبيرا من وقت المبرمجين ومن اهتمامهم

ال يمكن " High Level Language "أو بلغة عالية المستوى " Assembly "إن البرامج المكتوبة بلغة التجميع

Machine " الحاسوب وهي لغة اآللة" يفهمها"تنفيذها من قبل الحاسوب إال بعد تحويلها إلى اللغة الوحيدة التي

Language " عملية التحويل هذه تسمى ترجمة" Compiling "والبرنامج الذي يقوم بالترجمة يسمى مترجم "

Compiler. "

بناءا على ما سبق يمكن القول أن المترجم هو عبارة عن برنامج يمكنه قراءة البرنامج المكتوب بإحدى اللغات عالية

" Source Program "والذي يسمى البرنامج المصدري " Source Language" المستوى التي تسمى لغة المصدر

" .Target Program "وترجمته إلى برنامج مكافئ بلغة اآللة يسمى برنامج الهدف

باكتشاف الترجمة عملية خالل أيضا يقوم وإنما اللغة تحويل تقتصر فقط على ال المترجم مهمة أن بالذكر الجدير من

للتنفيذ قابال فيه يكون الذي الشكل على الهدف برنامج يصبح عندما. المصدري البرنامج عليها يحتوي قد التي االخطاء

.التنفيذ عملية تتم أي .مخرجات ونتائج إلى المدخالت تحويل يتم فقط عندها

مقدمة

Source Program Target Program Compiler

Page 3: Compiler programming in c# report

هو الذي يقوم بالقراءة الفعلية لبرنامج المصدر على Scanner""إن محلل المفردات أو مايطلق عليه احيانا بالماسح

لغة البرمجة وبالتالي فإن "Tokens"هيئة سلسلة متتابعة من الحروف التي تكون وحدات ذات مفردات تكون مفردات

.Scanner""يسمى او ما "Lexical Analyzer"ت هو تمييز مفردات اللغة ويقوم بذلك برنامج يسمىدور محلل المفردا

تتلقى شفرة المصدر من محلل "Parsing"أن مرحلة تحليل الصيغ النحوية أو مايطلق عليها أحيانا مرحلة اإلعراب

المفردات ليقوم بتحليل الصيغ النحوية لتحديد هيكل البرنامج وذلك مثل تحليل القواعد لجملة من اللغات الطبيعية

أي تقوم هذه المرحلة "Syntax Tree"أو شجرة النحو "Parse Tree"ونتيجة لهذا المرحلة يتم إنشاء شجرة اإلعراب

بحث في تركيب الجمله وترتيب الكلمات أو مفردات اللغة بشكل صحيح في الجملة ويقوم بهذا بتحليل البرنامج وال

. "Parser"يسمى أو ما "Syntax Analyzer"برنامج يسمى

إن دالالت أو معاني البرنامج هو المعنى الخاص في مقابله صيغة نحوية ويتم تحديد دالالت أي برنامج من خالل سلوكه

أثناء التشغيل ولكن معظم لغات البرمجة لديها بعض الصفات التي يمكن تحديدها قبل البدء في التشغيل ولكن تلك

Static" الصيغ النحوية ويطلق على هذه الصفات الدالالت الثابتة الصفات ال يستطيع وصفها أو تحليلها بواسطة محلل

Semantic" ومهمة محلل الدالالت هو تحليل هذا النوع من الدالالت التي تشمل بشكل اساسي على التعريفات

"Declarations" واختبار األنواع للبيانات"Type Checking" الت تسمى ، والمعلومات اإلضافية التي تعكس هذه الدال

ويتم تحديدها أثناء المرحل الحالية في هذه المرحله وغالبا ما يتم إضافتها كحواشي "Attributes"خصائص أو صفات

إذا هذه المرحله هي مرحلة . "Symbol Table"شجرة اإلعراب أو شجرة النحو أو قد تضاف إلى جدول الرموز الخاص

تحليل الجمل من ناحية معاني ودالالت األلفاظ المختلفه في جمل وعبارات البرنامج ، والذي يقوم بهذا برنامج يسمى

"Semantic Analyzer" .

ممراحل المترج

Lexical Analysisالمفرداتمحلل

Syntax Analysisالصيغ النحويةمحلل

Semantic Analysisالدالالتمحلل

Page 4: Compiler programming in c# report

the program pic.

Page 5: Compiler programming in c# report

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Text.RegularExpressions; using System.Windows.Forms.PropertyGridInternal; namespace compiler { public partial class compiler : Form { public compiler() { InitializeComponent(); } string[] op = {"{","}","[","]","(",")",".",",",":",";","+","-","*","/","%","&" + "|","^","!","~","=","<",">","?","??","::","++","--","&&","||","->","==","!=","<=",">=" + "+=","-=","*=","/=","%=","&=","|=","^=","<<","<<=","=>","'" }; string[] sy = { "#", "@", "$", "~", "`" }; string[] di = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; string[] leu = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; string[] lel = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; public static string[] syentax = new string[150]; string[] keyword ={"abstract","as","base","bool","break","byte","case","catch","char","checked","class","const","continue","decimal","default" , "delegate","do","double","else","enum","event","explicit","extern","false","finally","fixed","float","for","foreach","goto","if","implicit" ,"in","int", "interface","internal","is","lock","long","namespace","new","null","object","operator ","out","override","params","private","protected","public", "readonly","ref","return","sbyte","sealed","short","sizeof","stackalloc","static","string","struct","switch","this" ,"throw","true","try","typeof", "uint","ulong","unchecked","unsafe","ushort","using","virtual","void","volatile", "while"}; string[] operators = { "-", "+", "=", "/", "*", "%", "!", "<", ">" };

Source Code

Page 6: Compiler programming in c# report

string[] symbls = { ";", ",", "(", ")", "{", "}", "[", "]" }; //string token = ""; string m; char[] work; int r = 0; int i; private void picopen_Click(object sender, EventArgs e) { richTextBox1.Clear(); OpenFileDialog op = new OpenFileDialog(); if (op.ShowDialog() == DialogResult.OK) { StreamReader sr = new StreamReader(op.FileName); try { while ((m = sr.ReadLine().ToString()) != null) { richTextBox1.Text += m + "\r"; } } catch { } sr.Close(); } } private void pictureBox2_Click(object sender, EventArgs e) { Close(); } private void pictureBox3_Click(object sender, EventArgs e) { Close(); } private void lexical_Click(object sender, EventArgs e) { listBox1.Items.Clear(); work = richTextBox1.Text.ToCharArray(); r = richTextBox1.TextLength; string textlex, cy; int j = 0; cy = richTextBox1.Text; if (richTextBox1.Text.Length > 0) { textlex = ""; for (int i = 0; i < richTextBox1.Text.Length; i++) { j = i + 1; switch (richTextBox1.Text[i]) { case ' ': { listBox1.Items.Add(textlex); if (keyword.Contains(textlex))

Page 7: Compiler programming in c# report

{ listBox2.Items.Add("Keyword"); } else { check(textlex); } textlex = ""; }; break; case '\n': { listBox1.Items.Add(textlex); if (keyword.Contains(textlex)) { listBox2.Items.Add("Keyword"); } else { check(textlex); } textlex = ""; }; break; case '\r': { listBox1.Items.Add(textlex); if (keyword.Contains(textlex)) { listBox2.Items.Add("Keyword"); } else { check(textlex); } textlex = ""; }; break; case '+': { try { switch (richTextBox1.Text[i + 1]) { case '\r': { test(textlex, "+", "Addition Operator "); textlex = ""; //test(textlex, "++", "Increment by 1 "); i++; textlex = ""; }; break; case '+': { test(textlex, "++", "Increment by 1 "); i++; textlex = ""; }; break; case '=': { test(textlex, "+=", "Addition Assignment Operator"); i++; textlex = ""; }; break; // --i; default: { test(textlex, "+", "Addition Operator "); textlex = ""; } break; } } catch { test(textlex, "+", "Addition Operator "); textlex = ""; } }; break;

Page 8: Compiler programming in c# report

case '=': { try { switch (richTextBox1.Text[i + 1]) { case '=': { test(textlex, "==", "Equality Operator"); i++; textlex = ""; }; break; default: { test(textlex, "=", "Equal Operator "); textlex = ""; } break; } } catch { test(textlex, "=", "Equal Operator "); textlex = ""; } }; break; case '|': { try { switch (richTextBox1.Text[i + 1]) { case '|': { test(textlex, "||", "Logical Operator"); i++; textlex = ""; }; break; default: { test(textlex, "|", "Or Operator "); textlex = ""; } break; } } catch { test(textlex, "|", "Or Operator "); textlex = ""; } }; break; case '&': { try { switch (richTextBox1.Text[i + 1]) { case '&': { test(textlex, "&&", "Logical Operator"); i++; textlex = ""; }; break; default: { test(textlex, "&", "And Operator "); textlex = ""; } break; } } catch { test(textlex, "&", "And Operator "); textlex = ""; } }; break;

Page 9: Compiler programming in c# report

case '!': { try { switch (richTextBox1.Text[i + 1]) { case '=': { test(textlex, "!=", "Compares "); i++; textlex = ""; }; break; default: { test(textlex, "!", "Not Operator "); textlex = ""; } break; } } catch { test(textlex, "!", "Not Operator "); textlex = ""; } }; break; case '-': { try { switch (richTextBox1.Text[i + 1]) { case '=': { test(textlex, "-=", "Subtraction Assignment Operator "); i++; textlex = ""; }; break; case '-': { test(textlex, "--", "decrement by 1 "); i++; textlex = ""; }; break; default: { test(textlex, "-", "Mins Operator "); textlex = ""; } break; } } catch { test(textlex, "-", "Mins Operator "); textlex = ""; } }; break; case '*': { try { switch (richTextBox1.Text[i + 1]) { case '=': { test(textlex, "*=", "Mulity Assignment Operator "); i++; textlex = "";

Page 10: Compiler programming in c# report

}; break; case '/': { i++; textlex = ""; }; break; default: { test(textlex, "*", "Multiply Operator "); textlex = ""; } break; } } catch { test(textlex, "*", "Multiply Operator "); textlex = ""; } }; break; case '^': { try { switch (richTextBox1.Text[i + 1]) { case '=': { test(textlex, "^=", "Exclusive Assignment Operator "); i++; textlex = ""; }; break; default: { test(textlex, "^", "Exclusive Operator "); textlex = ""; } break; } } catch { test(textlex, "^", "Exclusive Operator "); textlex = ""; } }; break; case '%': { try { switch (richTextBox1.Text[i + 1]) { case '=': { test(textlex, "%=", "Mod Assignment Operator "); i++; textlex = ""; }; break; default: { test(textlex, "%", "Mod Operator "); textlex = ""; } break; } } catch { test(textlex, "%", "Mod Operator "); textlex = ""; } }; break;

Page 11: Compiler programming in c# report

case '/': { try { switch (richTextBox1.Text[i + 1]) { case '/': { try { listBox1.Items.Add(textlex); if (keyword.Contains(textlex)) { listBox2.Items.Add("Keyword"); } else { check(textlex); } while (richTextBox1.Text[i] != '\n') { i++; } textlex = ""; } catch { }; }; break; case '*': { listBox1.Items.Add(textlex); if (keyword.Contains(textlex)) { listBox2.Items.Add("Keyword"); } else { check(textlex); } while (richTextBox1.Text[i] != '*') { while (richTextBox1.Text[i + 1] != '/') { i++; } } i++; textlex = ""; }; break; case '=': { test(textlex, "/=", "Divid Assignment Operator "); i++; textlex = ""; }; break; default: { test(textlex, "/", "Divid Operator "); textlex = ""; } break; } } catch { test(textlex, "/", "Divid Operator "); textlex = ""; } }; break; case '<': { try { switch (richTextBox1.Text[i + 1]) { case '<':

Page 12: Compiler programming in c# report

{ test(textlex, "<<", "Left-Shift Operator "); i++; textlex = ""; }; break; case '=': { test(textlex, "<=", "Compares Operator "); i++; textlex = ""; }; break; default: { test(textlex, "<", "Compares Operator"); textlex = ""; } break; } } catch { test(textlex, "<", "Compares Operator"); textlex = ""; } }; break; case '>': { try { switch (richTextBox1.Text[i + 1]) { case '>': { test(textlex, ">>", "Right-Shift Operator "); i++; textlex = ""; }; break; case '=': { test(textlex, ">=", "Compares Operator"); i++; textlex = ""; }; break; default: { test(textlex, ">", "Compares Operator"); textlex = ""; } break; } } catch { test(textlex, ">", "Compares Operator"); textlex = ""; } }; break; case ';': { test(textlex, ";", "Semicolon Char_End_Code"); textlex = ""; }; break; case ',': { test(textlex, ",", "Comma Space word"); textlex = ""; }; break; case '\'': { test(textlex, "\'", "Qutation Singel Charchter"); textlex = ""; }; break;

Page 13: Compiler programming in c# report

case '"': { test(textlex, "\"", "Double Qutation String"); textlex = ""; }; break; case ':': { test(textlex, ":", "Conditional"); textlex = ""; }; break; case '?': { test(textlex, "?", "Qution mark Conditional"); textlex = ""; }; break; case '\\': { test(textlex, "\\", "Backslash "); textlex = ""; }; break; case '.': { test(textlex, ".", "period "); textlex = ""; }; break; case '(': { test(textlex, "(", "Parentheses " + "Casting"); textlex = ""; }; break; case ')': { test(textlex, ")", "Parentheses " + "Casting"); textlex = ""; }; break; case ']': { test(textlex, "]", "square brackets " + "Indexing"); textlex = ""; }; break; case '[': { test(textlex, "[", "square brackets " + "Indexing"); textlex = ""; }; break; case '{': { test(textlex, "{", "optional brackets "); textlex = ""; }; break; case '}': { test(textlex, "}", "optional brackets "); textlex = ""; }; break; default: { textlex += richTextBox1.Text[i]; if (i == richTextBox1.Text.Length - 1) { listBox1.Items.Add(textlex); if (keyword.Contains(textlex)) { listBox2.Items.Add("keyword"); }

Page 14: Compiler programming in c# report

else { check(textlex); } } } break; } } } else { MessageBox.Show("There is no exprission in textbox", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void check(string s) { int x = 0, y = 0, z = 0, v = 0, r = 0, e = 0, m = 0; for (int i = 0; i < s.Length; i++) { if (leu.Contains(Convert.ToString(s[i])) | lel.Contains(Convert.ToString(s[i])) | s[i] == '_' | di.Contains(Convert.ToString(s[i]))) { x++; if (leu.Contains(Convert.ToString(s[0])) | lel.Contains(Convert.ToString(s[0])) | s[0] == '_') { v++; } } if (s[i] == '.' | di.Contains(Convert.ToString(s[i]))) { y++; r++; } if (sy.Contains(Convert.ToString(s[i]))) { z++; e++; } if (s[i] == ' ') { m++; } } if ((x == s.Length) & (v > 0)) { listBox2.Items.Add("Ident"); v = 0; } else if ((y == s.Length) & Convert.ToBoolean(r > 0)) { listBox2.Items.Add("Digit"); } else if (z == s.Length & Convert.ToBoolean(e > 0)) { listBox2.Items.Add("Symbol"); } else if (m == s.Length) { listBox1.Items.Remove(s); } else { listBox2.Items.Add("Error_Ident"); } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { int i = ((ListBox)sender).SelectedIndex; listBox1.SelectedIndex = i;

Page 15: Compiler programming in c# report

listBox2.SelectedIndex = i; } private void test(string s, string msg1, string msg2) { listBox1.Items.Add(s); if (keyword.Contains(s)) { listBox2.Items.Add("Keyword"); } else { check(s); } listBox1.Items.Add(msg1); listBox2.Items.Add(msg2); } void coun(string token) { Decimal z; if (Decimal.TryParse(token, out z)) { listBox1.Items.Add(token); listBox2.Items.Add("number"); } else if (operators.Contains(token)) { listBox1.Items.Add(token); listBox2.Items.Add("operator"); } else if (symbls.Contains(token)) { if (i < work.Length - 1) if (symbls.Contains(work[i + 1].ToString())) { token += work[i + 1]; listBox1.Items.Add(token); listBox2.Items.Add("symbols"); } else { listBox1.Items.Add(token); listBox2.Items.Add("symbols"); } } } int count; string[] key = { "main", "for", "case", "switch", "if", "while", "return" };

Page 16: Compiler programming in c# report

private void pictureBox5_Click(object sender, EventArgs e) { count = listBox1.Items.Count; listBox3.Items.Clear(); for (int c = 0; c <= listBox1.Items.Count - 1; c++) { listBox3.Items.Add(listBox1.Items[c].ToString()); syentax[c] = listBox1.Items[c].ToString(); } } private void pictureBox4_Click(object sender, EventArgs e) { int dd; for (int i = 0; i < count; i++) { if (key.Contains(syentax[i])) { subnode(i); ii++; i += kk; dd = i; if (key.Contains(syentax[dd])) { subnode(dd); i += kk; ii++; } } else { treeView1.Nodes.Add(syentax[i]); ii++; } } } int kk=0; int ii = 0; private void button1_Click(object sender, EventArgs e) { } void subnode(int i) { string []syy= new string[100]; bool ch =false;

Page 17: Compiler programming in c# report

int j = 0; int k = i+1; int f = 0; treeView1.Nodes.Add(syentax[i]); while (!ch) { if (syentax[k] == "}") { syy[++f] = syentax[k]; ch = true; } else if (syentax[k] == " " || syentax[k] == "\n\n" || syentax[k] == "\t\t" || syentax[k] == " ") { k++; } else { syy[f] = syentax[k]; k++; f++; } } treeView1.Nodes[ii].Nodes.Add(syy[j]); treeView1.Nodes[ii].Nodes.Add("exp"); j++; k = 1; while (f>=0) { if (syy[j] == ")") { treeView1.Nodes[ii].Nodes.Add(syy[j]); k+=3; } else if (syy[j] == "{") { treeView1.Nodes[ii].Nodes.Add(syy[j]); treeView1.Nodes[ii].Nodes.Add("exp"); } else if (syy[j] == "}") { treeView1.Nodes[ii].Nodes.Add(syy[j]); ch = false; } else treeView1.Nodes[ii].Nodes[k].Nodes.Add(syy[j]);

Page 18: Compiler programming in c# report

j++; f--; } kk = j-1; } string ty, idd, vala; private void button1_Click_1(object sender, EventArgs e) { dataGridView1.ColumnCount = 4; dataGridView1.Columns[0].Name = "TYPE"; dataGridView1.Columns[1].Name = "NAME"; dataGridView1.Columns[2].Name = "VALUEO"; dataGridView1.Columns[3].Name = "SCOPE"; int tt = 0; for (int i = 0; i < count; i++) { if (syentax[i].Trim() == "int" || syentax[i].Trim() == "char" || syentax[i].Trim() == "float" || syentax[i].Trim() == "string") { ty = syentax[i]; idd = syentax[i + 1]; if (syentax[i + 2] == "=") { vala = syentax[i + 3]; } else { vala = "null"; } dataGridView1.Rows.Add(); dataGridView1.Rows[tt].Cells[0].Value = ty; dataGridView1.Rows[tt].Cells[1].Value = idd; dataGridView1.Rows[tt].Cells[2].Value = vala; dataGridView1.Rows[tt].Cells[3].Value = "pablic"; tt++; i += 3; } } } } }

END SOURCE CODE;;;

Page 19: Compiler programming in c# report