Mateo
Mateo

Reputation: 1960

ANTLR no viable alternative at input '/'

Okay, I'm really confused about this error. I know in the past having a '/' as a token in a rule hasn't produced any errors. However, this is simply baffling. Here is my grammar:

grammar LilWildC;

options {
    language = Java;
}

@header
{
    package com.matthewkimber.lilwildc;
}

@lexer::header
{
    package com.matthewkimber.lilwildc;
}

program
    :   global_variables procedure+
    ;

global_variables
    :   variable_definition*
    ;

variable_definition
    :   'number' IDENT ';'
    |   'number' '[' A_NUMBER ']' IDENT ';'
    ;

procedure
    :   'procedure' IDENT '{' block '}'
    ;

block
    :   local_variables statement+
    ;

local_variables
    :   variable_definition*
    ;

statement
    :   variable_reference '=' numeric_expression ';'
    ;

variable_reference
    :   IDENT
    |   IDENT '[' numeric_expression ']'
    ;

numeric_expression
    :   multiply_expression
        ( '+' multiply_expression
        | '-' multiply_expression
        )*
    ;

multiply_expression
    :   negative_factor
        ( '*' negative_factor
        | '/' negative_factor
        | '%' negative_factor
        )*
    ;

negative_factor
    :   '-'? factor
    ;

factor
    :   A_NUMBER
    |   variable_reference
    |   '(' numeric_expression ')'
    ;

A_NUMBER:   (('0'..'9')+'.'?) | (('0'..'9')*'.'('0'..'9')+) ;
IDENT:  ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9' | '_')* ;
WS: (' ' | '\t' | ('\r'?'\n'))+ { $channel = HIDDEN; } ;

When I run a test on the grammar with the following input:

procedure main
{
    var = 10 / 1;
}

I get the following parse tree in the ANTLR eclipse plug-in:

ANTLR Parse Tree - NoViableAltException

What I don't get is that multiplication and modulo work fine, only divide throws this error. Is ANTLR skipping right over the '/' and not seeing it as a token or have I missed something? Any help is greatly appreciated.

Upvotes: 2

Views: 2485

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

There's nothing wrong with your grammar, the problem must be the Eclipse plugin. ANTLRWorks' debugger produces the tree:

enter image description here

And creating a little test myself (after fixing the typo grammary LilWildC; to grammar LilWildC;, and removing the packages) with a main class and ANTLR 3.3:

LilWildC.g

grammar LilWildC;

options {
    language = Java;
}

program
    :   global_variables procedure+
    ;

global_variables
    :   variable_definition*
    ;

variable_definition
    :   'number' IDENT ';'
    |   'number' '[' A_NUMBER ']' IDENT ';'
    ;

p    rocedure
    :   'procedure' IDENT '{' block '}'
    ;

block
    :   local_variables statement+
    ;

local_variables
    :   variable_definition*
    ;

statement
    :   variable_reference '=' numeric_expression ';'
    ;

variable_reference
    :   IDENT
    |   IDENT '[' numeric_expression ']'
    ;

numeric_expression
    :   multiply_expression
        ( '+' multiply_expression
        | '-' multiply_expression
        )*
    ;

multiply_expression
    :   negative_factor
        ( '*' negative_factor
        | '/' negative_factor
        | '%' negative_factor
        )*
    ;

negative_factor
    :   '-'? factor
    ;

factor
    :   A_NUMBER
    |   variable_reference
    |   '(' numeric_expression ')'
    ;

A_NUMBER:   (('0'..'9')+'.'?) | (('0'..'9')*'.'('0'..'9')+) ;
IDENT:  ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9' | '_')* ;
WS: (' ' | '\t' | ('\r'?'\n'))+ { $channel = HIDDEN; } ;

Main.java

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    String src = 
        "procedure main     \n" +
        "{                  \n" +
        "    var = 10 / 1;  \n" +
        "}                  \n";
    LilWildCLexer lexer = new LilWildCLexer(new ANTLRStringStream(src));
    LilWildCParser parser = new LilWildCParser(new CommonTokenStream(lexer));
    parser.program();
  }
}
bart@hades:~/Programming/ANTLR/Demos/LilWildC$ java -cp antlr-3.3.jar org.antlr.Tool LilWildC.g
bart@hades:~/Programming/ANTLR/Demos/LilWildC$ javac -cp antlr-3.3.jar *.java
bart@hades:~/Programming/ANTLR/Demos/LilWildC$ java -cp .:antlr-3.3.jar Main

produces no errors or warnings.

Upvotes: 3

Related Questions