编写编译器...什么是正确的,什么问题? [英] Writing compilers ... what's right and what's wrong?

查看:134
本文介绍了编写编译器...什么是正确的,什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在我寻求编写一个编译器所需的东西,我遇到了一个路障。看来我发现的每个技术或工具在某个地方有一些反对。



我现在使用Bison和Flex,但我得到的感觉,这种方法是过时的。这是真的?



在一系列不同的概念和工具(ANTLR,LL(k),GLR(k))中,这是一个很好的向前兼容的方式来编写一个完整的编程语言,LALR,LLVM,Flex,Bison)当前的编写编译器的趋势和最佳实践是什么?

解决方案

除非你想编写一个真正简单的编译器,否则你的焦点是错误的。 p>

编写编译器只是关于编写解析器的一小部分。有一个解析器就像
攀登喜马拉雅山的山麓,当问题攀登珠穆朗玛峰。你到达山脚的顶部,看起来...只有20,000英尺去,你只做了真正容易的部分。你会注意到,到达山脚下山顶所需的技术比你需要的技术要简单得多。



(FYI:目前最好的解析技术是 GLR ,这很容易
接受不明确的语法,而不会破坏语法GLR甚至很容易解析C ++,
违反了C ++很难解析的民俗定理民俗定理
来自人们尝试使用YACC和ANTLR来解析它)。



要构建一个编译器,您需要许多机制:




  • AST建立

  • 符号表构造

  • 控制流分析

  • 数据流分析

  • 程序表示

  • 目标机器的模型

  • 一种将程序代码映射到机器指令的方法
  • / li>
  • 注册分配

  • 优化:常量传播,循环展开,...



对于涉及SIMD指令或缓存优化的现代指令集,我们甚至没有接近全局流分析,全局优化或特殊处理

...
列表继续。龙书提供了一个很好的介绍基本主题,但没有解决任何先进的。你会想要Cooper的工程编译器和Muchnick的高级编译器设计作为参考,如果你在开始之前已经撇过它们,那么这将是很好的。



现代编译器是相当的工程壮举。


Okay, in my quest to figure out the necessary stuff to write a compiler, I've reached a bit of a roadblock. It seems that every technology or tool that I find has some opposition somewhere.

I use Bison and Flex right now but I'm getting the feeling that this method is outdated. Is this true? Is this a good forward-compatible way to proceed with writing a full fledged programming language?

In a sea of different concepts and tools (ANTLR, LL(k), GLR, LALR, LLVM, Flex, Bison) What's the current trend and best practices for writing compilers? Is the dragon book out of date?

解决方案

Unless you want to write a truly simple compiler, your focus is wrong.

Writing compilers is only a tiny bit about writing parsers. Having a parser is like climbing the foothills of the Himalayas when the problem is climbing Everest. You get to the top of the foothill and look up ... only 20,000 feet to go and you've only done the truly easy part. And you'll note the technology required to get to the top of the foothills is radically easier than the technology you need to go the rest of the way.

(FYI: the best present parsing technology is GLR, which easily accepts ambiguous grammars without hacking the grammar. GLR even easily parses C++, which violates the folk theorem that C++ is hard to parse. The folk theorem came from people trying to use YACC and ANTLR to parse it).

To build a compiler you need lots of machinery:

  • AST building
  • Symbol table construction
  • Control flow analysis
  • Data flow analysis
  • Representation of program code essentially as a data flow computation (SSA or triples)
  • A model of the target machine
  • A means to map program code to machine instructions
  • Register allocation
  • Optimizations: constant propagation, loop unrolling, ...

We haven't even gotten near global flow analysis, global optimizations, or special handling for modern day instruction sets involving SIMD instructions or cache optimizations. ... The list goes on and on. The Dragon book gives a nice introduction to the basic topics, but doesn't address any of the advanced ones. You'll want Cooper's "Engineering a Compiler" and Muchnick's "Advanced Compiler Design" as references and it would be good if you had skimmed them well before you start.

Building a modern compiler is quite a feat of engineering.

这篇关于编写编译器...什么是正确的,什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