using // Echoes the original input line. Console.WriteLine(“INPUT: {0}”,

using System;using System.Collections.Generic;using System.IO;namespace CMinusCompiler{    class Program    {        // Designated keywords in C-        private static string keywords = new string        {            “float”,            “int”,            “else”,            “if”,            “return”,            “void”,            “while”        };        // Designated relational operators in C-        private static string relational = new string        {            “+”,            “-“,            “*”,            “/”,            “<=",            "<",            ">=”,            “>”,            “==”,            “!=”,            “=”,        };        // Designated deliminators in C-        private static string delims = new string        {            “;”,            “,”,            “(“,            “)”,            “”,            “”,            “{“,            “}”,            “.”,        };        // Designated deliminators for numbers in C-        private static string numDelims = new string        {            “;”,            “,”,            “(“,            “)”,            “”,            “”,            “{“,            “}”,        };        // Designated errors in C-        private static string errors = new string        {            “!”,            “@”,            “_”,        };        // Table where the list of symbols will be stored.        private static List symbolTable = new List();        // Comment count to check for nested comments.        private static int commentCount = 0;        // Main entry into the application.        // Expected arguments:        // Argument 0: Path to the file that will be processed.        static void Main(string args)        {            // Checks to make sure that there are a valid number of arguments.            if (args.Length < 1)            {                Console.WriteLine("The path to the file that will be processed must be the first argument.");                Console.ReadKey();                return;            }            // Checks to make sure that the argument is a valid file path.            if (string.IsNullOrWhiteSpace(args0) || !File.Exists(args0))            {                Console.WriteLine("Invalid file path detected.");                Console.ReadKey();                return;            }            // Calls the lexical analyzer method.            Lexical(args0);            // Pauses execution to read output.            Console.WriteLine("Press any key to continue...");            Console.ReadKey();        }        // Lexical analyzer method that takes in the path to the file.        private static void Lexical(string path)        {            // Reads the entire input file and converts into a string array where each            // item in the array represents a line of the file.            string lines = File.ReadAllLines(path);            // Temporary container for the line of code that is being processed.            string code = string.Empty;            // Cycles through each line for processing.            foreach (string line in lines)            {                // Checks for an empty line and ignores it if detected.                if (string.IsNullOrWhiteSpace(line.Trim())) continue;                // Echoes the original input line.                Console.WriteLine("INPUT: {0}", line);                // Removes any comments that are detected.                code = RemoveComments(line);                // Cycles through the rest of the line of code to check for keywords, IDs, numbers,                 // delimiters, relational operators, and errors.                while (code.Length > 0)                {                    code = CheckForKeyword(code);                    code = CheckForID(code);                    code = CheckForRelational(code);                    code = CheckForNum(code);                    code = CheckForDelimiters(code);                    code = CheckForError(code);                }            }        }        // Removes the comments from the code.        private static string RemoveComments(string code)        {            // String container for the uncommented line of code.            string uncommentedLine = string.Empty;            // Checks for single line comments and removes them.            if (code.Contains(“//”))            {                code = code.Substring(0, code.IndexOf(“//”));            }            // Checks for multi-line comments and removes them.            for (int i = 0; i < code.Length; i++)            {                // Detects the start of the comment.                if (i < code.Length - 1 && string.Compare(code.Substring(i, 2), "/*") == 0)                {                    commentCount++;                    i++;                }                // Detects the end of the multi-line comment.                else if (i < code.Length - 1 && string.Compare(code.Substring(i, 2), "*/") == 0 && commentCount > 0)                {                    commentCount–;                    i++;                }                // Detects any uncommented characters and keeps track of them.                else if (commentCount == 0)                {                    uncommentedLine += codei;                }            }            // Returns the uncommented code, if any.            return uncommentedLine.Trim();        }        // Checks for a keyword at the starting position.        // If detected, it will display it and remove it.        private static string CheckForKeyword(string code)        {            // Cycles through each available keyword to see if the start of the code contains it.            foreach (string keyword in keywords)            {                // Checks to make sure that there are no overrun issues.                if (code.Length < keyword.Length) continue;                // If a match is found, it will be reported and removed from the code.                if (string.Compare(code.Substring(0, keyword.Length), keyword) == 0)                {                    Console.WriteLine("Keyword: {0}", keyword);                    // Recursively checks for more keywords before returning to the caller.                    return CheckForKeyword(code.Substring(keyword.Length).Trim());                }            }            return code;        }        // Check for ID        private static string CheckForID(string code)        {            // Integer container used to keep track of the current index.            int idx = 0;            // String container used to keep track of the name of the ID.            string id = string.Empty;            // Cycles through until a non-letter character is detected and appends it to the ID.            while (idx < code.Length && char.IsLetter(codeidx))            {                id += codeidx++;            }            // If an ID is detected, report it, add it to the symbol table, and remove it from the code.            if (id.Length > 0)            {                Console.WriteLine(“ID: {0}”, id);                if (!symbolTable.Contains(id))                {                    symbolTable.Add(id);                }                code = code.Substring(id.Length).Trim();            }            return code;        }        // Checks for a number.        private static string CheckForNum(string code)        {            // Integer container used to keep track of the current index.            int idx = 0;            // String container used to keep track of the value of the number.            string num = string.Empty;            // Cycles through until a non-digit character is detected and appends it to the number.            // Also allows for periods since floats are allowed.            while (idx < code.Length && (char.IsDigit(codeidx) || codeidx == '.'))            {                num += codeidx++;            }            // If a number is detected, report it and remove it from the code.            if (num.Length > 0)            {                Console.WriteLine(“NUM: {0}”, num);                return code.Substring(num.Length).Trim();            }            return code;        }        // Checks for a relational operator.        private static string CheckForRelational(string code)        {            // Cycles through each valid operator.            foreach (string op in relational)            {                // Checks to make sure there is enough length on the code to prevent overrun issues.                if (code.Length < op.Length) continue;                // If an operator is detected, report it and remove it from the code.                if (string.Compare(code.Substring(0, op.Length), op) == 0)                {                    Console.WriteLine("{0}", op);                    return code.Substring(op.Length).Trim();                }            }            return code;        }        // Checks for delimiters.        private static string CheckForDelimiters(string code)        {            // Cycles through each valid delimiter.            foreach (string delim in delims)            {                // Checks to make sure there is enough length on the code to prevent overrun issues.                if (code.Length < delim.Length) continue;                // If a delimter is detected, report it and remove it from the code.                if (string.Compare(code.Substring(0, delim.Length), delim) == 0)                {                    Console.WriteLine("{0}", delim);                    return code.Substring(delim.Length).Trim();                }            }            return code;        }        // Checks for errors.        private static string CheckForError(string code)        {            // Cycles through each error string.            foreach (string err in errors)            {                // Checks to make sure there is enough length on the code to prevent overrun issues.                if (code.Length < err.Length) continue;                // If an error is detected, report it and remove it from the code.                if (string.Compare(code.Substring(0, err.Length), err) == 0)                {                    // String container to hold the erroneous string.                    string error = err;                    // Integer container representing the current index that is being checked.                    int idx = code.IndexOf(err) + err.Length;                    // Flag that is set to true if the error being checked is a number or an ID.                    bool isNum = code.Length > idx && char.IsDigit(codeidx);                    // Cycles through until a non-letter or non-number character is detected                    // depending on whether isNum is set or not.                    while (idx < code.Length && ((!isNum && char.IsLetter(codeidx)) ||                        (isNum && (char.IsDigit(codeidx) || codeidx == '.'))))                    {                        error += codeidx++;                    }                    // If an error is detected, report it and remove it from the code.                    if (error.Length > 0)                    {                        Console.WriteLine(“Error: {0}”, error);                        return code.Substring(error.Length).Trim();                    }                }            }            return code;        }    }}some more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhhome more letters for nooooooooooohhhhhhhhhh

x

Hi!
I'm Roxanne!

Would you like to get a custom essay? How about receiving a customized one?

Check it out