0
votes

I have a left-recursive error with my C grammar which can be found here http://www.archive-host.com/files/1959502/24fe084677d7655eb57ba66e1864081450017dd9/cAST.txt. When I replace

initializer
 : assignment_expression
 | '{' initializer_list '}'
 ;

with

initializer
 : assignment_expression
 | '{' initializer_list '}'
 |  initializer_list
 ;

I did this because I am trying to do this code in Ctrl-D

int k [2] = 1,4;

However this code does work with the first version

int k [2] = {1,4};

Is there a way to do without the { } please?

1
Are you writing a standard C grammar?! int k [2] = 1,4; isn't C. - masoud
Yes, but this will be the only exception. - Exia0890

1 Answers

3
votes

To do this, you'd need to introduce context sensitivity (or something on that order).

The problem is that 1,4 already has a defined meaning. It's an expression using the comma operator that evaluates the 1, discards the result, then evaluates the 4, which is the value of the expression as a whole.

As such, to make this work, you'd have to use a different syntax for initializers than for normal expressions (and in the process, depart pretty widely from C as it's current defined). From a purely grammatical viewpoint, that almost certainly does not need to be done with context sensitivity, but it will involve basically defining the syntax for initializers separately from/in parallel with the syntax for normal expressions, instead of using a common syntax for both.