Search code examples
javarecursioncompiler-constructionjavacc

What do parenthesis mean in JavaCC?


let's say I had:

void ProdRule() : {}
{
    (NonTerminal() OtherNonTerminal())
}

What do parenthesis around:

(NonTerminal() OtherNonTerminal())

mean?


Solution

  • Like in mathematical expressions or normal programming languages, parentheses can be used to group subexpressions together differently than what you'd get using normal operator precedence.

    For example if you have this:

    A() B() | C()
    

    That means "either 'A followed by B' or C", whereas this:

    A() (B() | C())
    

    Would instead mean "A followed by 'B or C'".

    Parentheses are also required to use postfix operators such as *, + or ? and determine which parts of the grammar those operators apply to. So for example:

    A() (B())*
    

    Would mean "one A, followed by zero or more Bs", whereas this:

    (A() B())*
    

    Would mean "zero or more occurrences of 'A followed by B'".

    In your example the parentheses don't do anything at all and could be removed.