SlideShare a Scribd company logo
Compiler programming in c# report
‫مقدمة‬
‫لغات البرمجه " ‪ " Programming Languages‬تتكون لغة البرمجة من مجموعة من الرموز التي ستخدم في وصف‬
‫العمليات التي يطلب المستخدم من الحاسوب إنجازها . هي الطريقة الوحيدة التي تتم بواسطتها إعطاء االوامر للحاسوب‬
‫ليقوم بإنجاز المهمات والوظائف المحددة هذا هو ما جعل االهتمام بلغات البرمجة وتطويرها وإنشاء الجديد منها يأخذ‬
‫حيزا كبيرا من وقت المبرمجين ومن اهتمامهم .‬
‫إن البرامج المكتوبة بلغة التجميع "‪ " Assembly‬أو بلغة عالية المستوى "‪ " High Level Language‬ال يمكن‬
‫تنفيذها من قبل الحاسوب إال بعد تحويلها إلى اللغة الوحيدة التي "يفهمها" الحاسوب وهي لغة اآللة "‪Machine‬‬
‫‪ "Language‬عملية التحويل هذه تسمى ترجمة "‪ " Compiling‬والبرنامج الذي يقوم بالترجمة يسمى مترجم "‬
‫‪. "Compiler‬‬
‫بناءاً على ما سبق يمكن القول أن المترجم هو عبارة عن برنامج يمكنه قراءة البرنامج المكتوب بإحدى اللغات عالية‬
‫المستوى التي تسمى لغة المصدر " ‪ "Source Language‬والذي يسمى البرنامج المصدري "‪" Source Program‬‬
‫وترجمته إلى برنامج مكافئ بلغة اآللة يسمى برنامج الهدف "‪. " Target Program‬‬

‫‪Target Program‬‬

‫‪Compiler‬‬

‫‪Source Program‬‬

‫من الجدير بالذكر أن مهمة المترجم ال تقتصر فقط على تحويل اللغة وإنما يقوم أيضا خالل عملية الترجمة باكتشاف‬
‫االخطاء التي قد يحتوي عليها البرنامج المصدري. عندما يصبح برنامج الهدف على الشكل الذي يكون فيه قابال للتنفيذ‬
‫عندها فقط يتم تحويل المدخالت إلى مخرجات ونتائج. أي تتم عملية التنفيذ.‬
‫مراحل المترجم‬
‫محلل المفردات ‪Lexical Analysis‬‬
‫إن محلل المفردات أو مايطلق عليه احياناً بالماسح "‪ "Scanner‬هو الذي يقوم بالقراءة الفعلية لبرنامج المصدر على‬
‫هيئة سلسلة متتابعة من الحروف التي تك ّن وحدات ذات مفردات تكون مفردات "‪ "Tokens‬لغة البرمجة وبالتالي فإن‬
‫و‬
‫دور محلل المفردات هو تمييز مفردات اللغة ويقوم بذلك برنامج يسمى"‪ "Lexical Analyzer‬او ما يسمى"‪."Scanner‬‬

‫محلل الصيغ النحوية ‪Syntax Analysis‬‬
‫أن مرحلة تحليل الصيغ النحوية أو مايطلق عليها أحياناً مرحلة اإلعراب "‪ "Parsing‬تتلقى شفرة المصدر من محلل‬
‫المفردات ليقوم بتحليل الصيغ النحوية لتحديد هيكل البرنامج وذلك مثل تحليل القواعد لجملة من اللغات الطبيعية‬
‫ونتيجة لهذا المرحلة يتم إنشاء شجرة اإلعراب "‪ "Parse Tree‬أو شجرة النحو "‪ "Syntax Tree‬أي تقوم هذه المرحلة‬
‫بتحليل البرنامج والبحث في تركيب الجمله وترتيب الكلمات أو مفردات اللغة بشكل صحيح في الجملة ويقوم بهذا‬
‫برنامج يسمى "‪ "Syntax Analyzer‬أو ما يسمى "‪. "Parser‬‬

