« Return to Thread: Any characters between two tokens

Any characters between two tokens

by Jeff higgins :: Rate this Message:

Reply to Author | View in Thread


Hi,

I'm a beginner with JavaCC and am having
problems understanding lexical states.

I provide the text: E( ... )E
to the parser below and expect to see the output:

(5)E(
  ...

or:

(5)E(
  ... )E

but instead I see:

(5)E(
specialToken == null

I expect this to work similarly to the
MULTI_LINE_COMMENT in the Java 1.5 grammar.

Will someone help by explaining what I'm doing wrong?

Thanks,
Jeff Higgins


options {
  JDK_VERSION = "1.5";
}
PARSER_BEGIN(EParser)
package scratch;

import java.io.*;

public class EParser {
  public static void main(String args[])
  throws ParseException {
  Expression exp = null;
  try {
      EParser parser = new EParser(
        new BufferedReader(new FileReader(args[0])));
      exp = parser.expression();
      if ( exp != null )
        System.out.println( exp.toString() );
      else
        System.out.println( "exp == null" );
  } catch(Exception e) {
      e.printStackTrace();
    }    
  }
}
PARSER_END(EParser)

<DEFAULT, IN_EXP>
SKIP : { " "|"\r"|"\t"|"\n" }

TOKEN : { < EXP_OPEN: "E(" > : IN_EXP }

<IN_EXP>MORE : { < EO: <EXP_OPEN> > : IN_EXP }

<IN_EXP> SPECIAL_TOKEN : { < EXP_CLOSE: ")E" > : DEFAULT }

<IN_EXP> MORE : { < ~[] > }

Expression expression():
{
  StringBuilder sb = new StringBuilder();
  Expression ret = null; }
{
  <EXP_OPEN>
  { sb.append("(");
    sb.append(token.kind);
    sb.append(")");
    sb.append(token.image);
    sb.append("\n"); }
  { if(token.specialToken != null)
      sb.append(token.specialToken.image);
    else
      sb.append("specialToken == null"); }
  { ret = new Expression( sb.toString() ); }
  { return ret; }
}






     

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: Any characters between two tokens