예제 #1
0
 static AbstractNode factor() {
   AbstractNode node = null;
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   if (nexttoken.getName().equals("(")) {
     insymbol();
     node = expression();
     if (nexttoken.getName().equals(")")) {
       insymbol();
     } else {
       error("')' expected", nexttoken.getLine(), nexttoken.getColumn());
     }
   } else if (nexttoken.getType().equals("Ident")) {
     node = new IdentNode(nexttoken.getName(), line, column);
     outStr(nexttoken.getName());
     Yytoken ident = nexttoken;
     insymbol();
     OperatorNode selectorNode = selector(ident);
     if (selectorNode != null) {
       // selectorNode.setLeft(node);
       node = new ContNode(selectorNode, line, column);
     } else {
       node = new ContNode(node, line, column);
     }
   } else if (nexttoken.getType().equals("Integer")) {
     outInt(nexttoken.getName());
     node = new IntegerNode(nexttoken.getName(), line, column);
     insymbol();
   } else if (nexttoken.getName().equals("READ")) {
     node = read();
   } else if (nexttoken.getName().charAt(0) == '"') {
     node = new ContNode(string(), line, column);
   }
   return node;
 }
예제 #2
0
  static AbstractNode module() {
    AbstractNode ident = null, decl = null, statementSeq = null;
    int line = 0, column = 0;
    String identName = "";
    if (nexttoken.getName().equals("MODULE")) {
      outStr("MODULE");
      insymbol();
      if (nexttoken.getType().equals("Ident")) {
        outStr(identName = nexttoken.getName());
        ident = new IdentNode(nexttoken.getName(), nexttoken.getLine(), nexttoken.getColumn());
        insymbol();
        if (nexttoken.getName().equals(";")) {
          outStr(";");
          insymbol();
          decl = declarations();
          if (nexttoken.getName().equals("BEGIN")) {
            outStr("BEGIN MODULE");
            insymbol();

            statementSeq = statementSequence();
            if (nexttoken.getName().equals("END")) {
              outStr("END MODULE");
              insymbol();
              if (nexttoken.getType().equals("Ident") && identName.equals(nexttoken.getName())) {
                outStr(nexttoken.getName());
                insymbol();

                if (nexttoken.getName().equals(".")) {
                  outStr(".");
                  insymbol();
                } else {
                  error("'.' expected", nexttoken.getLine(), nexttoken.getColumn());
                }
              } else {
                error("'Ident' expected", nexttoken.getLine(), nexttoken.getColumn());
              }
            } else {
              error("'END' expected", nexttoken.getLine(), nexttoken.getColumn());
            }
          } else {
            error("'BEGIN' expected", nexttoken.getLine(), nexttoken.getColumn());
          }
        } else {
          error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
        }
      } else {
        error("'Ident' expected", nexttoken.getLine(), nexttoken.getColumn());
      }
    }
    return new ModuleNode(ident, decl, statementSeq, line, column);
  }
예제 #3
0
 static AbstractNode statement() {
   AbstractNode statement = null;
   int line = 0, column = 0;
   if (nexttoken.getType().equals("Ident")) {
     Yytoken ident = nexttoken;
     line = nexttoken.getLine();
     column = nexttoken.getColumn();
     outStr(ident.getName());
     insymbol();
     if (nexttoken.getName().equals("(")) {
       statement = procedureCall(ident);
     } else {
       statement = assignment(ident);
     }
   } else if (nexttoken.getName().equals("IF")) {
     statement = ifStatement();
   } else if (nexttoken.getName().equals("PRINT")) {
     outStr("PRINT");
     insymbol();
     statement = new PrintNode(expression(), line, column);
   } else if (nexttoken.getName().equals("WHILE")) {
     statement = whileStatement();
   } else if (nexttoken.getName().equals("REPEAT")) {
     statement = repeatStatement();
   }
   return new StatementNode(statement, line, column);
 }
