Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why are certain Tokens listed twice in Lexical Analysis? (EASY QUESTION!) Okay,

ID: 3671901 • Letter: W

Question

Why are certain Tokens listed twice in Lexical Analysis? (EASY QUESTION!)

Okay, I am writing a lexer Java program for one of my development classes, but I am very confused with our Tokens Class. Certain datatypes are being listed twice (ex. Float float, and FLOAT <float>)

In TokenClass.java it instantiates a hashmap

public class TokenType {
public static java.util.HashMap<Tokens,Symbol> tokens = new java.util.HashMap<Tokens,Symbol>();
public TokenType() {
tokens.put(Tokens.Program, Symbol.symbol("program",Tokens.Program));
tokens.put(Tokens.Int, Symbol.symbol("int",Tokens.Int));
tokens.put(Tokens.BOOLean, Symbol.symbol("boolean",Tokens.BOOLean));
tokens.put(Tokens.If, Symbol.symbol("if",Tokens.If));
tokens.put(Tokens.Then, Symbol.symbol("then",Tokens.Then));
tokens.put(Tokens.Else, Symbol.symbol("else",Tokens.Else));
tokens.put(Tokens.While, Symbol.symbol("while",Tokens.While));
tokens.put(Tokens.Function, Symbol.symbol("function",Tokens.Function));
tokens.put(Tokens.Return, Symbol.symbol("return",Tokens.Return));
tokens.put(Tokens.Identifier, Symbol.symbol("<id>",Tokens.Identifier));
tokens.put(Tokens.INTeger, Symbol.symbol("<int>",Tokens.INTeger));

So basically we have for tokens 2 Integers:

INTeger <int>

Int int

Other examples include:

Char char
CHAR <char>
String string
STRING <string>

My question is simple. Why are tokens listed twice? What's the difference between <int> and int, and why is it important? THANKS for ANY help! It's greatly appreciated.

Explanation / Answer

In computer science, lexical analysis is the process of converting a sequence of characters (such as in a

computer program or web page) into a sequence of tokens (strings with an identified "meaning").

int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt("1") doesn't make sense because int is not a class and therefore doesn't have any methods.

Integer is a class, no different from any other in the Java language. Variables of type Integerstore references to Integer objects, just as with any other reference (object) type. Integer.parseInt("1") is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer).

--->To be more specific, Integer is a class with a single field of type int.

-->This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.