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 ...
随机推荐
- 201709011工作日记--ART与Dalvik&&静态类与非静态类
1.ART 与 Dalvik 的优缺点对比 什么是Dalvik:Dalvik是Google公司自己设计用于Android平台的Java虚拟机.dex格式是专为Dalvik应用设计的一种压缩格.Dalv ...
- (字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5455 Fang Fang Time Limit: 1500/1000 MS (Java/Others) ...
- 层层递进Struts1(三)之Struts组成
这篇博客我们来说一下Struts的主要组成我们,通过前几篇博客,我们知道这个框架最重要的几个步骤:获取路径.封装表单.获取转向列表.转向逻辑处理.转向,与此对应的是:ActionServlet.Act ...
- 虚拟化之Xen简介
1>相关知识简介: 1>常用的磁盘IO调度器: CFQ:完全公平队列算法: deadline:最后期限算法: anticipatory:顺序读写队列算法/预期算法: NOOP:no op ...
- The MATLAB Profiler
function a = myFunc(a,b,c,d,e) : a = a-b + c^d*log(e); end end >> profile on; myFunc(a,b,c,d,e ...
- 基于CORS的GeoServer跨域访问策略
GeoServer的跨域访问问题,有多种解决方法,本文介绍一种基于CORS的GeoServer跨域访问方法. CORS简介 CORS是一个W3C标准,全称是"跨域资源共享"(Cro ...
- 关于Java连接SQL Sever数据库
1.前提条件 需要: 1>本机上装有SQL Sever数据库(2005.2008或者更高版本) 2>eclipse或者myeclipse开发环境 3>jar文件(名为sql_jdbc ...
- Android事件分发机制浅析(2)
本文来自网易云社区 作者:孙有军 上面的两次执行中每次都调用了onInterceptTouchEvent事件,这个到底又是啥?我们去看看他的返回值是什么? public boolean onInter ...
- Restframework 渲染器 render 组件实例-4
渲染器默认存放位置: 在默认配置下 default-settings里 (APIVIEW点击去--> 1. renderer_classes = api_settings.DEFAULT_REN ...
- Android 日常总结的一些方法使用
1. setImageResource : 更改图片的资源 2. setClickable : 设置为true时,表明控件可以点击,如果为false,就不能点击 . 注意,setOnClickLi ...