2

How is a syntax checker for a programming language written? I am guessing a grammar has to be written first. How to then proceed?

Motivation

I'm using a programming language (Apex for Salesforce) that is compiled in the cloud and a list of errors is sent back to the basic Eclipse editor. I'd like to skip the process for simple typing mistakes and just check for them locally.

3
  • A tool like Treetop seems like it would be very useful here. Commented Aug 23, 2013 at 16:50
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Aug 23, 2013 at 18:25
  • You're right so far. First define the rules, then check them.
    – ott--
    Commented Aug 23, 2013 at 19:06

1 Answer 1

3

You're right so far. In all cases, you have to have a rigorous description of the syntax of the language (a "grammar") before you can build something that can take something allegedly in that language and determine if it complies with the syntax rules (a "parser").

The parser is the front end of a compiler. Code generation is the back end.

There are dozens of books out there on compiler construction. Most of them go into great detail on parsing techniques. Some deal only with the back end.

The best and most accessible book I've found for a beginner is Nicklaus Wirth's "Compiler Construction". Available free. You'll have to translate his source code from Oberon (a simple descendant of PASCAL and Modula-2) into your language of choice.

Jack Crenshaw's series, "Let's Build A Compiler", although incomplete, is almost as good, and just as accessible.

There are other tools. ANTLR is popular.

2
  • Yep. An intelligent editor is essentially a compiler minus the code generation. Note that to get anything which even remotely resembles the features of a modern IDE, you need much more than just the parser. You really need the full compiler minus the actual code generator: lexical analysis, syntactic analysis, semantic analysis, type inference, type checking etc. Commented Aug 24, 2013 at 2:55
  • @JörgWMittag, the problem I have with EVERY "modern" IDE I have encountered is that NOT ONE of them contains an editor that even BEGINS to approach being competitive with GNU Emacs. There's a REASON I've been using GNU Emacs almost exclusively since 1988. Actually, there are a lot of reasons. (I occasionally have to deal with clue-impaired managers who do not understand that quality tools are just as important for software guys as they are for hardware guys.) Commented Aug 24, 2013 at 3:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.