百科问答小站 logo
百科问答小站 font logo



Clang parser是完全手写的吗? 第1页

  

user avatar   rednaxelafx 网友的相关建议: 
      

是。

Clang - Features and Goals
A single unified parser for C, Objective C, C++, and Objective C++

Clang is the "C Language Family Front-end", which means we intend to support the most popular members of the C family. We are convinced that the right parsing technology for this class of languages is a hand-built recursive-descent parser. Because it is plain C++ code, recursive descent makes it very easy for new developers to understand the code, it easily supports ad-hoc rules and other strange hacks required by C/C++, and makes it straight-forward to implement excellent diagnostics and error recovery.

We believe that implementing C/C++/ObjC in a single unified parser makes the end result easier to maintain and evolve than maintaining a separate C and C++ parser which must be bugfixed and maintained independently of each other.

因为要加各种奇怪的功能还是手写的parser更容易实现和理解。

用parser generator的话总会有别扭的地方要跟那个generator“抗争”一下,反而麻烦。

其实GCC的C++ parser从GCC 3.4系列开始也改为手写的了。

gcc.gnu.org/gcc-3.4/cha
  • A hand-written recursive-descent C++ parser has replaced the YACC-derived C++ parser from previous GCC releases. The new parser contains much improved infrastructure needed for better parsing of C++ source codes, handling of extensions, and clean separation (where possible) between proper semantics analysis and parsing. The new parser fixes many bugs that were found in the old parser.

而GCC的C和Objective-C parser从GCC 4.1系列开始也改为手写的了。

gcc.gnu.org/gcc-4.1/cha
  • The old Bison-based C and Objective-C parser has been replaced by a new, faster hand-written recursive-descent parser.

当然,还是有人觉得用parser generator更好。例如这里:

c - Are GCC and Clang parsers really handwritten?

来自

Semantic Designs

的Ira Baxter认为坊间传闻C++ parser用generator不好写不是因为generator这个概念不好,而是因为用的parsing算法太落后了——传统的parser generator很多都是用LALR(1)的,例如bison的默认模式。

他认为如果用更强力的parsing算法,例如

GLR

,问题就迎刃而解了。

嗯这个还是见仁见智。召唤

@vczh

大神来发表对GLR的见解 >_<

有兴趣的同学可以看看一个用GLR的C++ parser,

Elsa

设计文档

,就跟

@vczh

的回答说的一样,在parse阶段其实parse出了多份有歧义的AST,然后通过类型检查把不合理的删除掉,得到最终的干净的AST。


Clang官网文档有对比Clang parser与Elsa parser的段落:

Comparing clang to other open source compilers



  

相关话题

  如何评价 SDCC (Small Device C Compiler) 编译器套件? 
  为什么编程语言中没有一种 if,来判断大概率为真(或假)的情况,来提升 CPU 分支预测的速度呢? 
  中国为什么不做编译器和编程语言? 
  请问从技术角度来看是鸿蒙操作系统,木兰编程语言,方舟编译器哪个技术水平更高一些? 
  大一新生做C语言课设被强制要求使用 Borland C++ 3.1,此IDE相对于其他IDE有何优点? 
  a += a *= a; 为什么在C++和Java算出了不同结果? 
  如果编译器遇到未定义行为把硬盘格式化了,需要负法律责任吗? 
  华为方舟编译器 Runtime 已经开源,从技术角度如何评价其架构和实现? 
  使用 open addressing 的 Hash 表载荷过高为什么会降低 CPU 的缓存命中率? 
  如何看待方舟编译器开源maple engine,编译的程序终于可以运行起来了? 

前一个讨论
AMD 盘前涨近 11%,业绩指引超预期,其上涨的原因是什么?
下一个讨论
AMD的CPU是否适合搭建深度学习主机?





© 2024-11-12 - tinynew.org. All Rights Reserved.
© 2024-11-12 - tinynew.org. 保留所有权利