Exia0890 Exia0890 - 4 years ago 61
C Question

Left-recursive error with my C grammar

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download