orangeduck / mpc

A Parser Combinator library for C
Other
2.67k stars 292 forks source link

String recognition problem #114

Closed peacefulpixel closed 5 years ago

peacefulpixel commented 5 years ago

So, my program looks like:

#include "../mpc.h"

int main(int argc, char **argv) {
    mpc_parser_t *Expr  = mpc_new("expr");

    mpca_lang(MPCA_LANG_PREDICTIVE,
        " expr : /^/ \"i1\" | \"i2\" /$/ ; ",
        Expr, NULL);

    mpc_result_t r;
    if (mpc_parse_contents(argv[1], Expr, &r)) {
        mpc_ast_print(r.output);
        mpc_ast_delete(r.output);
    } else {
        mpc_err_print(r.error);
        mpc_err_delete(r.error);
    }

    mpc_cleanup(1, Expr);
    return 0;
}

Then if i try to parse input like this:

i1

I got output:

> 
  regex 
  string:1:1 'i1'

But if i try to parse

i2

I got an error message:

test11:1:2: error: expected "i1" or "i2" at '2'

So i can't catch what's wrong with my program

orangeduck commented 5 years ago

Your grammar isn't predictive so if you remove the MPCA_LANG_PREDICTIVE flag it should work.

peacefulpixel commented 5 years ago

I set flag MPCA_LANG_DEFAULT and now it's work. Thanks!