[antlr-interest] CCCP - Compiler Compiler Challenged Person

Matt Wisner mwisner69 at comcast.net
Thu Dec 23 11:08:41 PST 2004


Hi,

I've scanned the ANTLR reference manual and poured over the samples but, for
the life of me, cannot get the simplest "conditional logic" parser/tree
parser working with ANTLR.   I've included an example of the source code I'm
trying to parse/walk along with my defs for my ANTLR parser and tree parser
below.  I've also included the output of getAST().toStringList() where the
"if" is obviously missing.   If anyone can shed some light on the nature of
my problem I would greatly appreciate it.

 

Thanks,

Matt

----------------------------------------------------------------------------
-------------

 

Source file:

 

line 3:4: expecting ASSIGN, found '('

ast:  ( = $var1 "batman" ) ( = $var2 $var1 )

 

Source:

 

$var1 = "batman"

If(1)

{

            $var2 = $var1

}

-----------------------------------------------------------------

 

 

Parser and Tree Parser defs:

 

 

class Parser_g extends Parser;

 

options

{

    k = 2;

    importVocab = RulesLexer;

    exportVocab = RulesParser;

    buildAST = true;

    ASTLabelType = "antlr.CommonAST";

}

 

tokens

{

    LITERAL_include="include";

}

 

{

protected SymbolTable m_symtab = null;

private Logger log = TapCollector.getLogger();

}

 

rulesFile

    :       ( rulesSection )? EOF!

    ;

 

rulesSection

        :   ( statement )*

        ;

 

//--------------------------------

//   ||

//   ||

//  _||_    S t a t e m e n t s

//  \  /       

//   \/

//--------------------------------

 

statement

        :       assignmentStatement          { #statement = #as; }

        |       cs:conditionalStatement         { #statement = #cs; }

        ;

 

assignmentStatement

            :           IDENT ASSIGN^ expression

            ;

 

conditionalStatement

        :       "if"^ LPAREN! expression RPAREN! LCURLY! assignmentStatement
RCURLY!

        ;

 

//----------------------------------

//   ||

//   ||

//  _||_    E x p r e s s i o n s

//  \  /       

//   \/

//----------------------------------

 

expression

            :           ( IDENT | STRING_LITERAL | DECIMAL_LITERAL )

            ;

 

 

//----------------------------------

 

quotedString returns [String qs]

{

    qs = "";

}

    :   sl:STRING_LITERAL

            {

                qs = sl.getText();

                qs = qs.substring(1, qs.length()-1);

            }

    ;

 

 

class TreeParser_g extends TreeParser;

 

options

{

    importVocab = RulesParser;

    exportVocab = Rules;

    buildAST = true;

    ASTLabelType = "antlr.CommonAST";

}

 

{

protected SymbolTable m_symtab = null;

private Logger log = TapCollector.getLogger();

 

private String unquoteQuotedString(String in)

{

    return in.substring(1, in.length()-1);

}

 

}

 

rulesSection returns [SymbolTable symtab]

{

    symtab = new SymbolTable();

    m_symtab = symtab;

}

    :   ( statement )*

    ;

 

statement

        :       assignmentStatement     { log.info("<< asgnStmt >>"); }

        |       ifStatement             { log.info("<< ifStmt   >>"); }

        ;

 

assignmentStatement

{

    String val = "?";

}

    :   #( ASSIGN id:IDENT val=expression )

            {

                log.info("asgnStmt: " + id.getText() + " := " + val);

                try

                {

                    // TODO:  if 'id.getText()' does not start with $|@|%
then throw exception?

                    m_symtab.updateEntry(id.getText(), val);

                }

                catch(Exception mjw_e)

                {

                    log.severe("EXCEPTION: " + mjw_e.toString());

                }

            }

    ;

 

ifStatement

{

    String e = "?";

}

    :   #( LITERAL_if e=expression assignmentStatement)

            {

                log.info("ifStmt: e=" + e);

            }

    ;

 

expression returns [String retval]

{

    retval = "";

    SymbolTableEntry entry = null;

}

    :  (    id:IDENT                

                {

                    try

                    {

                        entry = m_symtab.findEntry(id.getText());

                        if(entry == null)

                        {

                            m_symtab.addEntry(id.getText(), "");

                        }

                        retval = entry.getValue();

                    }

                    catch(Exception mjw_e)

                    {

                        log.severe("Exception: " + mjw_e.toString());

                    }

                }

        |   retval=quotedString

        |   dc:DECIMAL_LITERAL    

                {

                    retval = dc.getText().toString(); 

                }

        )

    ;

 

quotedString returns [String qs]

{

    qs = "";

}

    :   sl:STRING_LITERAL

            {

                qs = sl.getText();

                qs = qs.substring(1, qs.length()-1);

            }

    ;

 

 

 

 

 

 

_____________________

\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20041223/72e2455f/attachment-0001.html


More information about the antlr-interest mailing list