Raghul KB
Raghul KB

Reputation: 3

How to resolve the no viable input error in racket?

I've been trying to execute the following program in a parser

#lang racket
( define ( highest-number xs )
  ( define ( max x1 x2 )
    ( if ( > x1 x2) x1 x2 ) )
  ( foldl max ( first xs ) ( rest xs ) )

The error that is generated is as follows : line 3:4 no viable alternative at input '( define'

These are my rules :

grammar hello;

program
    : defOrExpr+ EOF
    ;

defOrExpr
    : definition
    | expr
    | testCase
    | libraryRequire
    ;

definition
    : '(' 'define' '(' name NAME+ ')' expr ')'
    | '(' 'define' name expr ')'
    | '(' 'define-struct' name '(' name* ')' ')'
    ;

Upvotes: 0

Views: 52

Answers (1)

Mike Cargal
Mike Cargal

Reputation: 6785

(As Bart mentioned, it's much easier to help if there's a buildable grammar and sample input to reproduce your problem.)

In this case, I think the problem is fairly obvious.

Your definition rule does not contain an alternative that allows for a nested definition, and your sample input has a nested definition.

I'm not familiar enough with Racket to suggest an alternative to address the issue, but that's why you're getting your error. (it's on the ( define ( max x1 x2 ) part, not the first define)

Upvotes: 1

Related Questions