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 ...
随机推荐
- iPhone 物理尺寸与分辨率
// iPhone 物理尺寸(pt:Point) 分辨率(px) // 4S 320*480(3.5英寸) 640*960 // 5,5c,5S 32 ...
- (字典树)How many--hdu--2609
http://acm.hdu.edu.cn/showproblem.php?pid=2609 How many Time Limit: 2000/1000 MS (Java/Others) Me ...
- Spring MVC 的@RequestParam注解和request.getParameter("XXX")
在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...
- Delphi XE5 图解为Android应用制作签名
http://redboy136.blog.163.com/blog/static/107188432201381872820132 Delphi XE5 图解为Android应用制作签名 2013- ...
- dpdk EAL: Error reading from file descriptor 23: Input/output error
执行test程序时输出: EAL: Error reading from file descriptor 23: Input/output error 原因: 在虚拟机添加的网卡,dpdk不支持导致的 ...
- 879. Profitable Schemes
There are G people in a gang, and a list of various crimes they could commit. The i-th crime generat ...
- docker registry 私有仓库 安装配置、查询、删除
#++++++++++++++++++++++++++++++ #docker-registry 私有仓库 #搜索,下载register镜像 docker search registry docker ...
- 世界线(bzoj2894)(广义后缀自动机)
由于春希对于第二世代操作的不熟练,所以刚使用完\(invasion process\)便掉落到了世界线之外,错综复杂的平行世界信息涌入到春希的意识中.春希明白了事件的真相. 在一个冬马与雪菜同时存在的 ...
- Divide and Conquer-169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- firebug中html显示为灰色的原因总结
1.被设置了display:none. 2.长.宽都为0.