[antlr-interest] [c target] debugging using antlrworks

Юрушкин Михаил yurushkin at rambler.ru
Mon Oct 5 02:48:06 PDT 2009


> Then you have something wrong in one of your rules.  Specifically, you  
> have a line that says "progName = $program_stmt.progName" in the wrong  
> place, or using the wrong syntax; ANTLR is trying to treat it as a  
> predicate instead of an action.  This also might explain why the  
> debugger is not stopping.
>
> If you want further help with that, then you'll need to post the rule.

I used your grammar (with small modification). the same result...


MyLexer.g
=================================
lexer grammar MyLexer;

options
{
    language = C;
}


ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
       ;

INT :	('0'..'9')+
       ;

WS  :   (   ' '
           |   '\t'
           |   '\r'
           |   '\n'
           )+
           { $channel=HIDDEN; }
       ;


MyParser.g
=================================

parser grammar MyParser;

options
{
       language	= C;
       tokenVocab = MyLexer;
}

@members
{
       ANTLR3_BOOLEAN enableEnum = ANTLR3_FALSE;
}

statement[ANTLR3_BOOLEAN asIDs]

       @init
       {
	enableEnum  = $asIDs;
       }
       : identifier    {printf("enum is an ID\n");}
       | enumAsKeyword {printf("enum is a keyword\n");}
       ;

identifier
       : ID
       | enumAsID
       ;

enumAsKeyword : {enableEnum}? enum1 ;

enumAsID : {!enableEnum}? enum1 ;


enum1: ID ;





MyParser.cpp
=================================

#include    <antlr3.h>
#include <stdio.h>
#include "generated/MyLexer.h"
#include "generated/MyParser.h"

int ANTLR3_CDECL
main	(int argc, char *argv[])
{
        pANTLR3_UINT8	    fName;

       pANTLR3_INPUT_STREAM    input;

       pMyLexer		    lxr;

       pANTLR3_COMMON_TOKEN_STREAM	    tstream;

       pMyParser				psr;
	fName	=(pANTLR3_UINT8)"out/test.txt"; // Note in VS2005 debug, working
directory must be configured

       input	= antlr3AsciiFileStreamNew(fName);

       if ( (ANTLR3_UINT64)input < 0 )
       {
	switch((ANTLR3_UINT64)input)
	{
	case    ANTLR3_ERR_NOMEM:

	    fprintf(stderr, "Unable to open file %s due to malloc() failure1\n",
(char *)fName);
	    exit(1);
	    break;

	default:

	    fprintf(stderr, "Failed to open file %s - exit with code %d\n", (char
*)fName, (ANTLR3_UINT64)input);
	    exit((int)((ANTLR3_UINT64)input));
	    break;
	}
       }

       // Our input stream is now open and all set to go, so we can create a
new instance of our
       // lexer and set the lexer input to our input stream:
       //  (file | memory | ?) --> inputstream -> lexer --> tokenstream -->
parser ( --> treeparser )?
       //
       lxr	    = MyLexerNew(input);	    // CLexerNew is generated by ANTLR

       // Need to check for errors
       //
       if ( (ANTLR3_UINT64)lxr < 0 )
       {
	switch((ANTLR3_UINT64)lxr)
	{
	case    ANTLR3_ERR_NOMEM:

	    fprintf(stderr, "Unable to create the lexer due to malloc()
failure1\n");
	    exit(1);
	    break;

	default:

	    fprintf(stderr, "Failed to create lexer - exit with code %d\n",
(ANTLR3_UINT64)lxr);
	    exit((int)((ANTLR3_UINT64)lxr));
	    break;
	}
       }

       tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT,
TOKENSOURCE(lxr));

       if ((ANTLR3_UINT64)tstream == ANTLR3_ERR_NOMEM)
       {
	fprintf(stderr, "Out of memory trying to allocate token stream\n");
	exit(ANTLR3_ERR_NOMEM);
       }

       // Finally, now that we have our lexer constructed, we can create the
parser
       //
       psr	    = MyParserNew(tstream);  // CParserNew is generated by ANTLR3

       if ((ANTLR3_UINT64)tstream == ANTLR3_ERR_NOMEM)
       {
	fprintf(stderr, "Out of memory trying to allocate parser\n");
	exit(ANTLR3_ERR_NOMEM);
       }

       psr->statement(psr, ANTLR3_TRUE);    // First see enum as a keyword


       psr	    ->free  (psr);	    psr = NULL;
       tstream ->free  (tstream);	    tstream = NULL;
       lxr	    ->free  (lxr);	    lxr = NULL;
       input   ->close (input);	    input = NULL;

       return 0;
}

===============================================
How do I use antlrworks debugger:
1) I generate lexer and parser using -debug option.
2) I run my project. It starts to listen antlrworks (standard port, I
checked).
3) I open MyParser.g grammar and click "Remote debug". I use standard port.
4) My parser continies to work... and stops when it reach
		    "DBG->enterRule(DBG, getGrammarFileName(), (const char
*)"statement");"  line
5) In this procedure parser transmits some info to debugger and waits.
6) I click "Step forward" and parser continies it work and returns.. In
AntlrWorks I tried to use breakpoints and "step over" button - no results.

-- 
Best regards,
Michael


More information about the antlr-interest mailing list