1
votes

I'm playing with bison and flex to understand the way how interpreters work. I've already done several things like range ({INT:INT} or {INT:INT:INT}), strings ({string"here comes any text"}), etc., but I can't decide which should be right way to help bison understand if user entered array in form [INT, INT, ID, ID, INT, ..., INT, ID, ID...]?

The first what comes in mind is to use flex keyword BEGIN and then look till the end of line, but that seems to stupid since you need a lot of stuff like to understand if next member is INT or ID, and it is not such good way. What can I do else? I'd rather prefer to do it with bison, but I don't know how to make bison look recursive between [ and ] to grab all what they contain. How can I do it?

Thanks in advance!

1
Do you know how to construct an LR(1) Context Free Grammar to recognize such an array? Or is your problem how to convert it to a format that Bison understands?Yakk - Adam Nevraumont
Don't know. Probably you can recommend some books or links to read?ghostmansd
The Cinderella Book: Introduction to Automata Theory, Languages, and Computation. The 4 editions (including the "0th") are apparently different in approach to a certain degree: en.wikipedia.org/wiki/… -- I am not certain, but I think that covers CFGs and LR(1) grammars etc, if possibly from a too-abstract angle. Do you have much formal computer science theory in your background?Yakk - Adam Nevraumont
@Yakk: no, I don't have it at all, since I'm linguist. :-)ghostmansd
@DeadMG, do you have an alternate text to suggest?Yakk - Adam Nevraumont

1 Answers

3
votes

Simple- just make a recursive rule.

expr :- INT | ID;
array_expr :- array_expr expr | expr;
array :- '[' array_expr ']';

It's been a while since I did Bison, but that should be pretty close to correct.