2

Do you think its possible for a single person (average C++ experience) to build a non-commercial C++ compiler using Erlang, possibly concentrating on optimization?

I wasnt sure if this is completely unrealistic? Is there any advice people could give?

Is erlang the best language to use? I thought it would be good due to its pattern matching. Im not sure if it concurrency would help with writing a compiler??

EDIT: The reason for this is that I dont get to code C++ at work and I want to learn more about the language as I am interested in low latency work. I thought knowing the ins and outs via writing a compiler would be the best way?

intrigued_66
  • 16,082
  • 51
  • 118
  • 189

1 Answers1

9

A C++ compiler is a lot of work. No, really, a lot of work. C++ is one of the hardest (if not the hardest) production languages to parse. Even just the front-end. Just try reading the standard, it's more than one thousand pages of dense text.

What do you want to use it for? LLVM has the Clang C/C++ front end and an extremely friendly and well-documented intermediate representation. I suggest you use something like this (from Erlang, appropriately adapted or otherwise) and concentrate on the optimisation stage - leaving the parsing to someone else.

Pattern matching does make for a nice compiler though. So Erlang/F#/Scala/Ocaml/Haskell will shine here.

Alex Wilson
  • 6,690
  • 27
  • 44
  • I want to do it to REALLY learn/improve my C++. I find the best way to learn is to do something interesting, involving it. I dont get to use C++ at work... – intrigued_66 Jul 10 '12 at 11:14
  • 13
    @fast_code building a compiler in Erlang will neither help you to learn nor improve your C++ ability, just as having sex will not help you learn or improve your child raising skills. It can certainly be a fun process, but it does not meet your stated goal. – mah Jul 10 '12 at 11:18
  • 1
    @Porcupine: Would you not be better off writing the implementation in C++ then? Or an implementation of something else in C++? Try writing a library for multiple platforms/compilers that does something interesting - say a [functional-programming inspired collections library](http://www.scala-lang.org/docu/files/collections-api/collections.html) for C++11 using the new lambdas. That'll really test your C++. :-) – Alex Wilson Jul 10 '12 at 11:18
  • @AlexWilson whilst I enjoy writing Erlang, I dont like all the "original" functional programming stuff. Its one of the reasons I enjoy Erlang but despise Haskell. I love the concurrency with Erlang, the fact its functional is just a consequence. Writing a C++ compiler in C++ sounds a better idea. – intrigued_66 Jul 10 '12 at 11:22
  • @mah wouldn't I have to basically learn the langauge specification to write a compiler though? Or do you mean I may know the specification, but I wont get a true feel and remember C++ code unless I am coding it? – intrigued_66 Jul 10 '12 at 11:23
  • You mean one thousand pages, not lines. – PlasmaHH Jul 10 '12 at 11:25
  • @PlasmaHH. Ahem. I do mean pages not lines. Thank you. – Alex Wilson Jul 10 '12 at 11:26
  • 1
    the latter; you can know the language spec inside and out but that doesn't make you a better developer in that language. On the other hand, the project would certainly be great for improving your Erlang ability! I agree with @Alex Wilson; if your goal is to improve your C++ skills, write the compiler in C++. – mah Jul 10 '12 at 11:27
  • @mah and alex, I completely discounted that idea- im not wondering whether (as im a fairly good java programmer) would it be easier for me to write a java compiler, in c++ code.... – intrigued_66 Jul 10 '12 at 11:28
  • 1
    @fast_code. A java compiler in C++ would be much more tractable. Java is right at the other end of the ease-of-compilation spectrum. – Alex Wilson Jul 10 '12 at 11:32
  • assuming i stuck with C++, I presume i could implement the compiler in stages and just progress as I completed each stage- in steps, as they say? Also, if i did stick with C++ I presume i would have to know x86 assembler very well!? – intrigued_66 Jul 10 '12 at 11:37
  • 1
    @fast_code. You don't need to know x86. You could output JVM bytecode, or target the LLVM backend and get all sorts of target architectures for free. – Alex Wilson Jul 10 '12 at 11:40
  • 1
    Don't scare him off. The language spec is a mere 400 pages. The rest is the standard library (which he can do later :-). – Bo Persson Jul 10 '12 at 11:44
  • 1
    @BoPersson: Just 400 pages, that makes it simple... oh, wait, now that I remember it is actually hard. I have been reading the standard (core language) here and there for a few years and I am still surprised each so often with some little tiny text that changes the semantics of some particular piece of code. A C compiler, that would be simpler... a C++ compiler, if you want it to comply with the standard to any degree... that is much more complex. It is just 400 pages of thick dense specification :) – David Rodríguez - dribeas Jul 10 '12 at 11:54
  • 1
    @BoPersson: on the other hand, it's a very dense 400 pages with lots of cross-references... and even worse, many missing cross-references. It's too big for a single person (I would dare say it's too big for 10 persons...) – Matthieu M. Jul 10 '12 at 11:56
  • 2
    @DavidRodríguez-dribeas: I agree, the problem with the way the Standard is expressed is that corner-cases are not always cross-referenced to the original statement they contradict, making it very hard when reading a statement to know whether it applies or not to your current situation. Any slightly difficult question regarding the Standard cites pieces from 2 or 3 differents paragraphs :( – Matthieu M. Jul 10 '12 at 11:57
  • Anyone got a link to THE standard? I thought there was various different implementations... – intrigued_66 Jul 10 '12 at 12:17
  • 1
    @fast_code - There is just one *active* standard at a time. The latest revision is from 2011. See here for some links [Where do I find the current C or C++ standard documents?](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) – Bo Persson Jul 10 '12 at 12:35
  • @DavidRodríguez-dribeas could you elaborate onyour comment regarding a C compiler? Thanks in advance – intrigued_66 Jul 10 '12 at 16:32
  • @fast_code: The C language is much simpler than the C++ language, writting a compiler for C should be much simpler than C++. – David Rodríguez - dribeas Jul 10 '12 at 17:56