antlr / antlr4-lab

A client/server for trying out and learning about ANTLR
MIT License
39 stars 11 forks source link

Given input that should be valid, returns errors #63

Closed JohnAlexCO closed 1 year ago

JohnAlexCO commented 1 year ago

This issue is specifically with the online lab. Minimum Repeatable example:

grammar Expr;       
prog:   expr EOF ;

WS : ' ' -> skip ; 
ASSIGNMENT : ':' ;
IDENTIFIER : [A-Za-z_]+ ;
DECLARE :
    'func' | 'int' | 'integer' |
    'float' | 'byte' |
    'str' | 'string' |
    'array' | 'list' |
    'dict'
    ;
expr : assignment ;
assignment: 
    IDENTIFIER ASSIGNMENT DECLARE IDENTIFIER
 ;

given the input

print: func value

Gives the errors

1:7 missing DECLARE at 'func'
1:12 extraneous input 'string' expecting

I'm not sure what these error messages are even supposed to mean in this context; DECLARE is clearly not missing, and assignment expects four terms, so I'm not sure how the input is "extraneous". I think there's something wrong with the lab.

JohnAlexCO commented 1 year ago

Different grammars also produce various unexplained errors. For example, with the rules

IDENTIFIER : [A-Za-z_$] ;
ASSIGNMENT : ':' ;

and the text

print:

the lab gives the following error:

1:5 token recognition error at: ':'
ericvergnaud commented 1 year ago

You need to define IDENTIFIER after all other tokens, otherwise 'func' is considered an IDENTIFIER

parrt commented 1 year ago

hi. yep, lexer rules Match the longest match and then, if there's an ambiguity between rules, matches the first one specified.

parrt commented 1 year ago
Screenshot 2022-12-25 at 7 08 52 PM