Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think, you cannot beat beats shift-reduce LALR(1) with recursive descent on raw performance on a reasonable syntax. However, parsing C++ with LALR(1) will require unmaintainable hacks in the grammar. Recursive descent accomodates ad-hoc fudging much better than parser generation. In any given rule, you can look ahead as much as you want and invoke any Turing computation to decide the parse. Also, recursive descent approaches support backtracking fairly naturally: try it this way, as far ahead as necessary, or else start again, falling back on this. Let's see ...

  28374 /* Commit to the currently active tentative parse.  */
  28375 
  28376 static void
  28377 cp_parser_commit_to_tentative_parse (cp_parser* parser)
  28378 {
  28379   cp_parser_context *context;
  28380   cp_lexer *lexer;
  28381 
  28382   /* Mark all of the levels as committed.  */
  28383   lexer = parser->lexer;
  28384   for (context = parser->context; context->next; context = context->next)
  28385     {
  28386       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
  28387         break;
  28388       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
  28389       while (!cp_lexer_saving_tokens (lexer))
  28390         lexer = lexer->next;
  28391       cp_lexer_commit_tokens (lexer);
  28392     }
  28393 }
Bison supports GLR ("generalized LR") via some technique of forking the parser and trying alternatives in parallel, which seems related to this.


The problem with C++ grammar is that inside templates, things are too flexible at times. For example:

    template<bool> struct a_t;

    template<> struct a_t<true> {
        template<int> struct b {};
    };

    template<> struct a_t<false> {
        enum { b };
    };

    typedef a_t<sizeof(void*)==sizeof(int)> a;

    enum { c, d };
    int main() {
        a::b<c>d; // declaration or expression?
    }
It's a fun C++ IDE torture test, which few pass. One that does is Visual Studio - if you create a C++ project and set up both 32-bit and 64-bit build targets, you can flip between them, and see the syntax highlighting change on the line with the comment.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: