antlr / antlr/antlr4

An error deep in the code of an input class shows an error message starting at the class

Open
#608 7 comments 0 reactions 0 assignees View on GitHub
comp:runtime error-handling type:bug
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

Using the grammar at tag https://github.com/antlr/grammars-v4/blob/distant_error_msg/swift/Swift.g4 we get a horrible error message that basically says I don't recognize the class just because he doesn't know what to do with a floating-point number, 2.0, deep in a method. Input exhibiting the problem is: https://github.com/antlr/grammars-v4/blob/master/swift/examples/Snippet.swift

I get the same error message in the interpreter and compiled code via grun, which is encouraging. ;) Somehow we need to enhance our check that looks for successfully predicted rules, rather than rolling all the way back to the class token.

Even:

```
var b = i < 2.0
```

highlights the problem in that it doesn't recognize anything but clearly parses lots of rules before it gets to the floating-point number. Here's a sample run that shows that the tokens look okay:

```
~/antlr/code/grammars-v4/swift $ grun Swift top_level -tokens
var b = i < 2.0
[@0,0:2='var',<46>,1:0]
[@1,3:3=' ',<96>,channel=1,1:3]
[@2,4:4='b',<87>,1:4]
[@3,5:5=' ',<96>,channel=1,1:5]
[@4,6:6='=',<29>,1:6]
[@5,7:7=' ',<96>,channel=1,1:7]
[@6,8:8='i',<87>,1:8]
[@7,9:9=' ',<96>,channel=1,1:9]
[@8,10:10='<',<7>,1:10]
[@9,11:11=' ',<96>,channel=1,1:11]
[@10,12:14='2.0',<93>,1:12]
[@11,15:15='\n',<96>,channel=1,1:15]
[@12,16:15='',<-1>,2:16]
line 1:12 no viable alternative at input 'var b = i < 2.0'
~/antlr/code/grammars-v4/swift $ grep "93$" *.tokens
Swift.tokens:Floating_point_literal=93
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with Swift.g4 and examples/Snippet.swift, using grun Swift top_level with the shown floating-point input and token output. Trace how the parser reports the error after `var b = i < 2.0`; done means interpreter and compiled grun runs report the error near the offending token rather than rolling back to the class.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.