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 ...
随机推荐
- 11.字符串{a,b}的幂集[回溯递归]
我一直在想着这个事,早晨起来五六点,躺在床上冥想.突然悟解了,真如某些书上写的,大道不过三言两语,说破一文不值.还是按照老方法,把问题最大程度的精简,现在求集合A={a,b}的幂集,只有两个元素,应该 ...
- Activity生命流程
做Android的同学说起 Activity,那绝对是熟悉的不能再熟悉了,但是越熟悉的东西往往越陌生.我们真的了解她吗?她是我们所认识的那样吗?或许是,或许不是!了解与否, 让我们往下看.首先借And ...
- linux环境下(非UI操作)所有软件的安装与卸载总结
UI界面的软件管理 linux下的软件一般都是经过压缩的,主要的格式有这几种:rpm.tar.tar.gz.tgz等.所以首先拿到软件后第一件事就是解压缩. 在xwindow下以rpm格式的软件安装比 ...
- Date日期操作
获取年月日时分秒: package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util ...
- js插件开发的一些感想和心得
起因 如果大家平时做过一些前端开发方面的工作,一定会有这样的体会:页面需要某种效果或者插件的时候,我们一般会有两种选择:1.上网查找相关的JS插件,学习其用法2.自己造轮子,开发插件. 寻找存在的插件 ...
- .NET高级代码审计(第一课)XmlSerializer反序列化漏洞
0X00 前言 在.NET 框架中的 XmlSerializer 类是一种很棒的工具,它是将高度结构化的 XML 数据映射为 .NET 对象.XmlSerializer类在程序中通过单个 API 调用 ...
- oracle 导出导入命令
imp YG_XSOA_NEW/kingo@20.14.12.14/XSSJZX file=d:\daochu.dmp full=y (导入) exp YG_XSOA_NEW/kingo@20 ...
- ASP.NET Core 2 学习笔记(八)URL重写
路由跟URL 重写的功能性略有不同.路由是将Request 找到对应的服务,而URL 重写是为了推卸责任转送Request.本篇将简单介绍下ASP.NET Core的URL重写(URL Rewrite ...
- 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构
本篇承接上篇内容,如果你不小心点击进来,建议重新从第一篇开始完整阅读. 构建NetCore应用框架之实战篇索引 一.BitAdminCore框架简介 从前篇论述我们知道,我们接下来将要去做一个管理系统 ...
- Win(Phone)10开发第(5)弹,本地媒体服务器的一些注意事项
首先有个wp上的http服务器 http://wphttpserver.codeplex.com/ 使用方式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...