예제 #4
0
 static AbstractNode procedureHeading() {
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   AbstractNode ident = null, formalParameters = null;
   if (nexttoken.getName().equals("PROCEDURE")) {
     outStr("PROCEDURE");
     insymbol();
     if (nexttoken.getType().equals("Ident")) {
       outStr(nexttoken.getName());
       ident = new IdentNode(nexttoken.getName(), nexttoken.getLine(), nexttoken.getColumn());
       insymbol();
       if (nexttoken.getName().equals("(")) {
         outStr("(");
         insymbol();
         if (!nexttoken.getName().equals(")")) {
           formalParameters = formalParameters();
         }
       } else {
         error("Missing '('", nexttoken.getLine(), nexttoken.getColumn());
       }
       if (nexttoken.getName().equals(")")) {
         outStr(")");
         insymbol();
       } else {
         error("Missing ')'", nexttoken.getLine(), nexttoken.getColumn());
       }
     } else {
       error("Ident expected", nexttoken.getLine(), nexttoken.getColumn());
     }
   } else {
     error("'PROCEDURE' expected", nexttoken.getLine(), nexttoken.getColumn());
   }
   return new ProcedureHeadingNode(ident, formalParameters, line, column);
 }
예제 #5
0
 static AbstractNode constIdent() {
   AbstractNode node = null;
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   if (nexttoken.getType().equals("Ident")) {
     outStr(nexttoken.getName());
     node = new IdentNode(nexttoken.getName(), line, column);
     insymbol();
   } else {
     error("ConstIdent Error", nexttoken.getLine(), nexttoken.getColumn());
   }
   return node;
 }
예제 #6
0
 static AbstractNode indexExpression() {
   AbstractNode node = null;
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   if (nexttoken.getType().equals("Integer")) {
     outInt(nexttoken.getName());
     node = new IntegerNode(nexttoken.getName(), line, column);
     insymbol();
   } else {
     node = constIdent();
   }
   return node;
 }
예제 #7
0
 static OperatorNode selector(Yytoken ident) {
   AbstractNode node = null;
   AbstractNode result = null;
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   while (nexttoken.getName().equals(".") || nexttoken.getName().equals("[")) {
     if (nexttoken.getName().equals(".")) {
       insymbol();
       if (nexttoken.getType().equals("Ident")) {
         outStr("." + nexttoken.getName());
         if (node == null) {
           node =
               new OperatorNode(
                   ".",
                   new IdentNode(ident.getName(), ident.getLine(), ident.getColumn()),
                   new IdentNode(nexttoken.getName(), line, column),
                   line,
                   column);
         } else {
           node =
               (new OperatorNode(
                   ".", node, new IdentNode(nexttoken.getName(), line, column), line, column));
         }
         insymbol();
       } else {
         error("identifier expected", nexttoken.getLine(), nexttoken.getColumn());
       }
     } else if (nexttoken.getName().equals("[")) {
       insymbol();
       if (node == null) {
         node =
             new OperatorNode(
                 "[",
                 new IdentNode(ident.getName(), ident.getLine(), ident.getColumn()),
                 expression(),
                 line,
                 column);
       } else {
         node = new OperatorNode("[", node, expression(), line, column);
       }
       if (nexttoken.getName().equals("]")) {
         insymbol();
       } else {
         error("']' expected", nexttoken.getLine(), nexttoken.getColumn());
       }
     }
   }
   return ((OperatorNode) node);
 }
예제 #8
0
 static AbstractNode type() {
   AbstractNode node = null;
   if (nexttoken.getType().equals("Ident")) {
     node = new IdentNode(nexttoken.getName(), nexttoken.getLine(), nexttoken.getColumn());
     outStr(nexttoken.getName());
     insymbol();
   } else if (nexttoken.getName().equals("ARRAY")) {
     node = arrayType();
   } else if (nexttoken.getName().equals("RECORD")) {
     node = recordType();
   } else {
     error("Type Error", nexttoken.getLine(), nexttoken.getColumn());
   }
   return node;
 }
