How to exclude the comma operator from an expression in Grammar-Kit?

320 views Asked by At

I'm writing an IntelliJ language plugin for a C-derived language which includes the comma operator. I'm using Grammar-Kit to generate the parser. Where the formal grammar has a lot of nested expression productions, I've rewritten them using Grammar-Kit's priority-based expression parsing, so my expression production looks like this:

expression ::= comma_expression
 | assignment_expression
 | conditional_expression
 | eor_expression
 | xor_expression
 | and_expression
 | equality_expression
 | relation_expression
 | add_expression
 | mul_expression
 | prefix_expression
 | postfix_group
 | primary_expression

comma_expression ::= expression ',' expression {pin=2}
// etc.

This works fine in itself, but there are places in the grammar where I need to parse an expression that can't be a comma expression. Function calls are one example of this:

function_call_expression ::= identifier '(' ('void'|<<comma_list expression>>)? ')'
private meta comma_list ::= <<p>> (',' <<p>>)*

A function argument can't be a comma expression, because that would be ambiguous with the comma separating the next argument. (In the grammar as I have it now, it always parses as a single comma expression.) The formal grammar deals with this by specifying that each function argument must be an assignment expression, because their assignment expression includes all the expressions with tighter precedence. That doesn't work for the Grammar-Kit priority-based grammar, because an assignment expression really does have to include an assignment.

The same applies to initializers, where allowing a comma expression would lead to an ambiguous parse in cases like int x=1, y;.

How should I deal with this situation? I'd like to keep using the priority-based parse to keep a shallow PSI tree, but also avoid manually rewriting the PSI tree for function calls to turn aCommaExpression into an argument list.

0

There are 0 answers