Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Why everything low-level is written in C?
I mean kernel code, drivers, Run-time libraries, compilers, embedded systems s/w are mainly written in C/C++.
Why not use Fortran, COBOL, Pascal or even Java/C# or some other third-generation programming language that produces machine-independent code but also gives you the freedom to do low-level ,tweaks and optimizations.
My question is why developers and companies ended up using mostly C for these purposes.
Edit: Most of you here talk about performance. So, is the reason that there is no other general-purpose low-level language faster than C?
A few points:
Pascal is not low level language but there are kernels and even whole OS written in it.
You would not want to have a OS written in Java\C# because it will be darn slow.
C is probably not the best language. It has many cavities, and improvements like D or C++ have been tried. The only "problem" is inertia, C is still popular because C is the most widely used programming language (weather you like it or not). There is a plethora of kernels\OS\libraries\books\course with this language. It would take decades to replace it. And it seems that despite its cavities, there is very little will to completely replace it.
Java (and all JVM-based languages) and C#/F# run inside "virtual machines". That means the applications written in these languages cannot use hardware resourses directly, they are contained weithin a "sandbox". It helps portability ("runs everywhere where a VM is implemented") but can hurt performance (and does).
Some would say that the type of mind capable of writing low-level stuff can only be forged by years of damage caused by using C :-)
On a more serious note, the whole purpose of C was as a systems programming language and, as such, it mostly keeps out of your way. Other languages have different purposes: COBOL is really for transactional/business stuff, C# is for applications running under MS Windows, LISP is for people who have love counting parentheses, and so on. They can be used for other things but I wouldn't write an operating system in COBOL.
Or an accounting package in assembler.
Or anything in Pascal :-)
C allows you unfettered access to the lowest levels without having to concern yourself with things like garbage collection which may adversely affect your code in ways you can't foresee.
Because, comparatively, C and C++ are low level programming languages. Some people still write in Assembler. I hope no one still writes in machine code. Anyway,
Why not use Java, C#, COBOL, Pascal or some other third-generation programming language that produces machine-independent code but also gives you the freedom to do low-level tweaks and optimizations?
Those languages are classified as high level languages. They provide a level of machine abstraction that is beneficial for programming, but not useful for low level bare metal development. Also, relevant might be Why Pascal is Not My Favorite Programming Language by BWK.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I decided to start studying compiler theory but the problem is that I want a compiler for any language in order to track each of
lexical analyzer output.
syntax tree.
intermediate representation.
code generation.
I dont care for optimization right now
I am aware of some questions similar to mine about clang and gcc and I understand that both of them make lexical and syntax analysing on the fly
I just want any compiler in any language as long as the compiler itself is written in C and run on ubuntu x64
I am not sure you have the right approach, if you are willing to learn about compilation techniques for C specifically. And C is not the best language to write a compiler in (if you start from scratch, Ocaml is better suited for that task). BTW, recent Clang/LLVM or GCC are coded in C++ (no more in C).
The C language now sort-of requires optimization, as I explained here, so skipping the optimization part is not very useful. Be aware that optimization passes form the majority and the most difficult part of real-world compilers.
The lexing and parsing parts of compiler are now well understood. And there are several code generator tools for them (yacc or bison, lex or flex, ANTLR...). For several pragmatical reasons, real compilers like GCC don't use these tools.
You could look into tinycc, nwcc, or 8cc if you want to look inside non-optimizing toy C compilers.
You could also look into the intermediate representations of real compiler, e.g. GIMPLE for GCC (BTW, try to compile with gcc -fdump-tree-all -O2 -c some simple C code with a few loops; you'll be surprized by the hundreds of dump files showing the many internal compiler representations from many passes). You'll learn a lot by customizing GCC with MELT, and the MELT documentation page contains several very useful references. This answer should also help and contains or references some pictures of GCC.
disclaimer: I am the main author of MELT
PS. There are very good reasons to bootstrap compilers. So a compiler for a language other than C is unlikely to be coded in C (it is often coded for that language itself), since C is not a good programming language to write a compiler from scratch.
PPS. If you only know C -and no other programming languages-, I would suggest to learn some other programming language (e.g. Scheme with SICP, Ocaml, or Haskell or Scala or Clojure or Common Lisp) before diving into compilers! Read also something about Programming Language Pragmatics. If you know a bit of Scheme or Lisp, Queinnec's book Lisp In Small Pieces will teach you a big lot.
There are many, many places to start from to explore this territory. Many languages include a compilation capability or aspect such as Lisp and Forth.
To learn about a C compiler, there is a book about the LCC compiler which includes the source code for the compiler. There are also repositories of old C compilers at The Unix History Society archive (tuhs.org).
Still another angle you could take is to examine the language False (an ancestor of the more famous Brainfuck) which is designed to be implemented with very little code.
Another angle, which connects to your interest in complexity theory, is to learn about the Chomsky Hierarchy of languages and the associated abstract machines which can parse them. This will teach you why Lex and Yacc are separate tools and what each is good for (and how to do it yourself and not need them at all).
I am actually on the very same quest myself. I'm currently reading the old 1979 book Anatomy of Lisp which contains compiler code in, of course, Lisp. But this is ok, because I already have my own homebrewed lisp interpreter to execute it with.
The Tiger language has been designed by prof. Andrew Appel exactly on purpose to illustrate, step-by-step, a full compiler construct process.
You can google for 'tiger language' and read some online resource, there are also some questions/answers here on SO, but the better choice would be to get a copy of the book for the language you prefer, and implement the parts you're most interested into.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've recently been very interested in compilers and how they work. Since gcc has it's source available, I figured it would be the best material to study.
The first thing I realized is that it would be pointless to study gcc if I didn't have a basic understand of simple compiler design principles. I have since been diligently reading the "Dragon Book" which, from what I have seen, is the de facto book on compiler implementation.
None-the-less, reading that book has only furthered my desire to learn about compilers such as gcc.
Additionally, I find it pertinent to say that I do have a intermediate understanding of c/c++ (aka, I'm not trying to study gcc without knowing c). I am hoping that studying gcc will help me improve upon that as well.
I have downloaded the latest build I could find; however, I get lost when perusing the source code.
What I'm looking for are suggestions on how to proceed. Is there a similar project, which is not so massive, I could use as a stepping stone to gcc? Is there a particular module of gcc which one would recommend studying first? Are there any books which go into gcc's implementation, rather than it's use? Perhaps I should stop whining and just keep reading the source until it clicks?
Any and all feedback will be greatly appreciated.
EDIT: If you think I should study a different compiler/interpreter, I would greatly appreciate suggestions as to which ones.
If you want to look at a very tiny compiler, I would recommend Fabrice Bellard's Tiny C Compiler.
Also worth mentioning, Fabrice Bellard won the obfuscated c code contest with his Obfuscated Tiny C Compiler. There's a deobfuscated version as well, and it fits in a single c file.
These should be great if you want something small and manageable to learn from.
I would definitely look at clang/LLVM. I think the code base is very readable. One very viable option you'd have is to use LLVM as a back end and write your own simple lexer and parser.
I think it's good to read the book "ruby under a microscope" and practise with ruby core development, before reading gcc's code. But you should need knowledge on ruby programming. It's about ruby internels.
As I know the best book on gcc is "the definitive guide to gcc" https://www.amazon.com/Definitive-Guide-GCC-Guides-Paperback/dp/1590595858. Although it is little bit old, I think you should read this.
Passionate about compilers too, I learned a lot from Niklaus Wirth's book Algorithms + Data Structures = Programs. One of the last chapters described the Pascal-0 languages, and the previous chapters show how to parse and compile a very minimalistic language. Pascal-0, PL/0 are two-step compilers, they generate p-code, which is 'machine code' for a minimalistic virtual machine (not unlike Java).
This page describes a PL/0 virtual machine instruction set, and, at the very end, links to a PL/0 compiler and other interesting info.
Niklaus Wirth has always had a knack for writing readable and well-structured code. Here's the language definition and many other interesting links.
The advantage of studying and using Pascal, is that the language is very structured, and not an evolution from Assembler (like C). It makes compiling much easier. It's not even necessary to do several passes...
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm interested to know how programming languages are interpreted by machines. I was looking at some language comparison/benchmarks and noticed that some programming languages are written in the C programming language while others were written in others. For example Ruby and JVM (although not a language) are written in C. But why did the people who wrote Ruby or JVM write it in C? Couldn't they write it in a way like C, which , I guess, wasn't written in another language. :-) Was it just because not to reinvent the wheel or is C the god of machines?
One has to write them in something. They could be written directly in machine language (actual processor instructions) but that would be very cumbersome - and not portable. So another - preferably standard, portable and ubiquitous - language (like C) is a much better option.
C (and C++, FORTRAN, etc) is compiled directly to machine code, while Ruby and Java are compiled to bytecode which is interpreted by a virtual machine, which is like a software platform on top of the hardware.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
C++ is often touted as the evolution of C, which it is not. To draw an analogy for the kind of language I'm looking for:
Perl, Python, Ruby, Groovy
C++, D
Java, C#
C, Fortran, Modula-2?, Pascal?, Go?, Rust?
Do any proposed, or implemented languages fit in the same (enormous) niche as C, with the intention of being an alternative, while maintaining all the applicability to OS, high performance, embedded and other roles?
I think the problem with this question and subsequent discussion is that very rarely are languages intended to replace others. Sure, as you attempted to clarify and defend some of your groupings, they share similar feature sets or accomplish similar things, but ultimately, I don't think anybody is going to sit down and write a language that is designed to replace another. What is far more likely is that language designers want to accomplish a goal and will pick and choose aspects of certain languages that already do that. For instance, take a look at Go which is designed to up the ante a bit with regards to ultra-high performance systems programming which, arguably, competes with C a bit. However, if you look at the FAQ on the mission of the project, they aren't seeking to replace C, but simply augment it and address issues it faces (such as dependency management to name one) (so perhaps this is the answer you're looking for).
So really, whether something is an evolution of something else, depends entirely on the perspective from which you examine and evaluate the evolution. C++ can be considered an evolution of C because it introduced a new feature set (OO programming) that many consider a step forward in systems design while still retaining a similar syntax and compatibility with C code. However, it is not entirely an evolution because it is also missing some features that make C a language currently used today. No one language can do everything (except for maybe Lisp ;-) I kid, I kid) and never will. No one language is without tradeoffs. The abstractions that make web development easy are the same abstractions that make low-level systems development impossible (or prohibitively difficult). So I really think it's hard to say X is an evolution of Y. Rather, I would say "I want to do W and I know I can use language X but is there something that accomplishes my goals of A, B, and C better?" Unfortunately programming languages aren't black and white enough to make sweeping statements like that and like every decision you make in programming, it's about trade-offs.
The following picture will say more than I could write:
Maybe Google's Go language will be. At least, that's what I'd expect Google's Go team hopes it will become.
Pascal would be an equivalent, so would Modula-2.
Mozilla also has a new language called Rust, this is a really nice modern language that is targeted at the same kinds of applications as C/C++
https://www.rust-lang.org/
C has more than one niche, really. For low-level systems programming, C replaced platform-specific assembly languages, and nothing has really challenged C in that domain. C++ would be the only other possible candidate, there (BeOS, many device drivers, etc.).
C was/is also used a lot for high performance numerical code. In that domain, FORTRAN still has the edge, and there are many other challengers (C++, Matlab, Numpy, Fortress, Scala).
At some level, C is the lowest common denominator that has cross-platform portability.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In what language was MSDOS originally written in?
The Wikipedia Article implies either C, QBasic or Pascal, but:
C was invented to write UNIX, so I don't believe it was used to write MSDOS
Pascal seems popular to teach programming, but not really popular to write Operating systems in
QBasic didn't seem to be very popular for Operating Systems at the time MSDOS was developed (or was *BASIC ever very popular to write Operating Systems in it?)
Except these three languages there is also Assembly, but I assume that Microsoft already switched from Assembly to a "higher" level language?
Since C was originally invented for UNIX, I still wouldn't think Microsoft is using C... although the Microsoft API is written in C (I find this kind-of oxymoronic, actually).
Can anyone enlighten me on this topic?
http://answers.google.com/answers/threadview?id=197874
Since CP/M was written in FORTRAN and
QDOS was based on CP/M, does it mean
that QDOS and MS-DOS were written in
FORTRAN? According to our next
article, written by Tim Patterson
himself, the assembly language used by
Seattle Computer Products wasn't
FORTRAN but was built in-house since
it was the only thing available to
them at that time.
"The last design requirement was that
MS-DOS be written in assembly
language. While this characteristic
does help meet the need for speed and
efficiency, the reason for including
it is much more basic. The only 8086
software-development tools available
to Seattle Computer at that time were
an assembler that ran on the Z80 under
CP/M and a monitor/debugger that fit
into a 2K-byte EPROM (erasable
programmable read-only memory). Both
of these tools had been developed in
house."
"An Inside Look at MS-DOS"
http://www.patersontech.com/Dos/Byte/InsideDos.htm
Well, MS-DOS was originally a renamed 86-DOS, and 86-DOS was written in assembly if I'm not mistaken, so that would make ASM the original language for MS-DOS as well.
As stated on http://www.patersontech.com/Dos/Byte/InsideDos.htm
"The last design requirement was that MS-DOS be written in assembly language."
(Note that alot of appllications, not just operating system parts, were written in assembly back then.)
See the timeline
Assembler source of 86DOS
Documentation
Unix pre-dates MS-DOS, so that's not an impediment for it to be programmed en C. But I'd go for the assembly for most parts at least...
If you look for MS-DOS on some websites, you can find the version 6 with the source code included. It was written in Assembler and there's no C code at all. All the utilities, kernel, and even installer was written in assembler.
And regarding Windows, it has a lot of assembly language on it but some parts where writting in C and then C++.