Skip to content

Ocamlyacc: overflow with 128 or more parsers #9207

Closed

Description

@andreasabel

ocamlyacc produces ill-formed .ml files if a grammar has 128 or more entrypoints.
This generated .ml file was produced by ocamlyacc from a BNFC-generated .mly file:

...
let pSuper (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =
   (Parsing.yyparse yytables 126 lexfun lexbuf : Abstest.super)
let pSwitch (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =
   (Parsing.yyparse yytables 127 lexfun lexbuf : Abstest.switch)
let pSynchronized (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =
   (Parsing.yyparse yytables -128 lexfun lexbuf : Abstest.synchronized)
let pTemplate (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =
   (Parsing.yyparse yytables -127 lexfun lexbuf : Abstest.template)
...

ocamlc reports a type error:

File "Partest.ml", line 5129, characters 4-28:
5129 |    (Parsing.yyparse yytables -128 lexfun lexbuf : Abstest.synchronized)
           ^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type
         int -> (Lexing.lexbuf -> 'a) -> Lexing.lexbuf -> 'b
       but an expression was expected of type int

The buggy .ml code is produced by this print statement:

ocaml/yacc/reader.c

Lines 1701 to 1703 in 4e6e912

fprintf(entry_file,
"let %s (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =\n (Parsing.yyparse yytables %d lexfun lexbuf : %s)\n",
bp->name, bp->entry, bp->tag);

Seems like the entry component of the bucket type was (needlessly?) dimensioned to bytesize, not catering for values >=128:

ocaml/yacc/defs.h

Lines 137 to 150 in 4e6e912

struct bucket
{
struct bucket *link;
struct bucket *next;
char *name;
char *tag;
short value;
short index;
short prec;
char class;
char assoc;
char entry;
char true_token;
};

I attach a .mly file created from this MWE BNFC file:

EInt. Exp ::= Integer;
coercions Exp 127;

Par128entrypoints.mly.zip

Activity

  1. nojb commented on Dec 24, 2019

    @nojb
    Contributor

    Thanks for the report. Did you try patching ocamlyacc by changing char entry to short entry and trying it on your grammar? I'm wondering if there are more places where this assumption on the number of entries is made...

  2. dra27 commented on Dec 24, 2019

    @dra27
    Member

    In this use case, are you able to use Menhir instead? Unless the fix is trivial, it’d seem better to alter ocamlyacc merely to display an error message if an attempt is made to generate more than 127 entry points.

  3. andreasabel commented on Dec 24, 2019

    @andreasabel
    ContributorAuthor

    If I supply a grammar with 256 entry points (or more) ocamlyacc complains:

    File "Par256entrypoints.mly", line 21: more than 256 entry points
    

    (Note the slight in accuracy in the error message: correct would be more than 255 entry points.)
    Thus, it seems that the idea was to allow 255 entry points, but printing char as a signed byte with %d below reduces this to 127 entry points.

    ocaml/yacc/reader.c

    Lines 1701 to 1703 in 4e6e912

    fprintf(entry_file,
    "let %s (lexfun : Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =\n (Parsing.yyparse yytables %d lexfun lexbuf : %s)\n",
    bp->name, bp->entry, bp->tag);

    In this use case, are you able to use Menhir instead?

    Well, I stumbled on this problem while trying to make BNFC more robust. BNFC's ocaml backend currently only targets ocamllex/ocamlyacc, writing a new backend to target Menhir is probably the long-term viable process.

    Did you try patching ocamlyacc ...?

    No, I have not familiarized myself with the ocaml build process...

  4. nojb commented on Dec 24, 2019

    @nojb
    Contributor

    No, I have not familiarized myself with the ocaml build process...

    It is actually quite easy:

    git clone https://github.com/ocaml/ocaml
    cd ocaml
    ./configure
    cd yacc
    make
    

    (ocamlyacc lives in the yacc subdirectory). The above creates an yacc/ocamlyacc executable. If you modify the source in the yacc directory just repeat the make step (in the yacc subdirectory) to rebuild the ocamlyacc executable.

  5. andreasabel commented on Dec 25, 2019

    @andreasabel
    ContributorAuthor

    Thanks, I will work on a PR then.

  6. added a commit that references this issue on Dec 25, 2019
    602d36b
  7. added 2 commits that reference this issue on Jul 12, 2026
    982d8ce
    1081df9
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions