Skip to content

ocamllex cannot handle large regexes (with small minimal automaton) #9964

Closed

Description

@andreasabel

BNFC is a tool to generate parsers from a LBNF file, e.g. producing input for ocamllex and ocamlyacc/menhir.
Here is a toy LBNF file:

EInt.  Exp ::= Integer;
EPlus. Exp ::= Exp "+" Integer;

comment "anananas" "anananas";

This specifies a grammar for +-expressions. Further, block comments are begun and ended by the string anananas.

BNFC-2.8.4 (current master) generates a large regular expression
Lex.mll.txt
that ocamllex cannot process (BNFC/bnfc#202):

$ ocamllex Lex.mll
1548 states, 169242 transitions, table size 686256 bytes
transition table overflow, automaton is too big

However, Haskell's lexer generator Alex can handle such large regular expressions (munches a while on them).

Shouldn't it be possible to optimize the produced automaton? In the end, the minimal automata (both deterministic and non-deterministic) for recognizing anananas are rather small.

There might also be a better way to recognize block comments, via lexer states, not going through regular expressions. I'd be happy for advice.

Activity

  1. changed the title [-]ocamllex: Cannot handle large regexes (that lead to small automaton)[/-] [+]ocamllex cannot handle large regexes (even when having small minimal automaton)[/+] on Oct 8, 2020
  2. changed the title [-]ocamllex cannot handle large regexes (even when having small minimal automaton)[/-] [+]ocamllex cannot handle large regexes (with small minimal automaton)[/+] on Oct 8, 2020
  3. xavierleroy commented on Oct 8, 2020

    @xavierleroy
    Contributor

    By default, ocamllex generates tables for a table-driven automaton, and indices into the tables are 16-bit integers, so 169242 transitions cannot be addressed. "64k transitions ought to be enough for anybody."

    You could try ocamllex -ml, which generates the automaton as recursive OCaml functions instead. But you'll end up with 1548 mutually-recursive functions which could cause ocamlc and especially ocamlopt to overflow the stack or take forever to compile.

    The usual way to handle block comments is via lexer states indeed. Something like

    rule main = parse
    | "anananas"  { comment lexbuf }
    | ...
    
    and comment = parse
    | "anananas"  { main lexbuf }
    | eof  { raise (Error "unterminated  comment") }
    | _ { comment lexbuf }
    

    That's extremely efficient and can even handle nested block comments shall your language support them.

    Concerning automaton compression, that's something we could attempt indeed. In the general case, ocamllex automata are DFAs with "memories", to handle named groups (regexp as var), and I'm not sure how to minimize these. In your particular example, there are no named groups so standard DFA minimization applies.

  4. andreasabel commented on Oct 12, 2020

    @andreasabel
    ContributorAuthor

    Thanks for your quick reaction, Xavier!
    Looks like I should try to modify the BNFC Ocaml backend to use lexer states.
    The rest (DFA minimization in the absence of named groups) is then maybe more of academic interest...

  5. github-actions commented on Oct 15, 2021

    @github-actions

    This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

  6. xavierleroy commented on Oct 15, 2021

    @xavierleroy
    Contributor

    I'll take the liberty to close this PR. It's objectively better to keep the automaton small by using lexer states. Please holler if you disagree.

  7. andreasabel commented on Oct 15, 2021

    @andreasabel
    ContributorAuthor

    Well, this is of course your decision! I raised the issue to alert of a problem, but workarounds exist, so this is entirely optional...

    I must confess that I haven't changed BNFC yet, hoping for a miracle upstream, but I also haven't gotten any user complaints. So the issue might be just academic for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions