68869

ANTLR4: Extraneous Input error

I'm following the "Definitive ANTLR4 Reference" book, and decided to add a couple of keywords to their calculator grammar to help clear memory. Building the grammar and compiling the resulting java code works fine, but when I execute the visitor code, I get the error: "line 6:0 extraneous input '$rem' expecting {<EOF>, '(', ID, INT, NEWLINE}" and the same for '$clearmem' on line 8:0.

Here is my grammar file:

grammar LabeledExpr; //Parser rules================================= prog: kword+ | stat+ ; stat: expr endl # printExpr | ID '=' expr endl # assign | NEWLINE # blank ; expr: expr op=('*'|'/') expr # MulDiv | expr op=('+'|'-') expr # AddSub | INT # int | ID # id | '(' expr ')' # parens ; kword: '$clearmem' endl #clearMem | '$rem' ID endl #remVar ; endl: NEWLINE | EOF ; //Lexer rules================================== ID: [a-zA-Z]+ ; INT: [0-9]+ ; NEWLINE: '\r'? '\n' ; WS: [ \t]+ -> skip; MUL: '*' ; DIV: '/' ; ADD: '+' ; SUB: '-' ;

And the .expr file with the code to parse:

193 a = 5 b = 6 a b $rem a a $clearmem

I just started on ANTLR4 last night so I really don't know much of what to look for error-wise, but from what I can tell nothing is wrong with either file.

I'm sure that I am missing something pretty simple, but I can find it, so I would appreciate help from anyone who is more familiar with ANTLR4.

Thanks.

Answer1:

The problem is your prog rule:

prog: kword+ | stat+ ;

This rule states that the program consists of one or more kword rules or one or more stat rules. There is no program that includes both a kword and stat. What you probably meant to write is the following, which allows any sequence of kword or stat rules. Note that I changed + to * to allow an empty program. Even if your compiler shouldn't allow the program to be empty, this error is better left for validation in a visitor or listener.

prog : ( kword | stat )* ;

Recommend

  • How to convert character of percentage into numeric in R
  • Using QUERY URL to group data before sending to Google Apps Script dashboard
  • How to make one column to take all the remaining width?
  • Whats better using HTML/CSS edited by hand or using design programs? and why?
  • why am I stuck in an infinite loop after invoking poll() or remove() on a priority queue? Java
  • Batch file read in var and write back
  • How to check a string does not start with a number in Batch?
  • Complex python3 csv scraper
  • Negated scanset in fscanf and EOF
  • SAXReader not re-ecape characters
  • How to know when stdin is empty if it contains EOF?
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • what is the difference between the asp.net mvc application and asp.net web application
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • Matrix multiplication with MKL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How to stop GridView from loading again when I press back button?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal