/******************************************************************** *** FUNCTION : Validate Literal Function *** *** DESCRIPTION : This function validates a literal. *** *** INPUT ARGS : LiteralsLinkedList literalsLinkedList, *** *** string literalToBeValidated, string literal *** *** OUTPUT ARGS : This function has zero output arguments. *** *** IN/OUT ARGS : This function has zero input/output arguments. *** *** RETURN : This function returns nothing. *** *********************************************************************/ private void ValidateLiteral(LiteralsLinkedList literalsLinkedList, string literalToBeValidated, string literal) { int hexchar; int address = 1; hexchar = literal.IndexOf("="); if (literal[hexchar + 1] == 'C' || literal[hexchar + 1] == 'c') { // Process character literal ProcessCharacterLiteral processCharacterLiteral = new ProcessCharacterLiteral(); processCharacterLiteral.Process(literalsLinkedList, literalToBeValidated, literal, address, processedLiterals); } else if (literal[hexchar + 1] == 'X' || literal[hexchar + 1] == 'x') { // Process hexidecimal literal if (!processedLiterals.Contains(literalToBeValidated.ToUpper()) && !processedLiterals.Contains(literalToBeValidated.ToLower())) { ProcessHexidecimalLiteral processHexidecimalLiteral = new ProcessHexidecimalLiteral(); processHexidecimalLiteral.Process(literalsLinkedList, literalToBeValidated, literal, address, processedLiterals); } } else { // Process invalid literal by adding it to literal errors list literalsLinkedList.LiteralErrors.Add("Error: The literal '" + literal + "' contains an invalid second character (must be C or X)."); } }
/******************************************************************** *** FUNCTION : Process Function *** *** DESCRIPTION : This function processes a hexidecimal literal *** *** and adds it to either the literal errors list *** *** or to the list of valid literals. *** *** INPUT ARGS : LiteralsLinkedList literalsLinkedList, *** *** string literalToBeValidated, string literal, *** *** int address, List<string> processedLiterals *** *** OUTPUT ARGS : This function has zero output arguments. *** *** IN/OUT ARGS : This function has zero input/output arguments. *** *** RETURN : This function returns nothing. *** *********************************************************************/ public void Process(LiteralsLinkedList literalsLinkedList, string literalToBeValidated, string literal, int address, List <string> processedLiterals) { string final = ""; Regex rg = new Regex("[0-9A-Fa-f]+"); if (rg.IsMatch(literalToBeValidated)) { if (literalToBeValidated[0] <= '7' && literalToBeValidated[0] >= '0') { if (literalToBeValidated.Length % 2 != 0) { final = "0"; foreach (char ch in literalToBeValidated) { final += ch; } } else { final = literalToBeValidated; } } else if (literalToBeValidated[0] == '8' || literalToBeValidated[0] == '9' || literalToBeValidated[0] >= 'A' || literalToBeValidated[0] <= 'B' || literalToBeValidated[0] == 'C' || literalToBeValidated[0] == 'D' || literalToBeValidated[0] == 'E' || literalToBeValidated[0] == 'F') { if (literalToBeValidated.Length % 2 != 0) { final = "F"; foreach (char ch in literalToBeValidated) { final += ch; } } else { final = literalToBeValidated; } } else { literalsLinkedList.LiteralErrors.Add("Error: " + literal + " may only contain hexadecimal characters (0 - F)"); } if (!processedLiterals.Contains(literalToBeValidated)) { processedLiterals.Add(literalToBeValidated); LiteralNode node = new LiteralNode(literal, final, (final.Count() / 2), address); literalsLinkedList.Add(node); address++; } else { literalsLinkedList.LiteralErrors.Add("Error: Literal " + literal + " is a duplicate literal"); } } else { literalsLinkedList.LiteralErrors.Add("Error: " + literal + " may only contain hexadecimal characters (0 - F)"); } }
/******************************************************************** *** FUNCTION : Validate Function *** *** DESCRIPTION : This function parses and validates a literal. *** *** INPUT ARGS : LiteralsLinkedList literalsLinkedList, *** *** List<string> literals *** *** OUTPUT ARGS : This function has zero output arguments. *** *** IN/OUT ARGS : This function has zero input/output arguments. *** *** RETURN : This function returns nothing. *** *********************************************************************/ public void Validate(LiteralsLinkedList literalsLinkedList, List <string> literals) { foreach (string literal in literals) { // Get the literal expression string literalToBeValidated = ParseLiteral(literalsLinkedList, literal); // Validate literal if (literalToBeValidated != "") { ValidateLiteral(literalsLinkedList, literalToBeValidated, literal); } } }
/******************************************************************** *** FUNCTION : Parse Literal Function *** *** DESCRIPTION : This function parses a literal so that it can *** *** be easily validated. *** *** INPUT ARGS : LiteralsLinkedList literalsLinkedList, *** *** string literal *** *** OUTPUT ARGS : This function has zero output arguments. *** *** IN/OUT ARGS : This function has zero input/output arguments. *** *** RETURN : This function returns literalToValidate as a *** *** string. *** *********************************************************************/ private string ParseLiteral(LiteralsLinkedList literalsLinkedList, string literal) { string literalToValidate = ""; Match match = Regex.Match(literal, @"'([^']*)"); if (match.Success) { literalToValidate = match.Groups[1].Value; } else { literalsLinkedList.LiteralErrors.Add("Error: The literal " + literal + " must be separated by single quotations"); } return(literalToValidate); }
/******************************************************************** *** FUNCTION : Process Function *** *** DESCRIPTION : This function processes a character literal *** *** and adds it to either the literal errors list *** *** or to the list of valid literals. *** *** INPUT ARGS : LiteralsLinkedList literalsLinkedList, *** *** string literalToBeValidated, string literal, *** *** int address, List<string> processedLiterals *** *** OUTPUT ARGS : This function has zero output arguments. *** *** IN/OUT ARGS : This function has zero input/output arguments. *** *** RETURN : This function returns nothing. *** *********************************************************************/ public void Process(LiteralsLinkedList literalsLinkedList, string literalToBeValidated, string literal, int address, List <string> processedLiterals) { string final = ""; foreach (char character in literalToBeValidated) { int value = Convert.ToInt32(character); string hexOutput = String.Format("{0:X}", value); final += hexOutput; } if (!processedLiterals.Contains(literalToBeValidated)) { processedLiterals.Add(literalToBeValidated); LiteralNode node = new LiteralNode(literal, final, literalToBeValidated.Count(), address); literalsLinkedList.Add(node); address++; } }
/******************************************************************** *** FUNCTION : Process File Function *** *** DESCRIPTION : This funciton gets the expression file name and *** *** reads in the contents of the expression file *** *** into an array of strings. It then separates the *** *** array of strings into literals and expressions. *** *** It then validates the expressions and validates *** *** the literals. *** *** INPUT ARGS : ExpressionsLinkedList expressionsLinkedList, *** *** LiteralsLinkedList literalsLinkedList, *** *** BinarySearchTree symbolTable, string arg = "" *** *** OUTPUT ARGS : This function has zero output arguments. *** *** IN/OUT ARGS : This function has zero input/output arguments. *** *** RETURN : This function returns nothing. *** *********************************************************************/ public void ProcessFile(ExpressionsLinkedList expressionsLinkedList, LiteralsLinkedList literalsLinkedList, BinarySearchTree symbolTable, string arg = "") { // Get file name if it exists string expressionFileName = GetExpressionFile(arg); // Read in the expression file and save it into a string array string[] fileContents = GetExpressionFileContents(expressionFileName); // Separate the file contents into literals and expressions SeparateFileContents(fileContents); // Validate the expressions ExpressionValidator expressionValidator = new ExpressionValidator(); expressionValidator.ValidateExpressions(expressionsLinkedList, symbolTable, expressions); // Validate the literals LiteralValidator literalValidator = new LiteralValidator(); literalValidator.Validate(literalsLinkedList, literals); }
static void Main(string[] args) { BinarySearchTree binarySearchTree = new BinarySearchTree(); PopulateSymbolTable symbolTable = new PopulateSymbolTable(); PopulateExpressionTable expressionTable = new PopulateExpressionTable(); ExpressionsLinkedList expressionsLinkedList = new ExpressionsLinkedList(); LiteralsLinkedList literalsLinkedList = new LiteralsLinkedList(); // 1. Read from SYMS.DAT file and populate the symbol table binarySearchTree = symbolTable.CreateBinarySearchTreeFromDataFile(); // 2. Create expressions if (args.Length > 0) { expressionTable.ProcessFile(expressionsLinkedList, literalsLinkedList, binarySearchTree, args[0]); } else { expressionTable.ProcessFile(expressionsLinkedList, literalsLinkedList, binarySearchTree); } Console.ReadKey(); // 3. Display expression data expressionsLinkedList.Display(); Console.ReadKey(); // 4. Display literal error data literalsLinkedList.DisplayErrors(); Console.ReadKey(); // 5. Display literal data literalsLinkedList.Display(); Console.ReadKey(); }