القائد الغالي
04-04-2008, 02:02 AM
السلام عليكم ورحمة الله وبركاته
تحية طيبة اخواني الأعضاء الكرام
طلب منا الدكتور مشروع بناء المحلل المفراداتي lexical analyser بلغة # C
بحيث يخرج جدول ال Symobl table ويعطينا ال token ورقم ال id تبعها
فياريت تساعدوني بالأفكار حتى لوكانت بلغة اخرى غير ال #C بس المطلوب شرح الفكرة حتى استطيع برمجتها بلغة # C والأكواد اذا تستطيعون ذلك ولكم مني كل الشكر والأحترام والتقدير
تحياتي
وهذا اخر ماتوصلت له من فمن يستطيع التعديل عليه اكون له شاكراً
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
public class Token
{
int code;
int attr;
}
public class SymbT
{
public string lexeme;
public int code;
}
public class LexicalA
{
SymbT[] SymbolTable = new SymbT[20];
char Inputbuff;
static bool WhiteSpace(char c1)
{
if(c1==' ')
return true;
else
return false;
}
static bool letter(char c1)
{
if ((c1 >= 'A' && c1 <= 'Z') || (c1 >= 'a' && c1 <= 'z'))
return true;
else
return false;
}
static bool digit(char c1)
{
if (c1 >= '0' && c1 <= '9')
return true;
else
return false;
}
public int lookup(string lexe)
{
for (int i = 0; i < 20; i++)
{
if (lexe == SymbolTable[i].lexeme)
return i;
}
return 0;
}
public int insert(string lexe, int tcode)
{
SymbolTable[tcode].lexeme = lexe;
SymbolTable[tcode].code = tcode;
return tcode;
}
static Token LexAn()
{
Token token = new Token();
int ID1=256;
int NUM=257;
int NONE=-1;
int state=0;
string lexbuf="";
char c;
do
{
switch(state)
{
case 0:
if( WhiteSpace(c));
else if(letter(c))
{lexbuf+=c; state=1;}
else if(digit(c))
{lexbuf+=c; state=2;}
else
{lexbuf+=c; state=3;}
break;
case 1:
if(letter(c)||(digit(c)))
{
lexbuf+=c;
}
else
{
int p=lookup(lexbuf);
if(p==0) insert(lexbuf,ID1);
token.code=SymbolTable[p].code;
token.attr=p;
return token;
}
break;
case 2:
if(digit(c))
lexbuf+=c;
else
{
token.code=NUM;
token.attr=Int32.Parse(lexbuf);
return token;
}
break;
case 3:
lexbuf+=c;
int tc = onechar(lexbuf);
token.attr=NONE;
if(tc==0){
// return c back to this Input buffer ;
token.code=ASCII_of_First_Char(lexbuf);
return token;
}
}
}
while(Inputbuff!=0);
}
static void Main()
{
LexAn();
}
}
}
تحياتي
تحية طيبة اخواني الأعضاء الكرام
طلب منا الدكتور مشروع بناء المحلل المفراداتي lexical analyser بلغة # C
بحيث يخرج جدول ال Symobl table ويعطينا ال token ورقم ال id تبعها
فياريت تساعدوني بالأفكار حتى لوكانت بلغة اخرى غير ال #C بس المطلوب شرح الفكرة حتى استطيع برمجتها بلغة # C والأكواد اذا تستطيعون ذلك ولكم مني كل الشكر والأحترام والتقدير
تحياتي
وهذا اخر ماتوصلت له من فمن يستطيع التعديل عليه اكون له شاكراً
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
public class Token
{
int code;
int attr;
}
public class SymbT
{
public string lexeme;
public int code;
}
public class LexicalA
{
SymbT[] SymbolTable = new SymbT[20];
char Inputbuff;
static bool WhiteSpace(char c1)
{
if(c1==' ')
return true;
else
return false;
}
static bool letter(char c1)
{
if ((c1 >= 'A' && c1 <= 'Z') || (c1 >= 'a' && c1 <= 'z'))
return true;
else
return false;
}
static bool digit(char c1)
{
if (c1 >= '0' && c1 <= '9')
return true;
else
return false;
}
public int lookup(string lexe)
{
for (int i = 0; i < 20; i++)
{
if (lexe == SymbolTable[i].lexeme)
return i;
}
return 0;
}
public int insert(string lexe, int tcode)
{
SymbolTable[tcode].lexeme = lexe;
SymbolTable[tcode].code = tcode;
return tcode;
}
static Token LexAn()
{
Token token = new Token();
int ID1=256;
int NUM=257;
int NONE=-1;
int state=0;
string lexbuf="";
char c;
do
{
switch(state)
{
case 0:
if( WhiteSpace(c));
else if(letter(c))
{lexbuf+=c; state=1;}
else if(digit(c))
{lexbuf+=c; state=2;}
else
{lexbuf+=c; state=3;}
break;
case 1:
if(letter(c)||(digit(c)))
{
lexbuf+=c;
}
else
{
int p=lookup(lexbuf);
if(p==0) insert(lexbuf,ID1);
token.code=SymbolTable[p].code;
token.attr=p;
return token;
}
break;
case 2:
if(digit(c))
lexbuf+=c;
else
{
token.code=NUM;
token.attr=Int32.Parse(lexbuf);
return token;
}
break;
case 3:
lexbuf+=c;
int tc = onechar(lexbuf);
token.attr=NONE;
if(tc==0){
// return c back to this Input buffer ;
token.code=ASCII_of_First_Char(lexbuf);
return token;
}
}
}
while(Inputbuff!=0);
}
static void Main()
{
LexAn();
}
}
}
تحياتي



