用于无大括号,空白敏感的C语法的工具

我现在正在写一些C,因为我喜欢空格敏感的语法,我想这样写:

#include  int main(void) printf("Hello, world!") return 0 

而不是这个:

 #include  int main(void) { printf("Hello, world!"); return 0; } 

有没有人知道将前者转换为后者的工具?

编辑:我真的没有兴趣与那些认为这是个坏主意的人争论。 通过各种方式继续认为,你有你的理由。 但至少知道这一点:我知道Python是一种空格敏感的语言,但我还没有使用它。 我为什么要? 我已经知道Ruby了。 也知道:我不仅仅是第一次学习C而且我已经使用PHP和JavaScript超过四年了,所以我并不是出于某些个人困难,对块语法缺乏熟悉或教条联系而要求这样做。 我也知道写这些内容会涉及到什么,这不是我的能力,但我不希望这足以certificate花时间写一个。

PythoidC是一种无支撑C语言http://pythoidc.googlecode.com

 c.include(chstdio) c.include(chstdlib) int fib(int n): if (n<=2): return 1 else: return fib(n-1) + fib(n-2) int main(int argc, char **argv): int n //C style annotation n=c.stdlib.atoi(argv[1]) c.stdio.printf('fibonacci(%d)=%d\n', n, fib(n)) 

PythoidC自动生成以下C代码:

 int fib(int n){ if (n<=2){ return 1;} else{ return fib(n-1) + fib(n-2);}} int main(int argc, char **argv){ int n ;//C style annotation n=atoi(argv[1]); printf("fibonacci(%d)=%d\n", n, fib(n)); } 

即使有这样的工具,我也强烈建议你重新考虑这个想法。 以下是我认为你会发现的一些问题:

  1. 您的代码将不再是标准C.
  2. 这意味着你会遇到其他程序员阅读代码的问题。
  3. 您也将无法使用任何代码分析工具,因为他们无法理解您的语法。
  4. 如果你有一些工具可以转换,比如每次编译,那仍然意味着你将编写不同的代码而不是你正在阅读的代码。 我讨厌使用一种工具来改变我的代码。

这似乎是一种将自己的习惯融入其他人的情况,这是一种更聪明的方法。

希望这会让你重新考虑。

如果你真的想这样做,那么在没有实现语言解析器的情况下是不可能的,即使这样,我也不确定编码约定对于你的“看起来像C的新语言”中的某些情况如何没有大括号“。 例如,请使用以下C代码:

 struct a { int i; }; int main(void) { ... } 

你可以把它写成

 struct a int i int main(void) ... 

但它必须转换为原始代码,而不是:

 struct a { int i; } /* Note the missing semicolon! */ int main(void) { ... } 

另外,鉴于以下片段:

 /* declare b of type struct a */ struct a { int i; } b; /* a struct typedef */ typedef struct a { int i; } b; 

你打算用你的语言指定这些?

您似乎也不想在您的语言中使用分号。 这会严重限制您的代码,并使转换工具也变得复杂,因为您无需额外的努力就无法使用延续线:

 i = j + k; 

是合法的C,但是

 i = j + ; k; 

不是。

首先,您需要更精确地定义“无支撑C”的语法。 正如其他人所说,这种事情充满了危险。

C语言的Python式缩进 。

看起来这就是你要找的东西。

没有工具,但伪代码:

 last_spc_count = 0 For all lines in input file check number of trailing spaces spc_count Print old line If spc_count > last_spc_count print "{\n" (last_spc_count-spc_count)/2 times Else If spc_count < last_spc_count print "}\n" (last_spc_count-spc_count)/2 times Else print "\n" last_spc_count = spc_count print "}\n" last_spc_count/2 times 

是的,我真的很喜欢。 他们称之为Python 。

http://www.cython.org/

但这是一种不同的语言……它基本上是一种具有C性能属性的Python方言。

不要使用自己的语言,使用标准的东西。

我不相信这样的工具存在。 存在用于清除格式的工具,但代码必须已经C格式化。

我真的认为你应该使用设计的语言并学会使用大括号。