예제 #9
0
 static AbstractNode fieldList() {
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   AbstractNode identList = null, type = null;
   if (nexttoken.getType().equals("Ident")) {
     identList = identList();
     if (nexttoken.getName().equals(":")) {
       insymbol();
       type = type();
     } else {
       error("':' expected", nexttoken.getLine(), nexttoken.getColumn());
     }
   } else {
     error("FieldList Error", nexttoken.getLine(), nexttoken.getColumn());
   }
   return new FieldListNode(identList, type, line, column);
 }
예제 #10
0
 static AbstractNode procedureDeclaration() {
   AbstractNode heading = null, body = null, ident = null;
   int line = nexttoken.getLine(), column = nexttoken.getColumn();
   heading = procedureHeading();
   if (nexttoken.getName().equals(";")) {
     outStr(";");
     insymbol();
     body = procedureBody();
     if (nexttoken.getType().equals("Ident")) {
       outStr("Ident");
       ident = new IdentNode(nexttoken.getName(), nexttoken.getLine(), nexttoken.getColumn());
       insymbol();
     } else {
       error("'Ident' expected", nexttoken.getLine(), nexttoken.getColumn());
     }
   }
   return new ProcedureDeclarationNode(heading, body, ident, line, column);
 }
예제 #11
0
  static AbstractNode identList() {
    int line = nexttoken.getLine();
    int column = nexttoken.getColumn();
    List<AbstractNode> list = new LinkedList<AbstractNode>();
    if (nexttoken.getType().equals("Ident")) {
      outStr(nexttoken.getName());
      list.add(new IdentNode(nexttoken.getName(), nexttoken.getLine(), nexttoken.getColumn()));
      insymbol();
      while (nexttoken.getName().equals(",")) {
        outStr(",");
        insymbol();
        outStr(nexttoken.getName());
        list.add(new IdentNode(nexttoken.getName(), nexttoken.getLine(), nexttoken.getColumn()));
        insymbol();
      }

    } else {
      error("IdentList Error", nexttoken.getLine(), nexttoken.getColumn());
    }
    return new ListNode(list, line, column);
  }
예제 #12
0
 static AbstractNode fpSection() {
   int line = nexttoken.getLine();
   int column = nexttoken.getColumn();
   boolean isVar = false;
   AbstractNode identList = null, type = null;
   if (nexttoken.getName().equals("VAR")) {
     outStr("VAR");
     isVar = true;
     insymbol();
   }
   if (nexttoken.getType().equals("Ident")) {
     identList = identList();
   } else {
     error("'IDENT' expected", nexttoken.getLine(), nexttoken.getColumn());
   }
   if (nexttoken.getName().equals(":")) {
     outStr(":");
     insymbol();
     type = type();
   } else {
     error("':' expected", nexttoken.getLine(), nexttoken.getColumn());
   }
   return new FPSectionNode(isVar, identList, type, line, column);
 }
