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

Re: Please help with syntax (long)



Dmitry Volpyansky wrote:
> I want to parse the following into a AST:
>... 
> I have tried so many different combinations, and some worked for
> various lines, but none have ever worked for all of them.

OK. I have modified your grammar a little, and attahced the result.  You
were almost there, but there were various little problems with it.  I
will let others help you identify and understand the various problems.
(Thanks in advance to anybody that helps;-)

> 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.

Lexer lookahead is not implemented (see known bugs on the web page). 
But, for such a simple grammar, you do not need lookahead.  In fact you
had too many tokens...

> 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

You're right.  It is very similar.

I hope this helps, and I really hope somebody will take some time to
explain the problems and how to avoid them (I'm quite short on time,
currently).

Have fun!

Etienne
PS: Commands used (SableCC 2.17.1):
java -jar sablecc.jar gr.sablecc --with-tools
javac decoder/tool/PrintTree.java
java decoder/tool/PrintTree test.input

-- 
----------------------------------------------------------------------
Etienne M. Gagnon, M.Sc.                     e-mail: egagnon@j-meg.com
Author of SableCC:                             http://www.sablecc.org/
and SableVM:                                   http://www.sablevm.org/
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;
*/

Helpers
  letter               = [['a' .. 'z'] + ['A' .. 'Z']];
  digit                = ['0' .. '9'];
  java_letter          = letter | '$' | '_';
  java_letter_or_digit = letter | digit | '$' | '_';
  parameter            = java_letter_or_digit | '/';
  cr                   = 13;
  lf                   = 10;
  tab                  = 9;
  space                = ' ';
  eol                  = cr | lf | cr lf;

Tokens
  identifier        = java_letter java_letter_or_digit*;
  open_parenthesis  = '(';
  close_parenthesis = ')';
  comma             = ',';
  number            = digit+;
  date              = digit+ '/' digit+ '/' digit+;
  price             = digit+ '.' digit digit;
  comment           = '#' [32 .. 127]* eol;
  whitespace        = space | tab | eol;

Ignored Tokens
  comment,
  whitespace;

Productions

file = 
  method?;

method =
  identifier open_parenthesis parameter_list? close_parenthesis;

parameter_list =
  parameter_comma* parameter;

parameter_comma =
  parameter comma;

parameter =
  {method}     method |
  {identifier} identifier |
  {number}     number |
  {date}       date |
  {price}      price;
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)
)
Start
 AFile
  AMethod
   TIdentifier: InboundOrder
   TOpenParenthesis: (
   AParameterList
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: OrderID
       TOpenParenthesis: (
       AParameterList
        ANumberParameter
         TNumber: 000001
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: Symbol
       TOpenParenthesis: (
       AParameterList
        AIdentifierParameter
         TIdentifier: DELL
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: OrderTimeInForce
       TOpenParenthesis: (
       AParameterList
        AParameterComma
         AIdentifierParameter
          TIdentifier: Gt
         TComma: ,
        ADateParameter
         TDate: 6/15/2001
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: RouteToStockExchange
       TOpenParenthesis: (
       AParameterList
        AMethodParameter
         AMethod
          TIdentifier: StockExchange
          TOpenParenthesis: (
          AParameterList
           AIdentifierParameter
            TIdentifier: NYSE
          TCloseParenthesis: )
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: AltRouteToStockExchange
       TOpenParenthesis: (
       AParameterList
        AMethodParameter
         AMethod
          TIdentifier: StockExchange
          TOpenParenthesis: (
          AParameterList
           AIdentifierParameter
            TIdentifier: NASDAQ
          TCloseParenthesis: )
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: Price
       TOpenParenthesis: (
       AParameterList
        APriceParameter
         TPrice: 100.00
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: OrderType
       TOpenParenthesis: (
       AParameterList
        AIdentifierParameter
         TIdentifier: Limit
       TCloseParenthesis: )
     TComma: ,
    AParameterComma
     AMethodParameter
      AMethod
       TIdentifier: OrderSide
       TOpenParenthesis: (
       AParameterList
        AIdentifierParameter
         TIdentifier: Buy
       TCloseParenthesis: )
     TComma: ,
    AMethodParameter
     AMethod
      TIdentifier: Quantity
      TOpenParenthesis: (
      AParameterList
       ANumberParameter
        TNumber: 1000
      TCloseParenthesis: )
   TCloseParenthesis: )
 EOF: