Thread: Braces

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Braces


    Hello Everybody,

    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)
    e.g.:
    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?
    thx,
    harald
  2. #2
  3. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    A BNF notation for an expression-grammar supporting numbers, parenthesis, +, -, * and / would look like this:

    Code:
    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

    • requinix agrees : very nice
    Last edited by prometheuzz; April 18th, 2009 at 10:14 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0
    thank you very much. I'll try to get this into javacc code.
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    Originally Posted by devhoush
    thank you very much. I'll try to get this into javacc code.
    You're welcome.

IMN logo majestic logo threadwatch logo seochat tools logo