April 18th, 2009, 08:34 AM
it's probably not the right place but I don't know where else to go... .
I want to write a grammar for a language where you have expr and ops. You can put braces around these expr but opening and closing braces have to conform to the rules that are given with adding integers.
(I use javacc)
a + b + c
a + (b + c)
(a + b) + c
(a + (b + c))
(a + b + c) + (d)
... - all are valid.
I cannot find a bnf grammar which can produce all the various nestings and combinations of braces.
Could you please help?
April 18th, 2009, 09:08 AM
A BNF notation for an expression-grammar supporting numbers, parenthesis, +, -, * and / would look like this:
expression ::= addExp*
addExp ::= mulExp (('+'|'-') mulExp)*
mulExp ::= atom (('*'|'/') atom)*
atom ::= number
| '(' expression ')'
number ::= DIGIT+ ('.' DIGIT+)?
DIGIT ::= '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'0'
// * zero or more times
// + one ore more times
// | logical OR
// ? zero or once
// Of course, the characters between quotes are the actual characters
Comments on this post
Last edited by prometheuzz; April 18th, 2009 at 09:14 AM.
April 19th, 2009, 07:26 AM
thank you very much. I'll try to get this into javacc code.
April 19th, 2009, 12:09 PM
Originally Posted by devhoush