Guest User

Untitled

a guest
Jan 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. // Declarations = Declaration*
  2.     private void parseDeclarations() {
  3.         while (currentToken == Token.INT || currentToken == Token.BOOLEAN)
  4.             parseDeclaration();
  5.     }
  6.  
  7.     // Declaration = Type Identifiers ";"
  8.     private void parseDeclaration() {
  9.         parseType();
  10.         parseIdentifiers();
  11.         accept(Token.SEMICOLON);
  12.     }
  13.  
  14.     // Identifiers = Identifier ("," Identifier)*
  15.     private void parseIdentifiers() {
  16.         parseIdentifier();
  17.         while (currentToken.kind == Token.SEMICOLOR){
  18.         accept(Token.COMA);
  19.         parseIdentifier();
  20.         }
  21.     }
  22.  
  23.     // Type = "int" | "boolean"
  24.     private void parseType() {
  25.         if (currentToken.kind == Token.INT)
  26.             accept("int");
  27.         else if (currentToken.kind == Token.BOOLEAN)
  28.             accept ("boolean");
  29.         else new Error("Syntax error: A suitable message.", currentToken.line);
  30.     }
  31.  
  32.     // Statements = Statement*
  33.     private void parseStatements() {
  34.         while (currentToken.kind == Token.SEMICOLON || currentToken.kind ==  Token.LBRACE || currentToken.kind == Token.IF || currentToken.kind == Token.WHILE)
  35.             parseStatement();
  36.     }
  37.  
  38.     // Statement = ";" | Block | Assignment | IfStatement | WhileStatement
  39.     private void parseStatement() {
  40.         if (currentToken.kind == Token.SEMICOLON)
  41.             expectIt();
  42.         else if (currentToken.kind == Token.LBRACE) {
  43.             parseBlock();
  44.         }
  45.         else if (currentToken.kind == Token.IDENTIFIER) {
  46.             parseAssignment();
  47.         }
  48.         else if (currentToken.kind == Token.IF) {
  49.             parseIfStatement();
  50.         }
  51.         else if (currentToken.kind == Token.WHILE) {
  52.             parseWhileStatement();
  53.         }
  54.         else new Error("Expeting ;, left brace, etc.", currentToken.line);
  55.     }
  56.  
  57.  
  58. private void parseBlock() {
  59.         accept (Token.LBRACE);
  60.         parseStatements();
  61.         accept (Token.RBRACE);
  62.     }
  63.  
  64.     // Assignment = Identifier "=" Expression ";"
  65.     /*
  66.  
  67.      */
  68.     private void parseAssignment() {
  69.          parseIdentifier();
  70.          accept (Token.ASSIGN);
  71.          parseExpression();
  72.          accept (Token.SEMICOLON);
  73.     }
Add Comment
Please, Sign In to add comment