0

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 The ANTLR Lab Simulation, I receive the error: 1:0 mismatched input 'package' expecting 'package'.

I've been staring at example after example, and I'm using the example C grammar on the Lab for guidance, yet I have no clue how to make this thing work...

Note: from link 2 I think I may be incorrectly defining my lexer rules, but it hasn't helped me fix the issue :(

Screenshot of ANTLR Lab

Atom
  • 325
  • 1
  • 11
  • 1
    Thanks for trying out lab.antlr.org. Yes, it looks like it needs more work when given a bad grammar. (It gives "BAD JSON RESPONSE" if you start from scratch and enter in the parser grammar, erase the lexer grammar, and enter in the input, the press "Run".) I'll let Parr know. Your grammar is missing token defs for '(', ')', '{', '}', and WS. – kaby76 Oct 17 '22 at 21:19
  • 1
    Hi. Thanks. Will check this out soon. Haven't spent much time on error handling just yet. – Terence Parr Oct 17 '22 at 22:51

2 Answers2

1

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

Either way, using this ANTLR VSCode Extension with the run configuration below, I was able to generate exactly what I needed: 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
    }
    ]
}
Atom
  • 325
  • 1
  • 11
1

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: https://github.com/antlr/antlr4-lab/issues/46

Ok, fixed. Retrying your grammar you'll see: enter image description here

Terence Parr
  • 5,912
  • 26
  • 32