Search code examples
c#compiler-construction

Is there any way to identify reserved word in a code file through a program at runtime?


Is there any way to identify the reserved word in a code file through a c# program? I think there is a place where c# keeps its tokens. where I can match my file words and identify them as a reserved word or not s reserved word.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class Program
{
    static void Main(string[] args)
    {
        string filepath = @"C:\Users\yasir\Documents\Visual Studio 
      2017\Projects\ConsoleApp1\ConsoleApp1\ProductsController.cs";
        StreamReader sreader = new StreamReader(filepath); 
           //path of the file



        var lineCount = File.ReadAllLines(filepath).Count(line => 
          !string.IsNullOrWhiteSpace(line));
        string strFileContent = sreader.ReadToEnd(); //Read all the content
        sreader.Close();


        string[] words = strFileContent.Split(new char[] { ' ', '\r', '\n' 
          }, StringSplitOptions.RemoveEmptyEntries); 
        //Split by words and remove new lines empty entries

        foreach( string ew in words)
        {

            //here i have to write check reserved word logic...
        }

        Console.Write(lineCount);


        Console.ReadKey();
        }

        }
        }

Solution

  • first i take all words in external file to a list

         string[] words = strFileContent.Split(new char[] { ' ', '\r', '\n', 
         '.', ';' }, StringSplitOptions.RemoveEmptyEntries);
    

    then i make a list for tokens

       List<string> keywords = new List<string> 
        {"id","set","return","Id","EventHandler<EventArgs>",
        "eventhandler","event","collections","system",
        "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", 
        "using", "static", "virtual", "void", "volatile", "while" };
    

    then comparing each word of file with every element in this list we will get the reserved word

      var ikeywords = words.Select(i => 
           i.ToString()).Intersect(keywords).ToList();
    

    i have written this full program for

    -identifying reserved word

    -counting external file line

    -identify and Delete comments

       using System;
       using System.Collections.Generic;
       using System.IO;
       using System.Linq;
       using System.Text.RegularExpressions;
    
       namespace ConsoleApp1
        {
        class Program
          {
               static void Main(string[] args)
            {
    
    
          string filepath = @"C:\Users\yasir\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Product.cs";
            //1.getting file count
            int linecount = CountLines(filepath);
            Console.WriteLine($"total number of lines in program file is:{linecount}");
            //2.identifying reserved words
            ReservedWords(filepath);
    
            //discarding comments
           Console.WriteLine( DeleteComments(filepath));
    
    
    
    
    
            Console.ReadKey();
        }
        public static void ReservedWords(string filepath)
        {
    
            StreamReader sreader = new StreamReader(filepath);
    
    
            string strFileContent = sreader.ReadToEnd(); 
            string[] words = strFileContent.Split(new char[] { ' ', '\r', '\n', '.', ';' }, StringSplitOptions.RemoveEmptyEntries);
    
    
    
            List<string> keywords = new List<string> { "id","set","return","Id","EventHandler<EventArgs>","eventhandler","event","collections","system","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", "using", "static", "virtual", "void", "volatile", "while" };
    
            var ikeywords = words.Select(i => i.ToString()).Intersect(keywords).ToList();
    
            int result = 0;
    
            foreach (string ew in ikeywords)
            {
    
    
                result = result + 1;
                Console.WriteLine(ew + Environment.NewLine);
    
    
    
            }
            Console.WriteLine($"total number of keywords in program file is:{result}");
    
    
    
        }
    
        public static int CountLines(string filepath)
        {
            StreamReader sreader = new StreamReader(filepath);
            string strFileContent = sreader.ReadToEnd(); //Read all the content
            string[] words = strFileContent.Split(new char[] { ' ', '\r', '\n', '.', ';' }, StringSplitOptions.RemoveEmptyEntries);
            //Split by words and remove new lines empty entries
            sreader.Close();
    
            var lineCount = File.ReadAllLines(filepath).Count(line => !string.IsNullOrWhiteSpace(line));
    
            return lineCount;
        }
    
        public static string DeleteComments(string filename)
        {
    
            Regex comments1 = new Regex(@"//.*?\n",RegexOptions.Multiline);
            Regex comments2 = new Regex(@"\s*/\*.*? \*/\s*",RegexOptions.Singleline);
    
            string all_text = File.ReadAllText(filename);
            string afterRemoval = comments1.Replace(all_text, " ");
            afterRemoval = comments2.Replace(afterRemoval, " ");
    
            return afterRemoval;
    
        }
    
    }
    }