antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.04k stars 3.27k forks source link

JavaScript Target does not parse input as expected (arithmetic operations) #3215

Closed paulsobolev closed 3 years ago

paulsobolev commented 3 years ago

We use antlr 4.9.2 (both sides, npm package on UI, and antlr-4.9.2-complete.jar to generate target files)

Rules for Parser:

expression  
            : expression operator=(MUL | DIV) expression #mulDivExp
            | expression operator=(ADD | SUB) expression #addSubExp
            | SUB? NUMBER #numericExp
            | OPEN_P expression CLOSE_P #parenthesisExp
            | FIELD #fieldExp
            ;

Code to test:

import antlr4 from 'antlr4'
import FormulaLexer from './FormulaLexer'
import FormulaParser from './FormulaParser'

function validateFormula(input) {
    console.log('INPUT: ', input)
    const chars = new antlr4.InputStream(input)
    const lexer = new FormulaLexer(chars)
    const tokens = new antlr4.CommonTokenStream(lexer)
    const parser = new FormulaParser(tokens)
    parser.buildParseTrees = true
    const tree = parser.resultExpression()
    console.log('TREE: ', tree.children)
}

validateFormula('-2\n') // OK
validateFormula('2 + 2\n') // Not OK, first child in tree is MulDivExpContext
validateFormula('2 - 2\n') // Not OK, first child in tree is MulDivExpContext
validateFormula('2 * 2\n') // OK
validateFormula('2 / 2\n') // OK
validateFormula('1 + 2 * 3 / 4\n') // Not OK, first child in tree is MulDivExpContext (instead of addSubExp), and input marked as invalid

Result:

Screenshot 2021-07-02 at 17 19 16

Files to test (Source files, Parser.g4, Lexer.g4, and compiled files to run in browser): formula-parser.zip

Thanks in advance for any help or advice how to make it work!

ericvergnaud commented 3 years ago

Thanks, looks like a bug, I'll look into it sometimes next week

ericvergnaud commented 3 years ago

Hi,

closing this since:

1) your generated parser is not up to date with your grammar 2) issue is already solved using latest js runtime (not yet released)

paulsobolev commented 3 years ago

Hi, thanks for feedback!

  1. your generated parser is not up to date with your grammar

I provided (by mistake) parser with more grammar rules which we use in our application, but these rules work fine.

  1. issue is already solved using latest js runtime (not yet released)

Could you please estimate release time?