I'm making a simple parser for some Java-like language (just for learning purposes). I'm having trouble determining whether a statement is a variable declaration. This may be a problem with my lexer (which is really sloppy). If the lexer sees some text, it simply labels it an identifier, even if that text is a keyword or a type. The job of telling those apart is given to the parser.
This has worked so far, but now I'm trying to parse variable declarations, like this one here:
int x = 3;
The problem is I don't know how to determine whether this is a variable declaration. If I just look at the first token and find that it's an "identifier", that doesn't tell me anything, since this line of code also starts with an identifier:
System.out.print("hi");
And statements like this are handled by another part of the parser.
Another solution I thought about was checking to see if the first token is a type. For example, I could have a method that looks something like:
boolean isType(String t) {
if( t.equals("int") ||
t.equals("long") ||
t.equals("char") ||
/* et cetera */ )
return true;
else return false;
}
The problem with this is that it only allows for a certain set of types. Since my little language is compiled to Java bytecode, I need it to recognize arbitrary classes as types.
So my question is: is it possible to determine whether a statement is a variable declaration or not, without knowing all the possible variable types?