[antlr-interest] Simple grammar not showing a recognition exception

amartinez at atc.ugr.es amartinez at atc.ugr.es
Tue Nov 11 09:12:49 PST 2008


Hi all,

I'm having problems in grammars that do not throw a
recognition exception.


I want to parse a very little/restricted assembly language source, only
the 'add' opcode for now.

The grammar should process this input:
<input>

add r1, 23
add r4, r5

</input>

Below it is attached an example to reproduce my error.

Everything seam to work fine (Lexer and sometimes the grammar complain
on the source).

But, if a try this source:

<answer input>

add  r1, 23
addj r4,56

</answer input>

the parser does not say anything about the inappropriate 'addj' (which is
not a legal assembly token). I have even create an AST from the original
grammar, have debugged it on AntlrWorks, and have seen that this environment
also does not complain on this input.

Where is the mistake?

Thank in advance, best regards


Attached is an example of a grammar to reproduce my error:


<Grammar T>

grammar T;

tokens {
	ADD;
}

prog			:	(add NEWLINE)+ ;
add			:	TOKEN_ADD renreg ',' renreg ;
renreg		: 	RX | UINT8 | ID ;

RX			:	('r'|'R') HEXDIGIT;
TOKEN_NAMEREG	:	('namereg' | 'Namereg' | 'NAMEREG');
TOKEN_CONST		:	('const' | 'Const' | 'CONST');
TOKEN_ADD		:	'add' ;

ID			:	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'.'|'0'..'9')* ;
UINT8			:	HEXDIGIT? HEXDIGIT;
fragment
HEXDIGIT		:	('0'..'9'|'a'..'f'|'A'..'F');
NEWLINE    		:	{getCharPositionInLine() > 0}? 	=> ('\r'? '\n')+ ;
NEWLINE_AT_COLUM_ZERO	:   	{getCharPositionInLine() == 0}? => ('\r'?
'\n')+ { $channel=HIDDEN; } ;
WS			:   	(' '|'\t') { $channel=HIDDEN; };
LINE_COMMENT 	:   	(';'|'//') (~'\n')* { $channel=HIDDEN; } ;


</Grammar T>





<Main.java>

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;


public class Main {

    public static void main(String args[]) throws Exception {

     CharStream input = new ANTLRFileStream(args[0]);
     TLexer lex = new TLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lex);
     TParser g = new TParser(tokens);
      g.prog ();
   }
}

</Main.java>




More information about the antlr-interest mailing list