2

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.

1 Answer 1

3

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.

2
  • Interesting. That kind of sucks that the eclipse plug-in is doing this. I guess that will teach me for not having tried other alternatives and just trusting the plug-in. Thanks Bart!
    – Mateo
    Jul 29, 2011 at 19:19
  • 1
    @Mateo, you're welcome. A lot of 3rd party ANTLR tools are like this. When I use ANTLRWorks and something doesn't go as I had expected, I always perform a "command line test" before investigating further.
    – Bart Kiers
    Jul 29, 2011 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.