Reputation: 2540
When i debug the grammar in the ANTLRWorks 3.4 it gives me folloing error,
javax.swing.text.BadLocationException: Position not represented by view
Grammar which given above error is listed below.
grammar CMinusMinus;
program : (vardeclaration | fundeclaration)* EOF ;
vardeclaration : INT ID (OPENSQ NUM CLOSESQ)? SEMICOL ;
fundeclaration : typespecifier ID OPENP params CLOSEP compoundstmt ;
typespecifier : INT | VOID ;
params : VOID | paramlist ;
paramlist : param (COMMA param)* ;
param : INT ID (OPENSQ CLOSESQ)? ;
compoundstmt : OPENCUR vardeclaration* statement* CLOSECUR ;
statementlist : statement* ;
statement : expressionstmt | compoundstmt | selectionstmt
| iterationstmt | returnstmt ;
expressionstmt : (expression)? SEMICOL ;
selectionstmt : IF OPENP expression CLOSEP statement
( (ELSE)=> ELSE statement
| ( )
)
;
iterationstmt : WHILE OPENP expression CLOSEP statement ;
returnstmt : RETURN (expression)? SEMICOL ;
expression : (var ASSIGN) => var ASSIGN expression
| simpleexpression ;
var : ID ( OPENSQ expression CLOSESQ )? ;
simpleexpression: addexpr ( ( LOREQ | LESS | GRTR | GOREQ | EQUAL | NTEQL) addexpr)? ;
addexpr : mulexpr ( ( PLUS | MINUS ) mulexpr)* ;
mulexpr : factor ( ( MULTI | DIV ) factor )* ;
factor : ( OPENP expression CLOSEP ) | var | call | NUM ;
call : ID OPENP arglist? CLOSEP ;
arglist : expression ( COMMA expression)* ;
ELSE : 'else' ;
IF : 'if' ;
INT : 'int' ;
RETURN : 'return' ;
VOID : 'void' ;
WHILE : 'while' ;
PLUS : '+' ;
MINUS : '-' ;
MULTI : '*' ;
DIV : '/' ;
LESS : '<' ;
LOREQ : '<=' ;
GRTR : '>' ;
GOREQ : '>=' ;
EQUAL : '==' ;
NTEQL : '!=' ;
ASSIGN : '=' ;
SEMICOL : ';' ;
COMMA : ',' ;
OPENP : '(' ;
CLOSEP : ')' ;
OPENSQ : '[' ;
CLOSESQ : ']' ;
OPENCUR : '{' ;
CLOSECUR: '}' ;
SCOMMENT: '/*' ;
ECOMMENT: '*/' ;
ID : ('a'..'z' | 'A'..'Z')+/*(' ')*/ ;
NUM : ('0'..'9')+ ;
WS : (' ' | '\t' | '\n' | '\r'/* | '\f'*/)+ {$channel = HIDDEN;};
COMMENT: '/*' .* '*/' {$channel = HIDDEN;};
And the test file is,
/* A program to perform selection sort on a 10
element array. */
int x[10];
int miniloc ( int a[], int low, int high )
{ int i, int x, int k;
if (a<b)
if (b<c)
b=0;
else
c=0;
else
a=0;
k= low;
x = a[low];
i = low + 1;
while ( i < high)
{ if (a[i] < x)
{ x= a[i];
k= i;}
i = i+1;
}
return k;
}
void sort( int a[], int low, int high)
{ int i, int k;
i = low;
while (i < high - 1)
{ int t;
k = minloc(a,i,high);
t = a[k];
a[k] = a[i];
a[i] = t;
i = i+1;
}
}
void main(void)
{
int i;
i = 0;
while (i < 10)
{ x[i] = input();
i = i+1;}
sort(x, 0, 10);
i=0;
while(i<10)
{ output(x[i]);
i = i + 1;}
}
Are there any way to solve this error. How this usually happens
Thank You.
Upvotes: 0
Views: 505
Reputation: 5290
I'm getting the same exception. I could solve it by switching from {$channel=HIDDEN}
to {skip();}
.
Upvotes: 1
Reputation: 24976
You mentioned ANTLRWorks 3.4 which doesn't exist. There is ANTLR 3.4 and ANTLRWorks 1.4.3, so I will take it that you are using ANTLRWorks 1.4.3 which uses ANTLR 3.4.
ANTLRWorks includes a copy of ANTLR in its build, so you can't change out the version of ANTLR that ANTLRWorks uses. This is because ANTLR is not a commercial product that maintains backward compatibility. So to ensure that ANTLRWorks works properly, the user is not given the option to choose the version of ANTLR to use.
Basically ANTLR 3.4 was the last version of ANTLR 3.x to be released and it was not tested to make sure it worked with ANTLRWorks. One of the biggest problems is that ANTLR 3.4 will not generate NFA and DFA diagrams, and these are needed for ANTLRWorks to work because it uses the output of ANTLR to display the NFA and DFA diagrams.
Normally Ter would fix ANTLR if by doing such would fix a problem with ANTLRWorks. However, Ter and the others basically halted all work on ANTLR 3.X to move onto ANTLR 4.x (Honey Badger). So ANTLR 3.4 is buggy and is not being patched to work with ANTLRWorks, nor will there be a new ANTLRWorks 1.4.x version to go with ANTLR 3.x. There will be a new ANTLRWorks 2.x to go with ANTLR 4.x.
So you may think that I have now driven you into a brick wall, well hold on.
I cut and pasted your sample right into ANTLRWorks 1.4.2, started up the debugger and clicked the "Go To End" button. It worked without a problem. I must mention that this was done on a Debian system running as a VMware virtual machine. I have this so that it is not contaminated by anything else.
Moral of the story is stay away from ANTLRWorks 1.4.3 and instead use ANTLRWorks 1.4.2. Also if you have a machine that cannot absolutely be sure will not have problems such as different JVM versions on or improper classpath, etc. consider using virtual machines.
Upvotes: 2