using System; using System.IO; class Tokenizer { private const int MaxLispLength = 200; private string lispName; private string inLispRecord; private bool isComment; private int lispLength; private string calcLengthStr; private int numLengthAdd; private bool parenRightYes, parenLeftYes; private string parenTempStr; private int parenTempNum; private char whichParen; private int formatStrIndex; private int count; private int stringPtr; private int tempNum; private bool flagYes; private int symbolTableSize; private string[] lispSymbols = new string[100]; private int[] symbolLen = new int[100]; public void MainProcedure(string lispFileName) { lispName = lispFileName; FileHandlingProcedure(); TokenizeLispProcedure(); CalLengthAllSymbols(); PrintSymbolTable(); } private void FileHandlingProcedure() { using (StreamReader reader = new StreamReader(lispName)) { string line; while ((line = reader.ReadLine()) != null) { AppendLispProcedure(line); } } Log("FILE-HANDLING-PROCEDURE", "COMPLETED reading LISP-FILE"); } private void AppendLispProcedure(string inLispRecord) { this.inLispRecord = inLispRecord; CalcLispLength(); if (!isComment) { if (tempNum == 0) this.inLispRecord = inLispRecord; else { string temp = this.inLispRecord.Substring(0, tempNum) + " " + inLispRecord; this.inLispRecord = temp; tempNum--; } tempNum += lispLength; } } private void CalcLispLength() { calcLengthStr = inLispRecord; lispLength = 0; numLengthAdd = 0; isComment = false; for (int i = 0; i < calcLengthStr.Length && i < MaxLispLength; i++) { if (calcLengthStr[i] == ';') isComment = true; else if (calcLengthStr[i] != ' ') { lispLength++; lispLength += numLengthAdd; numLengthAdd = 0; } else { numLengthAdd++; } } } private void TokenizeLispProcedure() { FormatLispProcedure(); stringPtr = 1; symbolTableSize = 0; flagYes = false; for (count = 1; count <= 100 && !flagYes; count++) { int index = inLispRecord.IndexOf(' ', stringPtr - 1); if (index == -1) lispSymbols[count] = inLispRecord.Substring(stringPtr - 1); else lispSymbols[count] = inLispRecord.Substring(stringPtr - 1, index - stringPtr + 2); if (string.IsNullOrWhiteSpace(lispSymbols[count])) flagYes = true; else symbolTableSize++; } Log("TOKENIZE-LISP-PROCEDURE", "COMPLETED tokenizing lisp"); } private void FormatLispProcedure() { calcLengthStr = inLispRecord; CalcLispLength(); if (inLispRecord[0] == '(' && inLispRecord[1] != ' ') { inLispRecord = "( " + inLispRecord.Substring(1); formatStrIndex += 3; lispLength++; } for (formatStrIndex = 1; formatStrIndex <= lispLength; formatStrIndex++) { parenRightYes = false; parenLeftYes = false; switch (inLispRecord[formatStrIndex - 1]) { case '(': FormatParenSpaceProcedure(); break; case ')': FormatParenSpaceProcedure(); break; } } Log("FORMAT-LISP-PROCEDURE", "COMPLETED formatting lisp string for parsing"); } private void FormatParenSpaceProcedure() { formatStrIndex--; if (inLispRecord[formatStrIndex - 1] != ' ') parenLeftYes = true; formatStrIndex += 2; if (inLispRecord[formatStrIndex - 1] != ' ') parenRightYes = true; formatStrIndex--; if (parenRightYes && parenLeftYes) FormatAddBothSpaces(); else if (parenRightYes) FormatAddRightSpace(); else if (parenLeftYes) FormatAddLeftSpace(); } private void FormatAddLeftSpace() { parenTempNum = formatStrIndex - 1; inLispRecord = inLispRecord.Substring(0, parenTempNum) + " " + inLispRecord.Substring(parenTempNum); formatStrIndex++; lispLength++; } private void FormatAddRightSpace() { parenTempNum = formatStrIndex; inLispRecord = inLispRecord.Substring(0, parenTempNum) + " " + inLispRecord.Substring(parenTempNum); formatStrIndex++; lispLength++; } private void FormatAddBothSpaces() { parenTempNum = formatStrIndex - 1; whichParen = inLispRecord[formatStrIndex - 1]; formatStrIndex++; inLispRecord = inLispRecord.Substring(0, parenTempNum) + " " + whichParen.ToString() + " " + inLispRecord.Substring(parenTempNum); formatStrIndex++; lispLength += 2; } private void CalLengthAllSymbols() { for (count = 1; count <= 100; count++) { CalcLengthSymbol(); symbolLen[count] = lispLength; } } private void CalcLengthSymbol() { lispLength = 0; bool parseHasEnded = false; for (formatStrIndex = 1; formatStrIndex <= 100 && !parseHasEnded; formatStrIndex++) { if (lispSymbols[count][formatStrIndex - 1] == ' ') parseHasEnded = true; else lispLength++; } } private void PrintSymbolTable() { for (count = 1; count <= symbolTableSize; count++) { Console.WriteLine(count); Console.WriteLine(lispSymbols[count]); Console.WriteLine(symbolLen[count]); } } private void Log(string functionName, string message) { // Simulate logging Console.WriteLine($"LOG: {functionName} - {message}"); } } class Program { static void Main() { Tokenizer tokenizer = new Tokenizer(); tokenizer.MainProcedure("path_to_lisp_file.lsp"); } }