Trying to make match on a rule that uses "recursive" identifier in flex

I have this line:

0, 6 -> W(1) L(#);

or

\# -> @shift_right R W(1) L

I have to parse this line with flex, and take every element from every part of the arrow and put it in a list. I know how to match simple things, but I don't know how to match multiple things with the same rule. I'm not allowed to increase the limit for rules. I have a hint: parse the pieces, pieces will then combine, and I can use states, but I don't know how to do that, and I can't find examples on the net. Can someone help me?

So, here an example:

{

a -> W(b) #invert_loop;

b -> W(a) #invert_loop;

-> L(#)

}

When this section begins I have to create a structure for each line, where I put what is on the left of -> in a vector, those are some parameters, and the right side in a list, where each term is kinda another structure. For what is on the right side I wrote rules:

writex W([a-zA-Z0-9.#]) for W(anything).

So I need to parse these lines, so I can put the parameters and the structures int the big structure. Something like this(for the first line):

new bigStruc with param = a and list of struct = W(anything), #invert(it is a notation for a reference to another structure)

So what I need is to know how to parse these line so that I can create and create and fill these bigStruct, also using to rules for simple structure(i have all I need for these structures, but I don't how to parse so that I can use these methods).

Sorry for my English and I hope this time I was more clear on what I want.

Last-minute editing: I have matched the whole line with a rule, and then work on it with strtok. There is a way to use previous rules to see what type of structure i have to create? I mean not to stay and put a lots of if, but to use writex W([a-zA-Z0-9.#]) to know that i have to create that kind of structure?

Upvotes: 0

Views: 146

Answers (1)

Spencer Rathbun
Spencer Rathbun

Reputation: 14910

Ok, lets see how this snippet works for you:

// these are exclusive rules, so they do not overlap, for inclusive rules, use %s
%x dataStructure
%x addRules
%%
<dataStructure>-> { BEGIN addRules; }
\{                { BEGIN dataStructure; }
<addRules>;       { BEGIN dataStructure; }
<dataStructure>\} { BEGIN INITIAL; }

<dataStructure>[^,]+ { ECHO; } //this will output each comma separated token
<dataStructure>. { } //ignore anything else
<dataStructure>\n { } //ignore anything else
<addRules>[^ ]+ { ECHO; } //this will output each space separated rule
<addRules>. { } //ignore anything else
<addRules>\n { } //ignore anything else
%%

I'm not entirely sure what it it you want. Edit your original post to include the contents of your comments, with examples, and please structure your English better. If you can't explain what you want without contradicting yourself, I can't help you.

Upvotes: 0

Related Questions