[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Please help with syntax (long)



Sorry for the length:

Caveat: I read the thesis 5 times, read a Java Parser book that's on
the market now (doesn't use any existing tools), and I am still lost.

I am trying to dynamically build Java objects to load test a system I
am working on. 

Couple of weeks ago I wrote a simple hand-parser for the file below,
a simple expression tree, and used a visitor to walk the tree, and
Java reflection to do what I will describe below -- then I decided to
switch to SableCC because I thought it would be a good learning
experience.  

Two weeks later, I am desperate for help.

----

I want to parse the following into a AST:

InboundOrder(
   OrderID(000001),
   Symbol(DELL),
   OrderTimeInForce(Gt,6/15/2001),
   RouteToStockExchange(StockExchange(NYSE)),
   AltRouteToStockExchange(StockExchange(NASDAQ)),
   Price(100.00),
   OrderType(Limit),
   OrderSide(Buy),
   Quantity(1000)
)
 

I have tried so many different combinations, and some worked for
various lines, but none have ever worked for all of them.

What finally caused me to send this email is that I decided to use
the lookahead (still there on lines 41 and 42) to try to fix my
problems, only to be thwarted again.

If anybody has any ideas, I will be very grateful.  It looks like to
me that this is very similar to parsing a normal C or Java program
(and I read both grammar files at least once), so I think that I am
just completely misunderstanding something very basic.

Grammar File:
-----------------

Package decoder;

Helpers

    every_character         = [32 .. 127];
    letter                  = [['a' .. 'z'] + ['A' .. 'Z']];
    digit                   = ['0' .. '9'];
    digits                  = digit+;
    java_letter             = letter | '$' | '_';
    java_letter_or_digit    = letter | digit | '$' | '_';
    parameter               = java_letter_or_digit | '/';
    cr                      = 13;
    lf                      = 10;
    tab                     = 9;
    space                   = ' ';
    hash                    = '#';
    open_parenthesis        = '(';
    close_parenthesis       = ')';
    comma                   = ',';
    slash                   = '/';
    eol                     = cr | lf | cr lf;
    valid_characters        = [32 .. 127];
    whitespace              = space | tab;

Tokens

    comma                   = comma;
    open_parenthesis        = open_parenthesis;
    close_parenthesis       = close_parenthesis;
    whitespace              = whitespace;
    eol                     = eol;
    comment                 = '#' [32 .. 127]* eol;
    empty_line              = eol whitespace* eol;
    parameter               = parameter / comma | close_parenthesis;
    identifier              = java_letter java_letter_or_digit* /
open_parenthesis;

Ignored Tokens

    empty_line,
    comment,
    whitespace,
    eol;

Productions

doit =
    {identifier} method |
    {empty}
;

method =

    {multi_simple_parameters_method}        T.identifier
T.open_parenthesis P.parameter_comma+ T.parameter T.close_parenthesis


//    {empty_method}                          identifier
open_parenthesis close_parenthesis                         |
//    {one_parameter_method}                  identifier
open_parenthesis parameter close_parenthesis               |
//    {one_function_method}                   identifier
open_parenthesis method close_parenthesis               |
//    {multi_function_method}                 identifier
open_parenthesis method_comma+ method close_parenthesis |
//    {multi_function_parameters_method}      identifier
open_parenthesis parameter_comma* parameter* method_comma* method*
close_parenthesis |
;

parameter_comma =
    parameter comma
;

method_comma =
    method comma
;


----
Java File
----

package decoder;

import java.io.*;
import java.util.*;

import decoder.analysis.*;
import decoder.parser.*;
import decoder.lexer.*;
import decoder.node.*;

public class decode
{
   public final static void main(final String[] args) throws
Exception
   {
       final Parser parser = new PrintParser(new PrintLexer(new
PushbackReader(new BufferedReader(new
FileReader("d:/decoder/test.orders"), 1024), 1024)));

       final Start tree = parser.parse();

       tree.apply(new decodeAdapter());
   }
}

class PrintLexer extends Lexer
{
   PrintLexer(final PushbackReader reader)
   {
       super(reader);
   }

   protected void filter()
   {
       if (token.getClass() == decoder.node.TWhitespace.class ||
           token.getClass() == decoder.node.TEol.class        ||
           token.getClass() == decoder.node.TComment.class
           )
       {
           return;
       }

       System.out.println(token.getClass() + ", text : [" +
token.getText() + "]");
   }
}

class PrintParser extends Parser
{
   public PrintParser(final Lexer lexer)
   {
       super(lexer);
   }

   protected void filter() throws ParserException, LexerException,
IOException
   {
//       System.out.println(node);
   }
}

class decodeAdapter extends DepthFirstAdapter
{
   public void caseTIdentifier(final TIdentifier node)
   {
       System.out.println("caseTIdentifier: " + node);
   }
}


__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/