compiler-errorsantlr4

Can I match an empty function argument with ANTLR?


I need to be able to recognize an empty argument to a function with ANTLR. For a given function, I would like to be able to support any argument being empty when parsing and the fact that a non-empty argument could have whitespace around the argument, e.g.:

f(x,y,)     parse args --> {x,y,empty}
f(x,,z)     parse args --> {x,empty,z}
f(x, ,z)    parse args --> {x,empty,z}
f( x , y, ) parse args --> {x,y,empty}

I am playing with a toy example at lab.antlr.org. I have the following:

Lexer

// DELETE THIS CONTENT IF YOU PUT COMBINED GRAMMAR IN Parser TAB
lexer grammar ExprLexer;

AND : 'and' ;
OR : 'or' ;
NOT : 'not' ;
EQ : '=' ;
COMMA : ',' ;
SEMI : ';' ;
LPAREN : '(' ;
RPAREN : ')' ;
LCURLY : '{' ;
RCURLY : '}' ;

INT : [0-9]+ ;
ID: [a-zA-Z_][a-zA-Z_0-9]* ;
WS: [ \t\n\r\f]+ -> skip ;

Parser

parser grammar ExprParser;
options { tokenVocab=ExprLexer; }

program
    : stat EOF
    | def EOF
    ;

stat: ID '=' expr ';'
    | expr ';'
    ;

def : ID '(' ID (',' ID)* ')' '{' stat* '}' ;

expr: ID
    | INT
    | WS*
    | func
    | 'not' expr
    | expr 'and' expr
    | expr 'or' expr 
    ;

func : ID '(' expr (',' expr)* ')' ;

Input

f(x,y,) {
    a = 3;
    x and y;
}

Running this code does actually give me the function definition with three arguments and the third one being empty, but there is an error with the rest of the parsing:

1:8 mismatched input '{' expecting {'and', 'or', ';'}

Linked question: ANTLR: empty condition not working


Solution

  • The trailing comma is invalid. If you want to support that, do this:

    def : ID '(' param (',' param)* ')' '{' stat* '}' ;
    
    param
     : ID
     | /* nothing */
     ;
    

    This will support f(x,y,) {}, f(x,y) {}, f(x,,,,y) {} and f() {}