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 ...
随机推荐
- (转) MVC身份验证及权限管理-2
转自:http://www.cnblogs.com/ldp615/archive/2010/10/27/asp-net-mvc-forms-authentication-roles-authoriza ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
- Billman_ford货币升值——正权回路
2240和1860那个题目很像啊 都是问货币能不能增多,钻社会制度得空子啊哈哈 唯一不同得是你的起点是任意一个点,这个比较麻烦了,多了一层循环嘞 处理货币名可以用map分配id 然后就是老套的Bill ...
- 深浅 buffer
var str = "深入浅出"; var buf = new Buffer(str, 'utf-8'); console.log(buf); 这种情况下是数字 var str = ...
- Java实现WordCount
GitHub项目地址:https://github.com/happyOwen/SoftwareEngineering wordcount项目要求: 程序处理用户需求的模式为:wc.exe [para ...
- 如何使用linq读取DataTable集合?AsQueryable() 和 AsEnumerable()区别?
一.准备工作 引入linq和data 相关的using命名空间 DataTable dt=new DataTable();//dt的来源可以是多个地方,比如:数据库,Excel等等.我这里使用Exce ...
- 一个简单的QQ隐藏图生成算法
隐藏图不是什么新鲜的东西,具体表现在大部分社交软件中,预览图看到的是一张图,而点开后看到的又是另一张图.虽然很早就看到过这类图片,但是一直没有仔细研究过它的原理,今天思考了一下,发现挺有趣的,所以自己 ...
- VS2017常用快捷键整理
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示 Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添 ...
- ODP.NET 之 ExecuteNoQuery 执行 Merge into 返回值
当执行Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteNonQuery时,如果sql语句是 merge into ...,则返回值表现不稳定, ...
- 第五章 企业项目开发--mybatis注解与xml并用
本章的代码建立在第四章<Java框架整合--切分配置文件>的项目代码之上,链接如下: http://www.cnblogs.com/java-zhao/p/5118184.html 在实际 ...