using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
namespace Laxical_Anlayzer
{
public class Program
{
string output = "";
Hashtable hashtable = new Hashtable();
Hashtable lexems_token = new Hashtable();
public void read()
{
hashtable.Add("void", "keyword");
hashtable.Add("main", "keyword");
hashtable.Add("int", "keyword");
hashtable.Add("if", "keyword");
hashtable.Add("for", "keyword");
hashtable.Add("float", "keyword");
hashtable.Add("while", "keyword");
hashtable.Add("+", "operator");
hashtable.Add("-", "operator");
hashtable.Add("*", "operator");
hashtable.Add("/", "operator");
hashtable.Add("(", "operator");
hashtable.Add(")", "operator");
hashtable.Add("{", "operator");
hashtable.Add("}", "operator");
hashtable.Add(";", "operator");
hashtable.Add(",", "operator");
hashtable.Add("=", "operator");
}
//////////////////////////////////////////////////////////////
bool LookUP(string str)
{
foreach (DictionaryEntry entry in hashtable)
{
if (lexems_token.Contains(str) == true)
{
return false;
}
} return true;
}
////////////////////////////////////////////////////////////////
public void ReomveComments()
{
using (TextReader reader = File.OpenText(@"E:\\Scan.cpp"))
{
while (reader.Peek() > 0)
{
char ch1, ch2, ch3, ch4;
ch1 = (char)reader.Read();
if (ch1 == '/')
{
ch2 = (char)reader.Read();
if (ch2 == '/')
{
while (ch2 != 10)
{
ch2 = (char)reader.Read();
}
continue;
}
////////////////////////////////////
else if (ch2 == '*')
{
ch3 = (char)reader.Read();
while (ch3 != '*')
{
ch4 = (char)reader.Read();
if (ch4 == '/')
break;
} continue;
}
}
output += ch1;
}
reader.Close();
}
}
///////////////////////////////////////////////////////////////////////
void remove_spaces()
{
string temp = null;
{
temp = Regex.Replace(output, "\n\r\t", " ");
temp = Regex.Replace(temp, "\\s+", " ");
output = temp;
}
}
////////////////////////////////////////////////////////////////////
public void Recognize_Keyword()
{
string temp = "";
string value="";
for (int i = 0; i < output.Length; i++)
{
while (validChar(output[i]))
{
temp += output[i];
i++;
}
if (temp != string.Empty)
{
if (hashtable.Contains(temp) == true)
{ value=(string)hashtable[temp];
// Console.Write(" " + temp + " ");
// Console.WriteLine(value);
if (LookUP(temp)==true)
{
lexems_token.Add(temp, value);
}
temp = "";
value="";
}
else
{
Const_Identify(temp);
temp = "";
}
}
if (output[i] == ' ')
{
continue;
}
else
{
char c;
c = output[i];
if (hashtable.Contains(c.ToString()) == true)
{// Console.WriteLine( c + "Operator");
string str = null;
str = c.ToString();
if (LookUP(str) == true)
{
lexems_token.Add(str, "Operator");
}
c = ' ';
}
}
}
}
//////////////////////////////////////////////////
bool validChar(char chr)
{
if ((chr >= 48 && chr <= 57) || (chr >= 65 && chr <= 90) || (chr >= 97 && chr <= 122))
{
return true;
}
else
{
return false;
}
}
/////////////////////////////////////////////////////////////
public void Const_Identify(string temp)
{
bool Numeric = true;
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] <= 57 && temp[i] >= 48)
{
continue;
}
else
{
Numeric = false;
break;
}
}
if (Numeric == true)
{
// Console.WriteLine( temp + "Numeric");
if (LookUP(temp) == true)
{
lexems_token.Add(temp, "Numeric");
} return;
}
if (temp[0] <= 57 && temp[0] >= 48)
{
// Console.WriteLine( temp + " In-Vaild Idenfier");
}
else
{
// Console.WriteLine( temp +" Identifier");
if (LookUP(temp))
{
lexems_token.Add(temp, "Identifier");
}
}
}
public void Display_new_hashtable()
{
foreach (DictionaryEntry pair in lexems_token)
{
Console.WriteLine(pair.Key +" " + pair.Value);
}
}
public static void Main() // "E:\\la.txt"
{
Program pg = new Program();
pg.read();
pg.ReomveComments();
pg.remove_spaces();
pg.Recognize_Keyword();
pg.Display_new_hashtable();
Console.ReadLine();
}
}
}
Download here
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
namespace Laxical_Anlayzer
{
public class Program
{
string output = "";
Hashtable hashtable = new Hashtable();
Hashtable lexems_token = new Hashtable();
public void read()
{
hashtable.Add("void", "keyword");
hashtable.Add("main", "keyword");
hashtable.Add("int", "keyword");
hashtable.Add("if", "keyword");
hashtable.Add("for", "keyword");
hashtable.Add("float", "keyword");
hashtable.Add("while", "keyword");
hashtable.Add("+", "operator");
hashtable.Add("-", "operator");
hashtable.Add("*", "operator");
hashtable.Add("/", "operator");
hashtable.Add("(", "operator");
hashtable.Add(")", "operator");
hashtable.Add("{", "operator");
hashtable.Add("}", "operator");
hashtable.Add(";", "operator");
hashtable.Add(",", "operator");
hashtable.Add("=", "operator");
}
//////////////////////////////////////////////////////////////
bool LookUP(string str)
{
foreach (DictionaryEntry entry in hashtable)
{
if (lexems_token.Contains(str) == true)
{
return false;
}
} return true;
}
////////////////////////////////////////////////////////////////
public void ReomveComments()
{
using (TextReader reader = File.OpenText(@"E:\\Scan.cpp"))
{
while (reader.Peek() > 0)
{
char ch1, ch2, ch3, ch4;
ch1 = (char)reader.Read();
if (ch1 == '/')
{
ch2 = (char)reader.Read();
if (ch2 == '/')
{
while (ch2 != 10)
{
ch2 = (char)reader.Read();
}
continue;
}
////////////////////////////////////
else if (ch2 == '*')
{
ch3 = (char)reader.Read();
while (ch3 != '*')
{
ch4 = (char)reader.Read();
if (ch4 == '/')
break;
} continue;
}
}
output += ch1;
}
reader.Close();
}
}
///////////////////////////////////////////////////////////////////////
void remove_spaces()
{
string temp = null;
{
temp = Regex.Replace(output, "\n\r\t", " ");
temp = Regex.Replace(temp, "\\s+", " ");
output = temp;
}
}
////////////////////////////////////////////////////////////////////
public void Recognize_Keyword()
{
string temp = "";
string value="";
for (int i = 0; i < output.Length; i++)
{
while (validChar(output[i]))
{
temp += output[i];
i++;
}
if (temp != string.Empty)
{
if (hashtable.Contains(temp) == true)
{ value=(string)hashtable[temp];
// Console.Write(" " + temp + " ");
// Console.WriteLine(value);
if (LookUP(temp)==true)
{
lexems_token.Add(temp, value);
}
temp = "";
value="";
}
else
{
Const_Identify(temp);
temp = "";
}
}
if (output[i] == ' ')
{
continue;
}
else
{
char c;
c = output[i];
if (hashtable.Contains(c.ToString()) == true)
{// Console.WriteLine( c + "Operator");
string str = null;
str = c.ToString();
if (LookUP(str) == true)
{
lexems_token.Add(str, "Operator");
}
c = ' ';
}
}
}
}
//////////////////////////////////////////////////
bool validChar(char chr)
{
if ((chr >= 48 && chr <= 57) || (chr >= 65 && chr <= 90) || (chr >= 97 && chr <= 122))
{
return true;
}
else
{
return false;
}
}
/////////////////////////////////////////////////////////////
public void Const_Identify(string temp)
{
bool Numeric = true;
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] <= 57 && temp[i] >= 48)
{
continue;
}
else
{
Numeric = false;
break;
}
}
if (Numeric == true)
{
// Console.WriteLine( temp + "Numeric");
if (LookUP(temp) == true)
{
lexems_token.Add(temp, "Numeric");
} return;
}
if (temp[0] <= 57 && temp[0] >= 48)
{
// Console.WriteLine( temp + " In-Vaild Idenfier");
}
else
{
// Console.WriteLine( temp +" Identifier");
if (LookUP(temp))
{
lexems_token.Add(temp, "Identifier");
}
}
}
public void Display_new_hashtable()
{
foreach (DictionaryEntry pair in lexems_token)
{
Console.WriteLine(pair.Key +" " + pair.Value);
}
}
public static void Main() // "E:\\la.txt"
{
Program pg = new Program();
pg.read();
pg.ReomveComments();
pg.remove_spaces();
pg.Recognize_Keyword();
pg.Display_new_hashtable();
Console.ReadLine();
}
}
}
Download here