c - 在哪里可以找到实现编译器所需的完整 C 语法?

标签 c parsing syntax implementation lexical-analysis

我的目标不是编写 C 编译器,但我确实需要 C 编程语言的完整语法。这将使我能够更轻松地编写程序来格式化、管理和分析 C 程序和库。为实现这一目标,我别无选择,只能掌握该语言的全部语法。

语法应清楚说明什么是有效的,什么是无效的。考虑以下代码行:

int (x) = 0;

C 程序员瞥了一眼这个语句可能会犹豫它的有效性,直到他尝试编译它,他可能不知道它实际上是有效的 C。当然,很容易看出它等同于 int x = 0; 并且 x 周围的括号是多余的,但对于第一次看到它的程序员来说是否允许它并不清楚。

这是我需要的有关语言完整语法的详细程度。实现者使用它来编写可以编译任何 C 代码的编译器就足够了,即使我的意图不是编写编译器,但我的项目需要完整的语法细节。

最佳答案

C standard列出 the complete grammar在最后。

http://www.lysator.liu.se/c/ANSI-C-grammar-y.html它采用 yacc/bison 可编译的形式。

int (x) = 0;

是有效的,因为当你合并

(6.7) declaration:
                declaration-specifiers init-declarator-listopt ;
                static_assert-declaration
(6.7) declaration-specifiers:
                storage-class-specifier declaration-specifiersopt
                type-specifier declaration-specifiersopt
                type-qualifier declaration-specifiersopt
                function-specifier declaration-specifiersopt
                alignment-specifier declaration-specifiersopt
(6.7) init-declarator-list:
                init-declarator
                init-declarator-list , init-declarator
(6.7) init-declarator:
                declarator
                declarator = initializer

(6.7.6) declarator:
               pointeropt direct-declarator
(6.7.6) direct-declarator:
                identifier
                ( declarator )
                direct-declarator [ type-qualifier-listopt assignment-expressionopt ]
                direct-declarator [ static type-qualifier-listopt assignment-expression ]
                direct-declarator [ type-qualifier-list static assignment-expression ]
                direct-declarator [ type-qualifier-listopt * ]
                direct-declarator ( parameter-type-list )
                direct-declarator ( identifier-listopt )

then x in int x = 0;direct-declarator 并且语法允许在它周围加上括号(生产 direct-声明符::= ( 声明符 )).

关于c - 在哪里可以找到实现编译器所需的完整 C 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54383731/

相关文章:

java - 解析文本以查找地理名称

syntax - 如何使用 if then else 在 Crystal 报表公式中构建字符串

c - 我应该为成功的函数返回 0 还是 1?

android - 崩溃的JSON解析Android应用

c - 为什么我的 C 程序不能运行?从文件中读取

c# - 将 > 转换为 HTML 字符串中等效的 HTML 实体

powershell - 在PowerShell中运行命令行

syntax - 具有空白字符的 Elixir 中的奇怪行为

c - Makefile编译

无法理解 MPI 中的 MPI_Reduce_scatter