« Return to Thread: Any characters between two tokens

Re: Any characters between two tokens

by Jeff higgins :: Rate this Message:

Reply to Author | View in Thread



On Sun, 3/1/09, Jeff Higgins wrote:

> Hi,
>
> I'm a beginner with JavaCC and am having
> problems understanding lexical states.
>
[snip]

The following solves the immediate problem,
but it seems not pretty somehow.
I hope to understand how to accomplish this
using a lexical state solution.

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)

SKIP : { " "|"\r"|"\t"|"\n" }

TOKEN :
{
  < EXP:
    "E"
    (" "|"\r"|"\t"|"\n")*
    "("
    (~[])*
    ")"
    (" "|"\r"|"\t"|"\n")*
    "E"
  >
}

Expression expression():
{
  String exp;
  Expression ret = null;
}
{
  <EXP>
  { exp = token.image; }
  { int b = exp.indexOf("(") + 1; }
  { int e = exp.lastIndexOf(")"); }
  { ret = new Expression( exp.substring(b,e) ); }
  { return ret; }
}






     

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

 « Return to Thread: Any characters between two tokens