tags:

views:

112

answers:

1
+1  Q: 

parsing with bison

I bought Flex & Bison from O'Reilly but I'm having some trouble implementing a parser (breaking things down into tokens was no big deal).

Suppose I have a huge binary string and what I need to do is add the bits together - every bit is a token:

 [0-1] { return NUMBER;}

1101010111111

Or for that matter a collection of tokens with no "operation".

Would a such a grammar be correct?

calclist :
         | calclist expr EOL {eval($2)}
expr: NUMBER
      |expr NUMBER { $$=$1+$2 }

or is there a better way to do it?

+2  A: 

Your example lex rule "[0-1] { return NUMBER; }" doesn't set yylval, so if you use that value in your grammar (as you do in the rule "expr NUMBER { $$=$1+$2; }") you'll get garbage.

In general what you're doing is correct, though the task you've chosen is so trivial that lex/bison is serious overkill.

Chris Dodd
this is not the task, it was just easier for me to put it this way :)
codenewbie
@codenewbie - He is right, though. Pay a bit closer attention to his first paragraph.
Chris Lutz