SamTebbs33
SamTebbs33

Reputation: 5647

Using brackets/parentheses in EBNF or GNU Bison grammar

I am defining my own grammar for a parser generator that outputs C/C++ code. I chose Bison for this, but have run into a problemm. I would like to use brackets ("(", ")") within rules, but Bison doesn't accept themFor example, to declare a character I use (the three dots are instead of all other possible ASCII characters):

character: "'" ("\0" | "\t" | "\n" | " " | "!" | ... | "}" | "~") "'";

As you can see, I use brackets to say that a character must have a quotation mark, any character and then another quotation mark.

I do something similar for an integer:

integer: (["-"] digits) | "0";

Is there any way of achieving something similar in Bison? Alternatively, is there a parser generator that accepts EBNF, or even just brackets and that outputs c++ code?

Upvotes: 1

Views: 2313

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126418

Bison does not support EBNF, only BNF, so if you want optional or grouped things, you'll have to write the rule yourself:

character: '\'' character_char '\''
character_char: '\0' | '\t' | ...

integer: opt_negate digits | '0'
opt_negate: | '-'

Also, note that " characters in bison don't denote literals, so won't do what you want (and are generally useless, as you can't refer to them easily in the lexer). Always use ' -- single quotes -- for literal.

In addition, anything that can be recognized by a regular expression (like these examples), should probably be recognized in the lexer and returned to the bison parser as a single token -- it's much simpler, clearer, and more efficient.

Upvotes: 5

Related Questions