A syntax-analysis question (when you want something quick-and-dirty)

I’m a Perl bigot, and … well, it is what it is. Recently I’m working on a problem that requires me to parse files output from ANTLR, and because it’s a tiny, tiny part of my project, I wrote the code to do that as if I were writing Perl: that is, with heavy, heavy, heavy use of regexps. I wondered if anybody here had any comments on how they would do it differently, just so I can try to stay open-minded.

Here’s the datatype I want to parse the file to

type t = {
    token_literal_names : string option list
  ; token_symbolic_names : string  option list
  ; rule_names : string list
  ; channel_names : string list option
  ; mode_names : string list option
  ; atn : int list
  }

and here’s an example of the input I need to parse (there are probably a thousand such files)

token literal names:
null
'+'
'-'
'*'
'/'
'('
')'
null
null

token symbolic names:
null
null
null
null
null
null
null
NUMBER
WS

rule names:
expr
term
factor


atn:
[4, 1, 8, 32, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 1, 0, 1, 0, 1, 0, 5, 0, 10, 8, 0, 10, 0, 12, 0, 13, 9, 0, 1, 1, 1, 1, 1, 1, 5, 1, 18, 8, 1, 10, 1, 12, 1, 21, 9, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 30, 8, 2, 1, 2, 0, 0, 3, 0, 2, 4, 0, 2, 1, 0, 1, 2, 1, 0, 3, 4, 32, 0, 6, 1, 0, 0, 0, 2, 14, 1, 0, 0, 0, 4, 29, 1, 0, 0, 0, 6, 11, 3, 2, 1, 0, 7, 8, 7, 0, 0, 0, 8, 10, 3, 2, 1, 0, 9, 7, 1, 0, 0, 0, 10, 13, 1, 0, 0, 0, 11, 9, 1, 0, 0, 0, 11, 12, 1, 0, 0, 0, 12, 1, 1, 0, 0, 0, 13, 11, 1, 0, 0, 0, 14, 19, 3, 4, 2, 0, 15, 16, 7, 1, 0, 0, 16, 18, 3, 4, 2, 0, 17, 15, 1, 0, 0, 0, 18, 21, 1, 0, 0, 0, 19, 17, 1, 0, 0, 0, 19, 20, 1, 0, 0, 0, 20, 3, 1, 0, 0, 0, 21, 19, 1, 0, 0, 0, 22, 30, 5, 7, 0, 0, 23, 24, 5, 5, 0, 0, 24, 25, 3, 0, 0, 0, 25, 26, 5, 6, 0, 0, 26, 30, 1, 0, 0, 0, 27, 28, 7, 0, 0, 0, 28, 30, 3, 4, 2, 0, 29, 22, 1, 0, 0, 0, 29, 23, 1, 0, 0, 0, 29, 27, 1, 0, 0, 0, 30, 5, 1, 0, 0, 0, 3, 11, 19, 29]

and here’s a pointer to the code I wrote: antlr-ocaml/lib/interp_syntax.ml at master · chetmurthy/antlr-ocaml · GitHub

So again, the question: how would you do this differently?

I guess I would probably use sedlex (GitHub - ocaml-community/sedlex: An OCaml lexer generator for Unicode · GitHub) as a lexer (and since it uses regexps, you’d like it!), and then, since the format is so simple, just hand write a parser, since it just needs to pick up the section separator keywords