Architecture of a Java Compiler
- The optimizer
- The code generator
- Parsing the source language to recognize correct programs and report syntax errors for incorrect language constructs. In the case of the BPI Java front end, this is done by a parser generated with the ANTLR parser generator. The output of the parser is an abstract syntax tree (AST) which includes all declarations that were in the source.
- Reading declaration information in Java class files and, for a native Java compiler, building ASTs from the byte code stream. This also involves following the transitive closure(传递闭包) of the classes required to define the root class. (Def: transitive closure - All the nodes in a graph that are reachable from the root. In this case the graph is the tree of classes that are needed to define all the classes read by the compiler).
- Processing the declarations in the AST and class files to build the symbol table. Once they are processed the declarations are pruned (删减) from the AST.
static char x; int foo() { int x; { float x; } }
- Java has a large global scope, since all classes and packages are imported into the global name space. Global symbols must be stored in a high capacity data structure that supports fast (O(n)) lookup (a hash table, for example).
- Java has lots of local scopes (classes, methods and blocks) that have relatively few symbols (compared to the global scope). Data structures that support fast high capacity lookup tend to introduce overhead (in either memory use or code complexity).支持快速高容量查找的数据结构往往会引入开销(在内存使用或代码复杂性方面) This is overkill for the local scope. The symbol table for the local scopes should be implemented with a data structure that is simple and relatively fast (e.g., (O(log2 n))). Examples include balanced binary trees and skip lists.
- The symbol table must be able to support multiple definitions for a name within a given scope. The symbol table must also help the compiler resolve the error cases where the same kind of symbol (e.g., a method) is declared more than once in a given scope.
class Rose { Rose( int val ) { juliette = val; } public int juliette; } // Rose class Venice { void thorn { garden = new Rose( 42 ); Rose( 86 ); garden.Rose( 94 ); } Rose Rose( int val ) { garden.juliette = val; } Rose garden; } // venice
Global (objects imported via import statements) Parent Interface (this may be a list) Interface (there may be a list of interfaces) Parent class Class Method Block
interface bar { int x = 42; } class fu { double x; } class DoD extends fu implements bar { int y; // No error, since there is no local reference to x }
class DoD extends fu implements bar { int y; DoD() { y = x + 1; // Error, since the reference to x is ambiguous } }
interface BuildEmpire { class KhubilaiKahn { public int a, b, c; } } class GengisKahn { class KhubilaiKahn { public double x, y, z; } } class mongol extends GengisKahn implements BuildEmpire { void mondo() { KhubilaiKahn TheKahn; // Ambiguous reference to class KhubilaiKahn } }
interface Maryland { String key = "General William Odom"; } interface ProcurementOffice { String key = "Admiral Bobby Inman"; } interface NoSuchAgency extends Maryland, ProcurementOffice { String RealKey = key + "42"; // ambiguous reference to key }
- Support for multiple definitions for a given identifier.
- Fast lookup (O(n)) for a large global (e.g., package level) symbol base.
- Relatively fast lookup (O(log2 n)) for local symbols (e.g., local to a class, method or block)
- Support for Java hierarchical scope
- Searchable by symbol type (e.g., member, method, class).
- Quickly determine whether a symbol definition is ambiguous.
interface tonic { int water = 1; int quinine = 2; int sugar = 3; int TheSame = 4; } class gin { public int water, alcohol, juniper; public float TheSame; } class g_and_t extends gin implements tonic { class contextName { public int x, y, z; } // contextName public int contextName( int x ) { return x; } public contextName contextName; }
Scope and Local Variables and Arguments
class bogus { public void foobar() { int a, b, c; { // this is a scope block int x, y, z; } }
class Test { public static void main( String[] args ) { int i; for (int i = 0; i < 10; i++) // Error: local variable redefinition redeclared System.out.println(i); } }
A local variable is allowed to redefine a class member. This makes variable redefinition a semantic check in the semantic analysis phase.
class Test { int i = j; // compile-time error: incorrect forward reference int j = 1; }
Nor is forward reference allowed for local variables. For example:
class geomancy { public float circleArea( float r ) { float area; area = pie * r * r; // undefined variable 'pie' float pie = (float)Math.PI; return area; } }
However, forward reference is allowed from a local scope (e.g., a method) to a class member defined in the enclosing class. For example, in the Java below the method getHexChar makes a forward reference to the class member hexTab:
class HexStuff { public char getHexChar( byte digit ) { digit = (byte)(digit & 0xf); char ch = hexTab[digit]; // legal forward reference to class member return ch; } // getHexchar private static char hexTab[] = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; } // HexStuff
- Class and interface definitions that have the public modifier.
- Sub-packages (e.g., packages that are imported into package B).
- Support for multiple definitions for a given identifier.
- Fast global lookup
- Package information
- Local lookup
- skip lists (see also Thomas Niemann's excellent web page on skip lists).
- Red-Black Trees (a form of balanced binary tree)
- Simple binary tree
- Support for Java hierarchical scope
- Searchable by symbol type
- Quickly determine whether a symbol definition is ambiguous
Recursive Compilation and the Symbol Table
Architecture of a Java Compiler的更多相关文章
- Java compiler level does not match解决方法
从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description Resource Path Location Type Java compiler level d ...
- idea报错:error java compilation failed internal java compiler error
idea下面报如下问题 error java compilation failed internal java compiler error 解决办法:Setting->Compiler-> ...
- idea Error:java: Compilation failed: internal java compiler error
idea 遇到Error:java: Compilation failed: internal java compiler error 是提示说你当前使用的编译器jdk版本不对. 按住Ctrl+Alt ...
- java compiler level does not match the version of the installed java project facet 解决方案
项目出现 java compiler level does not match the version of the installed java project facet 错误,一般是项目移植出现 ...
- idea之internal java compiler error
启动错误:Error:java: Compilation failed: internal java compiler error 解决:将圈选地方改为对应的jdk版本即可
- Error:java:Compilation failed: internal java compiler error
在IDEA中编译时出现这个错误:Error:java:Compilation failed: internal java compiler error! Information:Using javac ...
- java compiler level does not match the version of the installed java project facet
Java compiler level does not match the version of the installed java project facet错误的解决 因工作的关系,Eclip ...
- Java compiler level does not match the version of the installed Java project facet.(转)
Java compiler level does not match解决方法 从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description Resource P ...
- maven项目 Java compiler level does not match the version of the installed Java project facet
因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误.明明源码一模一样,为什么项目复制到另一台机器上,就会报“java compiler level does not m ...
随机推荐
- web service 架构
Web services architecture The service provider sends a WSDL file ...
- day04(权限修饰符,内部类,局部内部类,匿名内部类)
权限修饰符, Public >protected >default > private public 公共权限 随便都可以访问 protected 子类可以访问权限 (子类 ...
- (线段树)Mayor's posters --poj -- 2528
链接: http://poj.org/problem?id=2528 覆盖问题, 要从后往前找, 如果已经被覆盖就不能再覆盖了,否则就可以覆盖 递归呀递归什么时候我才能吃透你 代码: #include ...
- C++中的结构体的认识
C++中的结构体的认识 1. typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间. 实例像:typedef ...
- nodejs async
官网:https://github.com/caolan/async 流程控制:简化十种常见流程的处理集合处理:如何使用异步操作处理集合中的数据工具类:几个常用的工具类 流程控制 详细说明:http: ...
- nodejs 获取文件的编码方式
使用nodejs获取文件夹内文件的编码方式:使用jschardet模块. 下面的代码还有问题,没有添加结束的语句,没有判断应该在哪执行res.send(). res.send()不能放在forEach ...
- Android Studio生成get,set,tostring,构造方法
如何在AndroidStudio开发Android应用程序的时候,在对象模型中生成快捷方式生成get,set,tostring,构造方法等: 有两种方式: 第一种方式:Code –> Gener ...
- Eclipse C++,Cygwin 64,gcov,lcov 单体&覆盖率测试环境搭建笔记
1.下载并安装 Eclipse IDE for C/C++ Developers https://eclipse.org/downloads/packages/eclipse-ide-cc-devel ...
- navicat远程连接oracle
本机没有oracle,这个软件太大了. 想要远程连接oracle,本地不安装oracle的话是不行的,我们安装一个oracle instance client,然后配置navicat就ok了. 下载i ...
- JSON 数据转成Table
public static DataTable JsonToDataTable(string strJson) { //转换json格式 strJson = strJson.Replace(" ...