private bool Block(Tree.TreeNode parent) { var newNode = tree.Add("<block>", parent); Declarations(newNode); if (currentToken.Name == "BEGIN") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); StatementsList(newNode); if (currentToken.Name == "END") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); return(true); } else { Errors.AddToken(currentToken, locationNumber, $"'END' expected but {currentToken.Name} found", tree); return(false); } } else { Errors.AddToken(currentToken, locationNumber, $"'BEGIN' expected but {currentToken.Name} found", tree); return(false); } }
//private bool ConditionStatement(Tree.TreeNode parent) //{ // if (currentToken.Name == "IF") // { // var newNode = tree.Add("<condition-statement>", parent); // LeftCondition(newNode); // RightCondition(newNode); // if (currentToken.Name == ";") // { // newNode.AddToChildren(new Tree.TreeNode(currentToken.Name, newNode)); // } // else // { // Errors.AddToken(currentToken, locationNumber, // $"';' expected but {currentToken.Name} found", tree); // return false; // } // return true; // } // return false; //} //private bool LeftCondition(Tree.TreeNode parent) //{ // var newNode = tree.Add("<left-condition>", parent); // newNode.AddToChildren(new Tree.TreeNode(currentToken.Name, newNode)); // currentToken = GetNext(); // IncrementLocation(currentToken); // Constant(newNode); // currentToken = GetNext(); // IncrementLocation(currentToken); // if (currentToken.Name == "THEN") // { // newNode.AddToChildren(new Tree.TreeNode(currentToken.Name, newNode)); // currentToken = GetNext(); // IncrementLocation(currentToken); // DeclarationList(newNode); // } // else // { // Errors.AddToken(currentToken, locationNumber, // $"'THEN' expected but {currentToken.Name} found", tree); // return false; // } // return true; //} //private bool RightCondition(Tree.TreeNode parent) //{ // var newNode = tree.Add("<right-condition>", parent); // if (currentToken.Name == ";") // { // newNode.AddToChildren(new Tree.TreeNode("<empty>", newNode)); // } // else // { // if (currentToken.Name == "ELSE") // { // newNode.AddToChildren(new Tree.TreeNode(currentToken.Name, newNode)); // currentToken = GetNext(); // IncrementLocation(currentToken); // DeclarationList(newNode); // } // else // { // Errors.AddToken(currentToken, locationNumber, // $"'ELSE' expected but {currentToken.Name} found", tree); // return false; // } // } // return true; //} private bool Declaration(Tree.TreeNode parent) { var newNode = tree.Add("<declaration>", parent); VariableIdentifier(newNode); currentToken = GetNext(); IncrementLocation(currentToken); if (currentToken.Name == ":") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); AttributeProc(newNode); if (currentToken.Name != ";") { Errors.AddToken(currentToken, locationNumber, $"';' expected but {currentToken.Name} found", tree); return(false); } else { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); return(true); } } else { Errors.AddToken(currentToken, locationNumber, $"':' expected but {currentToken.Name} found", tree); return(false); } }
private bool ProcedureIdentifier(Tree.TreeNode parent) { var newNode = tree.Add("<procedure-identifier>", parent); currentToken = GetNext(); IncrementLocation(currentToken); return(Identifier(newNode)); }
private bool SignalProgram() { currentToken = GetNext(); if (currentToken != null) { IncrementLocation(currentToken); return(ProgramProc()); } return(true); }
private bool ConstantDeclarations(Tree.TreeNode parent) { var newNode = tree.Add("<constant-declarations>", parent); currentToken = GetNext(); IncrementLocation(currentToken); if (currentToken.Name == "CONST") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); return(ConstantDeclarationsList(newNode)); } newNode.AddToChildren(new Tree.TreeNode("<empty>", newNode)); return(true); }
private void IncrementLocation(Table.Token token) { if (token == null) { Errors.Add($"unexpected end of file", tree); } if (locationNumber.ContainsKey(token)) { locationNumber[token]++; return; } locationNumber.Add(token, 0); }
private bool StatementsList(Tree.TreeNode parent) { var newNode = tree.Add("<statements-list>", parent); currentToken = GetNext(); IncrementLocation(currentToken); //if (!ConditionStatement(newNode)) //{ newNode.AddToChildren(new Tree.TreeNode("<empty>", newNode)); //} //currentToken = GetNext(); //IncrementLocation(currentToken); return(true); }
private bool AttributeProc(Tree.TreeNode parent) { var newNode = tree.Add("<attribute>", parent); currentToken = GetNext(); IncrementLocation(currentToken); if ((currentToken.Name != "INTEGER") && (currentToken.Name != "FLOAT")) { Errors.AddToken(currentToken, locationNumber, $"'INTEGER' or 'FLOAT' expected but {currentToken.Name} found", tree); return(false); } newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); return(true); }
private bool ConstantDeclarationsList(Tree.TreeNode parent) { var newNode = tree.Add("<constant-declarations-list>", parent); currentToken = GetNext(); IncrementLocation(currentToken); if (!ConstantDeclaration(newNode)) { currentToken = GetNext(); IncrementLocation(currentToken); newNode.AddToChildren(new Tree.TreeNode("<empty>", newNode)); return(true); } while (currentToken.Name != "BEGIN") { ConstantDeclaration(newNode); } return(true); }
private bool ConstantDeclaration(Tree.TreeNode parent) { var newNode = tree.Add("<constant-declaration>", parent); ConstantIdentifier(newNode); currentToken = GetNext(); IncrementLocation(currentToken); if (currentToken.Name == "=") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); Constant(newNode); currentToken = GetNext(); IncrementLocation(currentToken); if (currentToken.Name == ";") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); return(true); } else { Errors.AddToken(currentToken, locationNumber, $"';' expected but {currentToken.Name} found", tree); return(false); } } else { Errors.AddToken(currentToken, locationNumber, $"'=' expected but {currentToken.Name} found", tree); return(false); } }
private bool ParametersList(Tree.TreeNode parent) { var newNode = tree.Add("<parameters-list>", parent); currentToken = GetNext(); IncrementLocation(currentToken); if (currentToken.Name == ";") { newNode.AddToChildren(new Tree.TreeNode("<empty>", newNode)); return(true); } if (currentToken.Name == "(") { newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); DeclarationList(newNode); if (currentToken.Name != ")") { Errors.AddToken(currentToken, locationNumber, $"')' expected but {currentToken.Name} found", tree); return(false); } newNode.AddToChildren(currentToken.Code + " " + currentToken.Name); currentToken = GetNext(); IncrementLocation(currentToken); return(true); } else { Errors.AddToken(currentToken, locationNumber, $"'(' expected but {currentToken.Name} found", tree); return(false); } }
public void AddToken(Table.Token currentToken, Dictionary <Table.Token, int> locationNumber, string text, Tree tree) { this.Add(currentToken.Locations[locationNumber[currentToken]].Line, currentToken.Locations[locationNumber[currentToken]].Column, text, currentToken.Name, tree); }