Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Declarations = Declaration*
- private void parseDeclarations() {
- while (currentToken == Token.INT || currentToken == Token.BOOLEAN)
- parseDeclaration();
- }
- // Declaration = Type Identifiers ";"
- private void parseDeclaration() {
- parseType();
- parseIdentifiers();
- accept(Token.SEMICOLON);
- }
- // Identifiers = Identifier ("," Identifier)*
- private void parseIdentifiers() {
- parseIdentifier();
- while (currentToken.kind == Token.SEMICOLOR){
- accept(Token.COMA);
- parseIdentifier();
- }
- }
- // Type = "int" | "boolean"
- private void parseType() {
- if (currentToken.kind == Token.INT)
- accept("int");
- else if (currentToken.kind == Token.BOOLEAN)
- accept ("boolean");
- else new Error("Syntax error: A suitable message.", currentToken.line);
- }
- // Statements = Statement*
- private void parseStatements() {
- while (currentToken.kind == Token.SEMICOLON || currentToken.kind == Token.LBRACE || currentToken.kind == Token.IF || currentToken.kind == Token.WHILE)
- parseStatement();
- }
- // Statement = ";" | Block | Assignment | IfStatement | WhileStatement
- private void parseStatement() {
- if (currentToken.kind == Token.SEMICOLON)
- expectIt();
- else if (currentToken.kind == Token.LBRACE) {
- parseBlock();
- }
- else if (currentToken.kind == Token.IDENTIFIER) {
- parseAssignment();
- }
- else if (currentToken.kind == Token.IF) {
- parseIfStatement();
- }
- else if (currentToken.kind == Token.WHILE) {
- parseWhileStatement();
- }
- else new Error("Expeting ;, left brace, etc.", currentToken.line);
- }
- private void parseBlock() {
- accept (Token.LBRACE);
- parseStatements();
- accept (Token.RBRACE);
- }
- // Assignment = Identifier "=" Expression ";"
- /*
- */
- private void parseAssignment() {
- parseIdentifier();
- accept (Token.ASSIGN);
- parseExpression();
- accept (Token.SEMICOLON);
- }
Add Comment
Please, Sign In to add comment