0
votes
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.

1
have you tried to define a grammar for your language? From my experience that's the most important part. The rest is easy afterwards.Ronald
yes I parse whole file but I don't know which condition I put so that it is match Both(BEGIN and ENDS) in Block name. Like In my case BLK_ROWDEC. So how can I do??shailavi shah
I need a bit more text here. So I'll answer, which might or might not resolve your problemRonald
which type of text???shailavi shah

1 Answers

1
votes

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.