|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Any characters between two tokensHi, 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@... |
|
|
Re: Any characters between two tokensOn 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@... |
| Free embeddable forum powered by Nabble | Forum Help |