Reputation: 181
BEGIN BLOCK BLK_ROWDEC
NAME cell_rowdec
SIZE UNI_rowdecSize
ITERATE itr_rows
DIRECTION lgDir_rowdec
STRAP STRD1,STRD3,STRD2
WRAP WRD1
VIA VIAB,VIAC,VIAD
ENDS BLK_ROWDEC
I want to parse this using flex and bison such that it matches block name of BEGIN and ENDS. And it finds both are equal then only parse. So how can it is possible with flex and bison please help me out.
From long times I am stuck with this problem. Please help me.
Thank you so much.
Upvotes: 0
Views: 585
Reputation: 2882
If I understand correctly, it's all about the begin/end pairs with a name.
If you have a context free grammar, you'll have begin/end pairs that match, like in
text := block
| text block
;
block := BEGIN BLOCK blockname blockcontents ENDS blockname
;
blockcontents := item
| blockcontents item
;
item := block
| VIA vialist
| WRAP wrapname
...
Now if you look at the production of block, you'll note that the name occurs twice. In your action you can check equality. If both names are equal, fine, if not, you have a syntax error. Ignoring everything since the opening "BEGIN BLOCK" is one strategy to cope with the syntax error. (If I'm not mistaken, the condition that the names must match makes the grammar not context free, but since the condition is very simple, I'd categorize it as "almost context free" ;)
If your text allows several blocks to be mixed, you have a grammar that is not context free and is much more difficult to parse (though not impossible). You still can use lex/yacc resp. flex/bison, but it'll require a lot more bookkeeping from your side.
Still, the first thing you need is a grammar. My (partial) example above could be a start. You can use bison/yacc syntax to specify your grammar. That would reduce a bit of effort.
Upvotes: 1