‫محلل الدالالت ‪Semantic Analysis‬‬
‫إن دالالت أو معاني البرنامج هو المعنى الخاص في مقابله صيغة نحوية ويتم تحديد دالالت أي برنامج من خالل سلوكه‬
‫أثناء التشغيل ولكن معظم لغات البرمجة لديها بعض الصفات التي يمكن تحديدها قبل البدء في التشغيل ولكن تلك‬
‫الصفات ال يستطيع وصفها أو تحليلها بواسطة محلل الصيغ النحوية ويطلق على هذه الصفات الدالالت الثابتة ‪"Static‬‬
‫"‪ Semantic‬ومهمة محلل الدالالت هو تحليل هذا النوع من الدالالت التي تشمل بشكل اساسي على التعريفات‬
‫"‪ "Declarations‬واختبار األنواع للبيانات "‪ ، "Type Checking‬والمعلومات اإلضافية التي تعكس هذه الدالالت تسمى‬
‫خصائص أو صفات "‪ "Attributes‬ويتم تحديدها أثناء المرحل الحالية في هذه المرحله وغالباً ما يتم إضافتها كحواشي‬
‫شجرة اإلعراب أو شجرة النحو أو قد تضاف إلى جدول الرموز الخاص "‪ . "Symbol Table‬إذاً هذه المرحله هي مرحلة‬
‫تحليل الجمل من ناحية معاني ودالالت األلفاظ المختلفه في جمل وعبارات البرنامج ، والذي يقوم بهذا برنامج يسمى‬
‫"‪. "Semantic Analyzer‬‬
the program pic.
Source Codeٍ
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.IO;
System.Text.RegularExpressions;
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","continu
e","decimal","default" ,
"delegate","do","double","else","enum","event","explicit","extern","false","finally","fixed","float","f
or","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","sw
itch","this" ,"throw","true","try","typeof",
"uint","ulong","unchecked","unsafe","ushort","using","virtual","void","volatile",
"while"};

