You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ocamllex cannot handle large regexes (with small minimal automaton) #9964
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.
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
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
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
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.
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...
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.
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.
Activity
[-]ocamllex: Cannot handle large regexes (that lead to small automaton)[/-][+]ocamllex cannot handle large regexes (even when having small minimal automaton)[/+][-]ocamllex cannot handle large regexes (even when having small minimal automaton)[/-][+]ocamllex cannot handle large regexes (with small minimal automaton)[/+]xavierleroy commentedon Oct 8, 2020
By default,
ocamllexgenerates 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 causeocamlcand especiallyocamloptto overflow the stack or take forever to compile.The usual way to handle block comments is via lexer states indeed. Something like
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.andreasabel commentedon Oct 12, 2020
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...
github-actions commentedon Oct 15, 2021
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.
xavierleroy commentedon Oct 15, 2021
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.
andreasabel commentedon Oct 15, 2021
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.