7

This blog post https://stackoverflow.blog/2020/04/20/brush-up-your-cobol-why-is-a-60-year-old-language-suddenly-in-demand/?cb=1 says

"You can’t write a compiler or a kernel module in COBOL"

I'm not a fan of COBOL and I'm not suggesting it would be a good idea but as COBOL is turing-complete then my understanding is that you should be able to write in it any program that any other turing-complete language can write. Which means it should in theory be possible to write a compiler in COBOL, no?

Perhaps the author is just short-cutting saying it's not usual or a good idea to write a compiler in COBOL but it got me wondering.

Caltor
  • 2,538
  • 1
  • 27
  • 55
  • 2
    I believe you are correct. You could, though no one would want to. – 500 - Internal Server Error Apr 21 '20 at 13:38
  • It's an easy assertion to make but all it would take is a single example of a compiler written in COBOL to prove it to be false. My COBOL is pretty rusty but I can't think of any fundamental reason why it would not be possible. – jwh20 Apr 21 '20 at 13:57
  • FWIW the article at an earlier point says "You'd never want to write a compiler in COBOL". So the article itself is sending a bit of a mixed signal. – TylerH Apr 21 '20 at 14:47
  • Clearly there's an example (see Rick Smith's answer). – Ira Baxter Apr 21 '20 at 18:42
  • It is possible, but economically infeasible, although apparently MicroFocus provides a counter-example. – user207421 Apr 23 '20 at 10:18

4 Answers4

10

Micro Focus was founded in 1976. Their first COBOL compiler was bootstrapped using SNOBOL until they had a COBOL compiler written in COBOL. Even the utilities were written in and compiled with COBOL. Later they began incorporating C and C++, but for a while everything was COBOL. [I used Micro Focus COBOL on PCs beginning in 1984.]

In the simplest terms, a standard COBOL program can be written to read and write files one byte-at-a-time. What happens in the PROCEDURE DIVISION determines what type of program it is: editor, compiler, linker, etc.

Rick Smith
  • 3,962
  • 6
  • 13
  • 24
  • That's impressive and amazing to know it has already been done. Nice little history lesson too, thanks. I had never heard of SNOBOL before. – Caltor Apr 23 '20 at 14:24
7

What didn't get answered is whether you could write an (OS) kernal in COBOL.

The answer is (drumroll...)

Sure, in exactly the same sense you can write an OS kernal in C. See my Quora article on what it takes to do this: Is it possible to write an operating system kernel without using an assembly language? (No, I'm not going to copy that entire answer here).

You'd probably be considered crazy if you did.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • That's really interesting. I didn't even bother asking that question because I just assumed it would be impossible for other technical reasons. :D – Caltor Apr 23 '20 at 14:26
3

Okay, as the actual author, I'll admit that was an oversimplification, for the simple reason that COBOL is Turing-complete and thus you can write ANY program in it — eventually. You could, for example, write an Assembler in COBOL, use the Assembler to write a C compiler, and then bootstrap a C compiler in C. Same thing goes for kernel loadable modules. Eventually.

It would be a crazy approach suitable for someone with waaaaay too much time on their hands, but it could be done.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • 1
    Great to have your input (and confession!) here Charlie ;) Hope you weren't offended by this question. It just got me curious. Maybe someone in "lockdown" fancies the challenge! BTW COBOL was the main reason I dropped out of college in the second year :D I couldn't believe in the 90s that they still wanted to teach me a language that required leaving 7 spaces at the start of each line in case it was input on a punch card. – Caltor Apr 27 '20 at 19:15
  • 1
    @Caltor Not offended, just a cranky old guy. I think the key thing for modern readers is that COBOL is what we call a *domain-specific language* — although the term hadn't been invented yet. The thing is, I've been trying to think of what would make a modern business-oriented language, and it keeps looking a fair bit like COBOL. Hmmm... that might make an article in itself. – Charlie Martin Apr 28 '20 at 14:19
  • I'll keep my eyes open for MOBOL (modern business-oriented language) then! :D Please don't make me leave 7 spaces at the start of each line though. – Caltor Apr 28 '20 at 16:41
  • 1
    Yeah. It's going to want to be a more traditional block-structured and free-format language — which you actually can do with COBOL now but most of the legacy code is still in card-images. @Caltor – Charlie Martin Apr 28 '20 at 18:52
2

You can see an implementation at https://rosettacode.org/wiki/Compiler/AST_interpreter#COBOL

Poke around the rest of the compiler task suite solutions that Steve Williams wrote up as a set of COBOL entries.

He pulled off all steps in the

  • Lexical Analyzer task
  • Syntax Analyzer task
  • Code Generator task
  • Virtual Machine Interpreter task
  • AST Interpreter task

A mini C compiler, interpreter, VM. Works the charm. See https://rosettacode.org/wiki/Compiler/syntax_analyzer for the language grammar. Assignment, strings, while, if, print, putc, math expressions and comparisons. Squiggly brace blocks. I'm pretty sure the Compiler task suite is the Rosetta Code sweet spot.

Testing sample is

count = 1;
 while (count < 10) {
     print("count is: ", count, "\n");
     count = count + 1;
 }

The COBOL sources all just work as advertised when built with GnuCOBOL. You end up with an easy to manage and extend programming language, compiled or interpreted. The various solutions listed on Rosetta Code are worth the time spent reading, and trying out.

Brian Tiffin
  • 3,978
  • 1
  • 24
  • 34