Emily H
Emily H

Reputation: 13

Antlr4 token recognition error and extraneous input

I'm trying to create SQL interpreter for my project. I ran into these errors when I run my program.

line 2:28 token recognition error at: ''a'
line 2:33 token recognition error at: '','
line 2:30 extraneous input 'nna' expecting Value

This is my test sql query:

INSERT INTO teacher VALUES ('Anna', 21);

And the partial of my grammar is:

insert: INSERT INTO ValidName VALUES '(' Value (',' Value)* ')' ';' ;
Value: Number | String;
ValidName: [a-z][a-z0-9_]*;
Number: [0-9]+;
String: '\''[^']+'\'';

I try to print out ctx.children and got this:

[INSERT, INTO, teacher, VALUES, (, nna, 21, ), ;]

Would anyone please help me where did I do wrong?

Upvotes: 1

Views: 707

Answers (1)

Mike Cargal
Mike Cargal

Reputation: 6785

A couple of thing should help:

1 - Value should probably be a parser rule rather than a Lexer rule:

value: Number | String;

(and change the Valuess in your rules to values

2 - For your STRING rule, it's a bit simpler to use the non-greedy operator to pick up everything until you match the next character:

STRING: '\'' .*? '\'';

Upvotes: 2

Related Questions