Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

I got errors in parser.java. How to create methods in TokenHandler.java for getCurrentToken(), consumeToken(), getLastMatchedToken()? Please create these methods in TokenHandler.java. Fix the error for programNode that is attached. Below is TokenHandler.java

 

TokenHandler.java

import java.util.LinkedList;
import java.util.Optional;
import mypack.Token.TokenType;
 
public class TokenHandler {
 
private LinkedList<Token> tokens;// List of tokens
 
    public TokenHandler(LinkedList<Token> tokens) {
        this.tokens = tokens;
    }
 
    public Optional<Token> Peek(int j) {
        if (j < tokens.size()) {
            return Optional.of(tokens.get(j));
        } else {
            return Optional.empty();
        }
    }
 
    public boolean MoreTokens() {
        return !tokens.isEmpty();
    }
 
    public Optional<Token> MatchAndRemove(TokenType t) {
        if (MoreTokens() && Peek(0).get().getType() == t) {
            return Optional.of(tokens.remove(0)); // Remove and return a token of specified type
        } else {
            return Optional.empty();
        }
    }
}
21
ជ ជួ ន ដ ឌ ន ន ន ន ន
22
23
24
25
26
27
28
29
30
310
*32
33
34
€35
36
37
38
39
40
41
42
430
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}
}
if (currentToken.getType() == Token. TokenType.NEWLINE || currentToken.getType() == Token. TokenType.
tokenHandler.consumeToken();
foundSeparator = true;
} else {
}
break;
}
return foundSeparator;
public ProgramNode Parse() throws Exception {
ProgramNode program = new ProgramNode ();
while (tokenHandler.MoreTokens()) {
if (!parseFunction (programNode) && !parseAction (programNode)) {
throw new ParseException ("Unexpected token: " + tokenHandler.getCurrentToken());
}
}
return programNode;
private boo
Token f
if (tok
Fun
pro
programNode cannot be resolved to a variable
5 quick fixes available:
}
• Create local variable 'programNode'
• Create field 'programNode'
Change to 'program'
Create parameter 'programNode'
• Create constant 'programNode'
de) {
htToken();
IDENTIFIER) != null) {
unctionDefinitionNode (functionName Token.getValue());
if (tokenHandler.MatchAndRemove (Token. TokenType. LPAREN) != null) {
// Handle function parameters here if needed
// You can implement this part according to your language's syntax
// For simplicity, let's assume no parameters for now
if (tokenHandler. MatchAndRemove (Token. TokenType.RPAREN)!= null) {
AcceptSeparators();
return true;
■
expand button
Transcribed Image Text:21 ជ ជួ ន ដ ឌ ន ន ន ន ន 22 23 24 25 26 27 28 29 30 310 *32 33 34 €35 36 37 38 39 40 41 42 430 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 } } if (currentToken.getType() == Token. TokenType.NEWLINE || currentToken.getType() == Token. TokenType. tokenHandler.consumeToken(); foundSeparator = true; } else { } break; } return foundSeparator; public ProgramNode Parse() throws Exception { ProgramNode program = new ProgramNode (); while (tokenHandler.MoreTokens()) { if (!parseFunction (programNode) && !parseAction (programNode)) { throw new ParseException ("Unexpected token: " + tokenHandler.getCurrentToken()); } } return programNode; private boo Token f if (tok Fun pro programNode cannot be resolved to a variable 5 quick fixes available: } • Create local variable 'programNode' • Create field 'programNode' Change to 'program' Create parameter 'programNode' • Create constant 'programNode' de) { htToken(); IDENTIFIER) != null) { unctionDefinitionNode (functionName Token.getValue()); if (tokenHandler.MatchAndRemove (Token. TokenType. LPAREN) != null) { // Handle function parameters here if needed // You can implement this part according to your language's syntax // For simplicity, let's assume no parameters for now if (tokenHandler. MatchAndRemove (Token. TokenType.RPAREN)!= null) { AcceptSeparators(); return true; ■
Expert Solution
Check Mark
Step 1: Code after errors correction

To create the methods getCurrentToken(), consumeToken(), and getLastMatchedToken() in TokenHandler.java, you can modify the class as follows:


import java.util.LinkedList;
import java.util.Optional;
import mypack.Token.TokenType;

public class TokenHandler {

    private LinkedList<Token> tokens; // List of tokens
    private Token currentToken;
    private Token lastMatchedToken;

    public TokenHandler(LinkedList<Token> tokens) {
        this.tokens = tokens;
        this.currentToken = null;
        this.lastMatchedToken = null;
    }

    public Optional<Token> Peek(int j) {
        if (j < tokens.size()) {
            return Optional.of(tokens.get(j));
        } else {
            return Optional.empty();
        }
    }

    public boolean MoreTokens() {
        return !tokens.isEmpty();
    }

   


Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education