I am writing a compiler for a subset of the Java language and during writing the scanner i was wondering how do i recognize types that are not primitive but come from a library. For example the String type is not primitive in the Java language. In general, how do i recognize new types from libraries as “keywords” in the language without knowing them in advance. Thanks!
Share
Try getting through http://www.antlr.org/grammar/1152141644268/Java.g (pay attention to the
typedeclaration and its usage) — it will give you some insight.In brief, when you parse the source, you know where you expect to see a type, be it after modifiers in fields and methods, just before parameter names in formal parameter list, etc. And when you know that
Stringis some type, you can check withimporttable to get a full name, and go find the actual class in the classpath.Now, as Bart mentions in the comments (now deleted), the real resolution happens later in the pipeline. At the parsing stage, you only create a node saying that you have a
typewhich is anIdentifierwhich isString. So the answer at this moment would be, “you don’t.”Update: try following this thesis, it should help you.