(The Best) IDE for working with Lex and Yacc/Bison

It’s painful to code something without an adequate IDE isn’t it? If you have been searching for the best IDE for Yacc/Lex, I think we faced the same challenges. But just to save your time: for Linux/macOS users: try Vim. I personally use CLion and Xcode, though.

It seems like there’s no IDE offering proper syntax highlighting, indentation, and code intellisense for editing both Lex and Yacc. It might be because not many people edit Yacc and Lex day-to-day as part of their work. I myself won’t be coding them if it’s not for the compiler class I am taking this semester.

Or, maybe, it’s just difficult if not bordering impossible to come up with the perfect grammar thingy (like, tmLanguage JSON file) so that color highlighter, indentation and so on would just work™, considering that Lex and Yacc each use a host language such as C in addition to their own constructs, effectively making them to be just as difficult as HTML to be parsed with regular expressions.

Anyway, anything is better than Notepad certainly, no!?

I have tried Vim, NeoVIM, Textmate, Emacs/Spacemacs, Visual Studio Code, Sublime, XCode, the Visual Studio, Atom, and CLion. I’d say: Vim is probably the best overall, albeit there are some irritating issues in Vim. So, be prepared 🙂

Editing Yacc and Lex in Vim

Editing Yacc and Lex in Vim

Especially when editing Yacc files. Vim is inconvenient as the indentation just didn’t work. For example, when I type if something something, and then pressed enter, I thought the cursor would be indented but it didn’t. Strangely tho, Lex did not exhibit this problem at all. It was smooth sailing with Lex files.

For other IDEs, you may skip CLion and the macOS version of Visual Studio as they do not recognize Lex and Yacc files at all. Maybe, Visual Studio will behave differently on Windows, but I couldn’t verify. The macOS version of Visual Studio does not support C/C++ ; only C# and mobile.

Editing a Lex file like a Text file on macOS Visual Studio

Editing a Lex file like a Text file on macOS Visual Studio

A JetBrains user 9 years ago has asked for the much-needed Lex and Yacc support. But… their representative couldn’t do much as “there is pretty small interest in the topic.”

It’s rather surprising. No doubt. Many would expect CLion and other paid JetBrains IDEs to have some kind of support, at least syntax highlighting and proper indentation if nothing else, since those products are aimed for professionals. I hope someday they’ll support Yacc and Lex.

Anyway. Yeah, we can simply cross them out for now. But I still use CLion nevertheless, please continue reading.

Inconsistent syntax highlighting on Emacs

Inconsistent syntax highlighting on Emacs

Now, between VSCode, Sublime, Textmate, Emacs/Spacemacs and Atom, I really can’t choose. All of them offered only nominal support. They all have serious issues with syntax highlighting, issues which Vim has none. Sad.

Onto XCode… surprisingly, it checked the decent syntax highlighting requirement. And, this one not surprisingly, it has the most pleasing user interface to edit those Lex/Yacc files with. Although, I find the fact that I can’t have access to the terminal from within the IDE a rather painful experience.

Editing a Lex file in XCode

Editing a Lex file in XCode

But, I still admire this IDE’s look and feel.

Editing Yacc on XCode

Editing Yacc on XCode

Pretty UI asides, the experience of editing Yacc files in XCode warrant some betterment. For example, let’s consider we have the following rules:

1
2
3
screens: /* empty */
       | screens screen
       ;

If I wanted to add an action next to the screen non-terminal on the second line, Xcode will indent the whole line incorrectly upon seeing the opening brace ({) …to be:

1
2
3
screens: /* empty */
       | screens screen {
       ;

Not only that, the syntax highlighter for C (within Yacc files) is buggy. I noticed how some function calls were highlighted with blue while others did not. It happened occasionally, and when I overwrite the function with the same name, it’s highlighted correctly. I also could not replicate this kind of issue after restarting the IDE.

So yeah, Vim is generally the most sensible choice overall. If it’s the only choice available.

But indeed, there’s no single best IDE.

Xcode exhibited issues with rules/productions indentation, as does Vim with the subroutines part. Personally, I simply turned off XCode’s automatic indentation to help it stop from being that much annoying. In that way, XCode has supplanted Vim for me.

I’d then use CLion to edit C files as it has the best-in-class intellisense for my C files.

If I were a Windows user, however, I’ll not miss giving Visual Studio a try. It seems Visual Studio on Windows has excellent support for C/C++ in addition to Lex and Yacc.

In any case, they are all better than Notepad.

Good luck.