Ido Barash
Ido Barash

Reputation: 5132

Antlr4 arithmetics representation

I am parsing a SQL like language and I need to be able to handle this kind of expressions (for example only):

SELECT number_of_children - number_of_cars   

this should be arithmetic subtraction of two columns.

SELECT child-name

this should be a column name 'child-name'

How can I do this?

Upvotes: 0

Views: 34

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51390

In the first case you should have 4 tokens: SELECT number_of_children - number_of_cars.
In the second case you should only have two: SELECT child-name.

This can be achieved by carefully choosing your lexer rules:

SELECT: 'SELECT';
OP_SUB: '-';
ID:     [a-zA-Z][a-zA-Z0-9_-]*;
WS:     [ \t\r\n]+ -> skip;

These rules will yield SELECT ID OP_SUB ID in the first case and SELECT ID in the second one.

Upvotes: 1

Related Questions