Home > Software design >  ANTLR 4 Lexing issues - 1:0 mismatched input 'x' expecting 'x'
ANTLR 4 Lexing issues - 1:0 mismatched input 'x' expecting 'x'

Time:10-29

I'm trying to create an ANTLR 4 grammar to understand this code:

package main () {
    
    name1;
    name2;
    
}

Here is what I have so far:

grammar Crimson;

// Parser rules

program 
    : packageDefinitionList EOF
    ;

packageDefinitionList
    : packageDefinition 
    ;

packageDefinition
    : Package Identifier parameterList packageBody
    ;
    
parameterList
    : parameter*
    ;

parameter
    : parameterType Identifier
    ;

packageBody
    : statement*
    ;

parameterType
    : Integer | Boolean
    ;
    
statement
    : Identifier
    ;

// Lexer rules

Package
    : 'package'
    ;
Integer
    : 'int'
    ;
Boolean
    : 'bool'
    ;
Identifier
    : NonDigit 
    ;
fragment NonDigit 
    : [a-zA-Z_]
    ;

When I put these Screenshot of ANTLR Lab

CodePudding user response:

Hmm alright I guess the website was having a bad day? Maybe the versions are incompatible or something...

Either way, using this enter image description here

{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "Debug ANTLR4 grammar",
        "type": "antlr-debug",
        "request": "launch",
        "input": "Crimson\\CSharp\\Antlr\\test.crm",
        "grammar": "Crimson\\CSharp\\Antlr\\crimson.g4",
        "startRule": "program",
        "printParseTree": true,
        "visualParseTree": true
    }
    ]
}

CodePudding user response:

I believe that your example is valid (although missing whitespace rule and others) and I looked at the JSON result from my antlr server and it does, in fact, yield an incorrect bit of JSON. I created a bug for you: enter image description here

  • Related