Пример #1
0
        public LexicalAnalyzer(LangProcessor processor, Logger logger)
        {
            this.processor = processor;
            this.logger    = logger;

            lexTable = new LStack <lexChain>();

            this.errors     = 0;
            this.inputChain = "";
        }
Пример #2
0
        public SyntaxAnalyzer(LangProcessor processor, Logger logger)
        {
            this.processor = processor;
            this.logger    = logger;

            this.lexTable    = new LStack <lexChain>();
            this.tmpLexTable = new LStack <lexChain>();

            this.identifierStack = new LStack <string>();

            this.arifmeticStack = new LStack <string>();
            this.labelStack     = new LStack <string>();
            this.logicalStack   = new LStack <string>();

            this.arifmeticVariables = new List <string>();
            this.labelVariables     = new List <string>();
            this.logicVariables     = new List <string>();

            this.minus = new LStack <string>();

            this.compiledProgram = "";
            this.errors          = 0;
        }
Пример #3
0
 public GlobalStack()
 {
     this.data = new LStack <ActionValue>();
 }
Пример #4
0
        public void parse(LStack <lexChain> lexTable)
        {
            logger.Log("Syntax analyzer started...");
            this.processor.syntax.GlobalStack.init();
            this.compiledProgram = "";
            this.lexTable        = lexTable;
            this.tmpLexTable.Clear(); this.identifierStack.Clear();
            this.arifmeticStack.Clear(); this.arifmeticVariables.Clear();
            this.logicalStack.Clear();  this.logicVariables.Clear();
            this.labelStack.Clear(); this.labelVariables.Clear();
            this.minus.Clear();
            int         action = 0;
            ActionValue glStackTop;
            int         stackSymbol;

            while ((this.processor.syntax.GlobalStack.Size != 0) && (this.lexTable.Count != 0) && (action != Actions.UNDEFINED_ACTION))
            {
                glStackTop = this.processor.syntax.GlobalStack.pop();
                if (glStackTop.i != Actions.SYNTAX_ACTION)
                {
                    stackSymbol = glStackTop.i;
                    if (stackSymbol == Actions.SYNTAX_SYMBOL)
                    {
                        stackSymbol = glStackTop.val;
                    }
                    action = this.processor.syntax.SyntaxTable[stackSymbol, this.lexTable[0].type];
                    if (action != -1)
                    {
                        this.doAction(action);
                    }
                }
                else
                {
                    this.translateActionSymbol(glStackTop.val);
                }
                if ((this.processor.syntax.GlobalStack.Size == 1) && (this.lexTable.Count == 0))
                {
                    this.translateActionSymbol(this.processor.syntax.GlobalStack.bottom().val);
                    this.processor.syntax.GlobalStack.pop();
                }
            }
            string[] spl = this.compiledProgram.Split(new string[] { "VAR" }, StringSplitOptions.None);
            if (this.labelVariables.Count > 0)
            {
                spl[0] = "LABEL ";
                for (var i = 0; i < this.labelVariables.Count - 1; i++)
                {
                    spl[0] += "\n" + this.labelVariables[i] + ",";
                }
                spl[0] += "\n" + this.labelVariables[this.labelVariables.Count - 1] + ";\nVAR";
            }
            else
            {
                spl[0] = "VAR";
            }
            if (this.logicVariables.Count > 0)
            {
                for (int i = 0; i < this.logicVariables.Count - 1; i++)
                {
                    spl[0] += "\n" + this.logicVariables[i] + ",";
                }
                spl[0] += "\n" + this.logicVariables[this.logicVariables.Count - 1] + " :boolean;\n";
            }
            for (int i = 0; i < this.arifmeticVariables.Count; i++)
            {
                spl[0] += "\n" + this.arifmeticVariables[i] + ",";
            }
            if (spl.Length > 1)
            {
                this.compiledProgram = spl[0] + spl[1];
            }
            if (action == Actions.UNDEFINED_ACTION)
            {
                logger.Log("\n^Error: unexpected symbol on the " + this.lexTable[0].l + " line:" + this.lexTable[0].val + " \n");
                this.errors++;
            }
            else
            if ((this.processor.syntax.GlobalStack.Size != 0) && (this.lexTable.Count == 0))
            {
                logger.Log("\n^Error: unexpected end of the file\n");
                this.errors++;
            }
            else
            if ((this.processor.syntax.GlobalStack.Size == 0) && (this.lexTable.Count != 0))
            {
                logger.Log("\n^Warning: there code after end.'\n");
            }
            else
            {
                logger.Log("Translation complete!\n");
            }
        }