예제 #13
0
  static AbstractNode declarations() {
    List<AbstractNode> constNodes = new LinkedList<AbstractNode>();
    List<AbstractNode> varNodes = new LinkedList<AbstractNode>();
    List<AbstractNode> typeNodes = new LinkedList<AbstractNode>();
    List<AbstractNode> procDeclNodes = new LinkedList<AbstractNode>();
    AbstractNode identNode;
    AbstractNode expressionNode;
    AbstractNode type;
    int line = 0, column = 0;
    if (nexttoken.getName().equals("CONST")) {
      outStr("CONST");
      insymbol();
      if (nexttoken.getType().equals("Ident")) {
        outStr(nexttoken.getName());
        line = nexttoken.getLine();
        column = nexttoken.getColumn();
        identNode = new IdentNode(nexttoken.getName(), line, column);

        insymbol();
        if (nexttoken.getName().equals("=")) {
          outStr("=");
          insymbol();
          expressionNode = expression();
          if (nexttoken.getName().equals(";")) {
            outStr(";");
            insymbol();
            constNodes.add(new ConstNode(identNode, expressionNode, line, column));
            while (nexttoken.getType().equals("Ident")) {
              line = nexttoken.getLine();
              column = nexttoken.getColumn();
              identNode = new IdentNode(nexttoken.getName(), line, column);
              outStr("Ident");
              insymbol();
              if (nexttoken.getName().equals("=")) {
                outStr("=");
                insymbol();
                expressionNode = expression();
                if (nexttoken.getName().equals(";")) {
                  outStr(";");
                  constNodes.add(new ConstNode(identNode, expressionNode, line, column));
                  insymbol();
                } else {
                  error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
                }
              } else {
                error("'=' expected", nexttoken.getLine(), nexttoken.getColumn());
              }
            }
          } else {
            error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
          }

        } else {
          error("'=' expected", nexttoken.getLine(), nexttoken.getColumn());
        }
      } else {
        error("'Ident' expected", nexttoken.getLine(), nexttoken.getColumn());
      }
    }
    if (nexttoken.getName().equals("TYPE")) {

      outStr("TYPE");
      insymbol();
      if (nexttoken.getType().equals("Ident")) {
        outStr(nexttoken.getName());
        line = nexttoken.getLine();
        column = nexttoken.getColumn();
        identNode = new IdentNode(nexttoken.getName(), line, column);
        insymbol();
        if (nexttoken.getName().equals("=")) {
          outStr("=");
          insymbol();
          type = type();
          if (nexttoken.getName().equals(";")) {
            outStr(";");
            typeNodes.add(new TypeNode(identNode, type, line, column));
            insymbol();
            while (nexttoken.getType().equals("Ident")) {
              outStr(nexttoken.getName());
              line = nexttoken.getLine();
              column = nexttoken.getColumn();
              identNode = new IdentNode(nexttoken.getName(), line, column);
              insymbol();
              if (nexttoken.getName().equals("=")) {
                outStr("=");
                insymbol();
                type = type();
                if (nexttoken.getName().equals(";")) {
                  outStr(";");
                  typeNodes.add(new TypeNode(identNode, type, line, column));
                  insymbol();
                } else {
                  error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
                }
              } else {
                error("'=' expected", nexttoken.getLine(), nexttoken.getColumn());
              }
            }
          } else {
            error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
          }
        } else {
          error("'=' expected", nexttoken.getLine(), nexttoken.getColumn());
        }
      } else {
        error("'Ident' expected", nexttoken.getLine(), nexttoken.getColumn());
      }
    }
    if (nexttoken.getName().equals("VAR")) {
      outStr("VAR");
      insymbol();
      line = nexttoken.getLine();
      column = nexttoken.getColumn();
      identNode = identList();
      if (nexttoken.getName().equals(":")) {
        outStr(":");
        insymbol();
        type = type();
        varNodes.add(new VarNode(identNode, type, line, column));
        if (nexttoken.getName().equals(";")) {
          outStr(";");

          insymbol();
          while (nexttoken.getType().equals("Ident")) {
            line = nexttoken.getLine();
            column = nexttoken.getColumn();
            identNode = identList();
            if (nexttoken.getName().equals(":")) {
              outStr(":");
              insymbol();
              type = type();
              if (nexttoken.getName().equals(";")) {
                outStr(";");
                varNodes.add(new VarNode(identNode, type, line, column));
                insymbol();
              } else {
                error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
              }
            } else {
              error("'=' expected", nexttoken.getLine(), nexttoken.getColumn());
            }
          }
        } else {
          error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
        }
      } else {
        error("'=' expected", nexttoken.getLine(), nexttoken.getColumn());
      }
    }
    // Procedure ist first token in ProcedureDeclaration -> ProcedureHeading
    // -> Procedure
    // Direkt Access because of the while Procedure
    while (nexttoken.getName().equals("PROCEDURE")) {

      procDeclNodes.add(procedureDeclaration());
      if (nexttoken.getName().equals(";")) {
        insymbol();
      } else {
        error("';' expected", nexttoken.getLine(), nexttoken.getColumn());
      }
    }
    return new DeclarationsNode(constNodes, typeNodes, varNodes, procDeclNodes, line, column);
  }