string[] operators = { "-", "+", "=", "/", "*", "%", "!", "<", ">" };
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))
{ 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;
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;
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 = "";
}; 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;
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 '<':
{
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;
case '"':
{
test(textlex,
}; break;
case ':':
{
test(textlex,
}; break;
case '?':
{
test(textlex,
}; break;
case '':
{
test(textlex,
}; break;
case '.':
{
test(textlex,
}; break;
case '(':
{
test(textlex,
}; break;
case ')':
{

""", "Double Qutation

":", "Conditional"); textlex = "";

"?", "Qution mark Conditional"); textlex = "";

"", "Backslash "); textlex = "";

".", "period "); textlex = "";

"(", "Parentheses

test(textlex, ")", "Parentheses
}; break;
case ']':
{
test(textlex,
}; break;
case '[':
{
test(textlex,
}; break;
case '{':
{
test(textlex,
}; break;
case '}':
{
test(textlex,
}; break;
default:
{

String"); textlex = "";

" + "Casting"); textlex = "";

" + "Casting"); textlex = "";

"]", "square brackets

" + "Indexing"); textlex = "";

"[", "square brackets

" + "Indexing"); textlex = "";

"{", "optional brackets

"}", "optional

brackets

textlex += richTextBox1.Text[i];
if (i == richTextBox1.Text.Length - 1)
{
listBox1.Items.Add(textlex);
if (keyword.Contains(textlex))
{
listBox2.Items.Add("keyword");
}

"); textlex = "";

"); textlex = "";
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;
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" };
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;
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] == " ")
{
k++;

" || syentax[k] == "nn" || syentax[k] == "tt" ||

}
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]);
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
dataGridView1.Columns[1].Name
dataGridView1.Columns[2].Name
dataGridView1.Columns[3].Name

=
=
=
=

"TYPE";
"NAME";
"VALUEO";
"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
dataGridView1.Rows[tt].Cells[1].Value
dataGridView1.Rows[tt].Cells[2].Value
dataGridView1.Rows[tt].Cells[3].Value
tt++;
i += 3;
}
}
}
}
}

END SOURCE CODE;;;

=
=
=
=

ty;
idd;
vala;
"pablic";
Compiler programming in c# report

More Related Content

PDF
JQuery
PDF
Effective java 摘選條目分享 2 - 泛型
DOCX
Propuesta..sujei
DOC
Compiler Design(NANTHU NOTES)
PDF
SOFTWARE ENGINEERING HOME WORK
PDF
Internet information service ( iis ) مدير خدمة الانترنت
PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
PDF
Storytelling For The Web: Integrate Storytelling in your Design Process
JQuery
Effective java 摘選條目分享 2 - 泛型
Propuesta..sujei
Compiler Design(NANTHU NOTES)
SOFTWARE ENGINEERING HOME WORK
Internet information service ( iis ) مدير خدمة الانترنت
2024 Trend Updates: What Really Works In SEO & Content Marketing
Storytelling For The Web: Integrate Storytelling in your Design Process

Recently uploaded (7)

PDF
ಶ್ರೀ ಕ್ಷೇತ್ರ ಚಂಪಕಧಾಮ ಸ್ವಾಮಿ ದೇವಾಲಯSri Kshetra Champakadham Swamy Temple
PPTX
Slide Ibadah siang 29 mei 2025 jika .pptx
PPTX
Tahfidz Qur’an TIMING tampa musik bagian 2.pptx
PDF
"ಶ್ರೀ ಕ್ಷೇತ್ರ ಚಂಪಕಧಾಮ ಸ್ವಾಮಿ ದೇವಾಲಯ""Sri Kshetra Champakadham Swamy Temple"
PPTX
Coklat Beige Ilustrasi 3 Dimensi Tugas Kelompok Presentasi.pptx
PDF
فێرکردن و فێربوونی مۆدێرن.pdf دەروازەیەک بۆ
PPTX
science grade 7 quiz_Scientific Method.pptx
ಶ್ರೀ ಕ್ಷೇತ್ರ ಚಂಪಕಧಾಮ ಸ್ವಾಮಿ ದೇವಾಲಯSri Kshetra Champakadham Swamy Temple
Slide Ibadah siang 29 mei 2025 jika .pptx
Tahfidz Qur’an TIMING tampa musik bagian 2.pptx
"ಶ್ರೀ ಕ್ಷೇತ್ರ ಚಂಪಕಧಾಮ ಸ್ವಾಮಿ ದೇವಾಲಯ""Sri Kshetra Champakadham Swamy Temple"
Coklat Beige Ilustrasi 3 Dimensi Tugas Kelompok Presentasi.pptx
فێرکردن و فێربوونی مۆدێرن.pdf دەروازەیەک بۆ
science grade 7 quiz_Scientific Method.pptx
Ad
Ad

Compiler programming in c# report

  • 2. ‫مقدمة‬ ‫لغات البرمجه " ‪ " Programming Languages‬تتكون لغة البرمجة من مجموعة من الرموز التي ستخدم في وصف‬ ‫العمليات التي يطلب المستخدم من الحاسوب إنجازها . هي الطريقة الوحيدة التي تتم بواسطتها إعطاء االوامر للحاسوب‬ ‫ليقوم بإنجاز المهمات والوظائف المحددة هذا هو ما جعل االهتمام بلغات البرمجة وتطويرها وإنشاء الجديد منها يأخذ‬ ‫حيزا كبيرا من وقت المبرمجين ومن اهتمامهم .‬ ‫إن البرامج المكتوبة بلغة التجميع "‪ " Assembly‬أو بلغة عالية المستوى "‪ " High Level Language‬ال يمكن‬ ‫تنفيذها من قبل الحاسوب إال بعد تحويلها إلى اللغة الوحيدة التي "يفهمها" الحاسوب وهي لغة اآللة "‪Machine‬‬ ‫‪ "Language‬عملية التحويل هذه تسمى ترجمة "‪ " Compiling‬والبرنامج الذي يقوم بالترجمة يسمى مترجم "‬ ‫‪. "Compiler‬‬ ‫بناءاً على ما سبق يمكن القول أن المترجم هو عبارة عن برنامج يمكنه قراءة البرنامج المكتوب بإحدى اللغات عالية‬ ‫المستوى التي تسمى لغة المصدر " ‪ "Source Language‬والذي يسمى البرنامج المصدري "‪" Source Program‬‬ ‫وترجمته إلى برنامج مكافئ بلغة اآللة يسمى برنامج الهدف "‪. " Target Program‬‬ ‫‪Target Program‬‬ ‫‪Compiler‬‬ ‫‪Source Program‬‬ ‫من الجدير بالذكر أن مهمة المترجم ال تقتصر فقط على تحويل اللغة وإنما يقوم أيضا خالل عملية الترجمة باكتشاف‬ ‫االخطاء التي قد يحتوي عليها البرنامج المصدري. عندما يصبح برنامج الهدف على الشكل الذي يكون فيه قابال للتنفيذ‬ ‫عندها فقط يتم تحويل المدخالت إلى مخرجات ونتائج. أي تتم عملية التنفيذ.‬
  • 3. ‫مراحل المترجم‬ ‫محلل المفردات ‪Lexical Analysis‬‬ ‫إن محلل المفردات أو مايطلق عليه احياناً بالماسح "‪ "Scanner‬هو الذي يقوم بالقراءة الفعلية لبرنامج المصدر على‬ ‫هيئة سلسلة متتابعة من الحروف التي تك ّن وحدات ذات مفردات تكون مفردات "‪ "Tokens‬لغة البرمجة وبالتالي فإن‬ ‫و‬ ‫دور محلل المفردات هو تمييز مفردات اللغة ويقوم بذلك برنامج يسمى"‪ "Lexical Analyzer‬او ما يسمى"‪."Scanner‬‬ ‫محلل الصيغ النحوية ‪Syntax Analysis‬‬ ‫أن مرحلة تحليل الصيغ النحوية أو مايطلق عليها أحياناً مرحلة اإلعراب "‪ "Parsing‬تتلقى شفرة المصدر من محلل‬ ‫المفردات ليقوم بتحليل الصيغ النحوية لتحديد هيكل البرنامج وذلك مثل تحليل القواعد لجملة من اللغات الطبيعية‬ ‫ونتيجة لهذا المرحلة يتم إنشاء شجرة اإلعراب "‪ "Parse Tree‬أو شجرة النحو "‪ "Syntax Tree‬أي تقوم هذه المرحلة‬ ‫بتحليل البرنامج والبحث في تركيب الجمله وترتيب الكلمات أو مفردات اللغة بشكل صحيح في الجملة ويقوم بهذا‬ ‫برنامج يسمى "‪ "Syntax Analyzer‬أو ما يسمى "‪. "Parser‬‬ ‫محلل الدالالت ‪Semantic Analysis‬‬ ‫إن دالالت أو معاني البرنامج هو المعنى الخاص في مقابله صيغة نحوية ويتم تحديد دالالت أي برنامج من خالل سلوكه‬ ‫أثناء التشغيل ولكن معظم لغات البرمجة لديها بعض الصفات التي يمكن تحديدها قبل البدء في التشغيل ولكن تلك‬ ‫الصفات ال يستطيع وصفها أو تحليلها بواسطة محلل الصيغ النحوية ويطلق على هذه الصفات الدالالت الثابتة ‪"Static‬‬ ‫"‪ Semantic‬ومهمة محلل الدالالت هو تحليل هذا النوع من الدالالت التي تشمل بشكل اساسي على التعريفات‬ ‫"‪ "Declarations‬واختبار األنواع للبيانات "‪ ، "Type Checking‬والمعلومات اإلضافية التي تعكس هذه الدالالت تسمى‬ ‫خصائص أو صفات "‪ "Attributes‬ويتم تحديدها أثناء المرحل الحالية في هذه المرحله وغالباً ما يتم إضافتها كحواشي‬ ‫شجرة اإلعراب أو شجرة النحو أو قد تضاف إلى جدول الرموز الخاص "‪ . "Symbol Table‬إذاً هذه المرحله هي مرحلة‬ ‫تحليل الجمل من ناحية معاني ودالالت األلفاظ المختلفه في جمل وعبارات البرنامج ، والذي يقوم بهذا برنامج يسمى‬ ‫"‪. "Semantic Analyzer‬‬
  • 5. Source Codeٍ using using using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.IO; System.Text.RegularExpressions; 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","continu e","decimal","default" , "delegate","do","double","else","enum","event","explicit","extern","false","finally","fixed","float","f or","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","sw itch","this" ,"throw","true","try","typeof", "uint","ulong","unchecked","unsafe","ushort","using","virtual","void","volatile", "while"}; string[] operators = { "-", "+", "=", "/", "*", "%", "!", "<", ">" };
  • 6. 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))
  • 7. { 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;
  • 8. 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;
  • 9. 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 = "";
  • 10. }; 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;
  • 11. 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 '<':
  • 12. { 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;
  • 13. case '"': { test(textlex, }; break; case ':': { test(textlex, }; break; case '?': { test(textlex, }; break; case '': { test(textlex, }; break; case '.': { test(textlex, }; break; case '(': { test(textlex, }; break; case ')': { """, "Double Qutation ":", "Conditional"); textlex = ""; "?", "Qution mark Conditional"); textlex = ""; "", "Backslash "); textlex = ""; ".", "period "); textlex = ""; "(", "Parentheses test(textlex, ")", "Parentheses }; break; case ']': { test(textlex, }; break; case '[': { test(textlex, }; break; case '{': { test(textlex, }; break; case '}': { test(textlex, }; break; default: { String"); textlex = ""; " + "Casting"); textlex = ""; " + "Casting"); textlex = ""; "]", "square brackets " + "Indexing"); textlex = ""; "[", "square brackets " + "Indexing"); textlex = ""; "{", "optional brackets "}", "optional brackets textlex += richTextBox1.Text[i]; if (i == richTextBox1.Text.Length - 1) { listBox1.Items.Add(textlex); if (keyword.Contains(textlex)) { listBox2.Items.Add("keyword"); } "); textlex = ""; "); textlex = "";
  • 14. 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;
  • 15. 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" };
  • 16. 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;
  • 17. 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] == " ") { k++; " || syentax[k] == "nn" || syentax[k] == "tt" || } 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]);
  • 18. 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 dataGridView1.Columns[1].Name dataGridView1.Columns[2].Name dataGridView1.Columns[3].Name = = = = "TYPE"; "NAME"; "VALUEO"; "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 dataGridView1.Rows[tt].Cells[1].Value dataGridView1.Rows[tt].Cells[2].Value dataGridView1.Rows[tt].Cells[3].Value tt++; i += 3; } } } } } END SOURCE CODE;;; = = = = ty; idd; vala; "pablic";