Bison input analyzer - basic question on optional grammer and input interpretation

Posted by kumar_m_kiran on Stack Overflow See other posts from Stack Overflow or by kumar_m_kiran
Published on 2010-05-27T13:08:06Z Indexed on 2010/05/27 13:11 UTC
Read the original article Hit count: 412

Filed under:
|
|

Hi All,
I am very new to Flex/Bison, So it is very navie question.
Pardon me if so. May look like homework question - but I need to implement project based on below concept.

My question is related to two parts,
Question 1
In Bison parser, How do I provide rules for optional input.

Like, I need to parse the statment
Example :

-country='USA' -state='INDIANA' -population='100' -ratio='0.5' -comment='Census study for Indiana'

Here the ratio token can be optional. Similarly, If I have many tokens optional, then How do I provide the grammer in the parser for the same?

My code looks like,

%start program
program : TK_COUNTRY TK_IDENTIFIER TK_STATE TK_IDENTIFIER TK_POPULATION TK_IDENTIFIER ...
where all the tokens are defined in the lexer. Since there are many tokens which are optional, If I use "|" then there will be many different ways of input combination possible.

Question 2
There are good chance that the comment might have quotes as part of the input, so I have added a token -tag which user can provide to interpret the same,

Example :

-country='USA' -state='INDIANA' -population='100' -ratio='0.5' -comment='Census study for Indiana$'s population' -tag=$  

Now, I need to reinterpret Indiana$'s as Indiana's since -tag=$.

Please provide any input or related material for to understand these topic. Thanks for your input in advance.

© Stack Overflow or respective owner

Related posts about input

Related posts about bison