mirkomario
mirkomario

Reputation: 33

ANTLR rule for function call

I'm new on ANTLR4 and I trying to parse this input

X = 1 2 A(2) B (2)

In this input, A should be a function call while B should be a variable of name B.But i have a rule in lexer that skip the whitespace.

How can i write the parser rule for this input but keeping the rule that skip whitespace

Thanks in advance

Upvotes: 0

Views: 576

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53317

The solution is to define a function introducer in the lexer, where you have control over the whitespaces and then continue the function call in the parser for flexible parameter handling:

FUNCTION_START: ID OPEN_PAR;


function: FUNCTION_START parameters CLOSE_PAR;

The keypoint here is that in the lexer the whitespace rule doesn't kick in while you are in another lexer rule, hence the FUNCTION_START rule will only accept input of the form identifer( with no whitespaces inbetween. It will not match B (.

Upvotes: 1

Related Questions