[antlr-interest] CCCP - Compiler Compiler Challenged Person

Nigel Sheridan-Smith nbsherid at secsme.org.au
Thu Dec 23 13:51:16 PST 2004


 

Literals are case-sensitive. "If" is different to "if". so it matches IDENT
then looks for ASSIGN.

 

Nigel

--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435
  

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Matt Wisner
Sent: Friday, 24 December 2004 6:09 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] CCCP - Compiler Compiler Challenged Person

 

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/20041224/3e9ac541/attachment-0001.html


More information about the antlr-interest mailing list