Пример #1
0
        // Initializes and runs Lexical Analysis module
        public static void StartLexicalAnalyzer(string[] arguments)
        {
            ValidateArguments(arguments);
            CommonTools.SetupOutputs(new string[] { GlobalConfiguration.LexicalAnalyzerOutputPath });
            CommonTools.ClearOutputDisplays();

            LexicalAnalyzer lexicalAnalyzer = new LexicalAnalyzer(arguments[0]);

            CommonTools.DisplayHeader = lexicalAnalyzer.DisplayTokenHeader;
            lexicalAnalyzer.DisplayTokenHeader();

            while (lexicalAnalyzer.Token != Token.EndOfFileToken)
            {
                lexicalAnalyzer.GetNextToken();
                lexicalAnalyzer.DisplayCurrentToken();
            }
            CommonTools.PromptProgramExit();
        }
        // InputStatement -> CharacterInToken RightShiftOperatorToken IdentifierToken InputEnd
        private bool ProcessInputStatement()
        {
            if (!MatchToken(Token.CharacterInToken))
            {
                return(false);
            }
            if (!MatchToken(Token.RightShiftOperatorToken))
            {
                return(false);
            }

            VariableNode node = (VariableNode)SymbolTable.LookupNode(LexicalAnaylzer.Lexeme);

            if (node == null)
            {
                CommonTools.PromptProgramErrorExit($"ERROR: Line {LexicalAnaylzer.LineNumber} Use of " +
                                                   $"undeclared variable \"{LexicalAnaylzer.Lexeme}\"");
                return(false);
            }
            if (node.Type == Token.CharToken)
            {
                OutputThreeAddressCode($"\t_RDC {GetThreeAddressCodeName(LexicalAnaylzer.Lexeme)}");
            }
            else if (node.Type == Token.IntToken)
            {
                OutputThreeAddressCode($"\t_RDI {GetThreeAddressCodeName(LexicalAnaylzer.Lexeme)}");
            }
            else
            {
                OutputThreeAddressCode($"\t_RDF {GetThreeAddressCodeName(LexicalAnaylzer.Lexeme)}");
            }

            if (!MatchToken(Token.IdentifierToken))
            {
                return(false);
            }
            if (!ProcessInputEnd())
            {
                return(false);
            }
            return(true);
        }
Пример #3
0
        // Initializes and runs Three Address Code module
        public static void StartThreeAddressCode(string[] arguments)
        {
            CommonTools.ThreeAddressCodeRun = true;
            ValidateArguments(arguments);
            CommonTools.SetupOutputs
                (new string[] { GlobalConfiguration.ThreeAddressCodeOutputPath,
                                AppendFileExtension(arguments[0], "tac") });

            CommonTools.ClearOutputDisplays();

            LexicalAnalyzer lexicalAnalyzer = new LexicalAnalyzer(arguments[0]);

            lexicalAnalyzer.GetNextToken();

            SymbolTable symbolTable = new SymbolTable();
            Parser      parser      = new Parser(lexicalAnalyzer, symbolTable);

            parser.Start();

            CommonTools.PromptProgramExit();
        }
Пример #4
0
        // Initializes and runs Parser module
        public static void StartParser(string[] arguments)
        {
            ValidateArguments(arguments);
            CommonTools.SetupOutputs(new string[] { GlobalConfiguration.ParserOutputPath });
            CommonTools.ClearOutputDisplays();

            LexicalAnalyzer lexicalAnalyzer = new LexicalAnalyzer(arguments[0]);

            lexicalAnalyzer.GetNextToken();

            SymbolTable symbolTable = new SymbolTable();
            Parser      parser      = new Parser(lexicalAnalyzer, symbolTable);

            if (parser.Start())
            {
                symbolTable.OutputSymbolTable(1);
            }

            CommonTools.WriteOutput($"Completed processing {Path.GetFileName(arguments[0])}");
            CommonTools.PromptProgramExit();
        }
 // Displays expected tokens error to appropriate displays
 private void DisplayExpectedTokensError(string expectedToken)
 {
     CommonTools.PromptProgramErrorExit($"ERROR: Line {LexicalAnaylzer.LineNumber} Expected token " +
                                        $"\"{expectedToken}\" - Received token \"{LexicalAnaylzer.Token}\"");
 }
        // Returns the three address code variable name of the given lexeme
        private string GetThreeAddressCodeName(string lexeme)
        {
            decimal n;

            if (decimal.TryParse(lexeme, out n))
            {
                string temporaryVariable;
                int    x;
                if (int.TryParse(lexeme, out x))
                {
                    temporaryVariable = GetTemporaryVariableName(Token.IntToken);
                }
                else
                {
                    temporaryVariable = GetTemporaryVariableName(Token.FloatToken);
                }

                OutputThreeAddressCode($"\t{temporaryVariable} = {SignOperation}{lexeme}");
                SignOperation = string.Empty;
                return(temporaryVariable);
            }

            Node node = SymbolTable.LookupNode(lexeme);

            if (node is VariableNode)
            {
                if (node.Depth == GlobalConfiguration.BaseDepth)
                {
                    return($"_{lexeme}");
                }
                else
                {
                    return($"_BP" + ((VariableNode)node).Offset.ToString("+0;-#"));
                }
            }
            else if (node is ConstantNode)
            {
                string temporaryVariable;
                if ((((ConstantNode)node).Value != null))
                {
                    temporaryVariable = GetTemporaryVariableName(Token.IntToken);
                }
                else
                {
                    temporaryVariable = GetTemporaryVariableName(Token.FloatToken);
                }

                string outputvalue = (((ConstantNode)node).Value ?? ((ConstantNode)node).ValueReal).ToString();
                OutputThreeAddressCode($"\t{temporaryVariable} = {SignOperation}{outputvalue}");
                SignOperation = string.Empty;
                return(temporaryVariable);
            }
            else if (node is FunctionNode)
            {
                CommonTools.PromptProgramErrorExit($"ERROR: Line {LexicalAnaylzer.LineNumber} Invalid use function {node.Lexeme}");
                return(string.Empty);
            }
            else
            {
                return(lexeme);
            }
        }
 // Writes given array of outputs to screen at index columns
 public void OutputAssembly(string[] outputs)
 {
     CommonTools.WriteOutput(string.Format(OutputFormat, outputs));
 }
Пример #8
0
 // Initializes and runs Symbol Table module
 public static void StartSymbolTable()
 {
     CommonTools.SetupOutputs(new string[] { GlobalConfiguration.SymbolTableOutputPath });
     CommonTools.ClearOutputDisplays();
     CommonTools.PromptProgramExit();
 }