Menhir parsing elements tuples

109 views Asked by At

Hello I have a work when I have to do tuples of elements with parser but it gives me cyclic grammar error any way to do it proprely?

elements: | separated_nonempty_list(SEPARATOR_TOKEN,elements)

I'm looking for something like that:

elements (SEPARATOR_TOKEN) elements... (list)

Thank you

1

There are 1 answers

0
coredump On

If your elements are either tuples (comma-separated lists) or single-characters terminals, "a,b,c,d" could be parsed as (a, (b, c, d)), (a, b, (c, d)), (a,(b,(c,(d)))), ((((a),b),c),d) etc., where I marked the tuples with parentheses.

It is a cyclic grammar error because in order to parse a tuple, you may need to parse a tuple first (you could parse it if you had a backtracking mechanism in place where you want to try different ways of parsing a string, but in your case you want a deterministic grammar).

You should have another non-terminal named for example atom (or atomic_element), and make sure your elements can be a list of atoms.

If you want elements to recursively contain elements, you have to add a layer of disambiguation with e.g. parentheses:

atom : <single-letter>
     | '(' elements ')'