JAvacc学習-jjtree例(二)

3081 ワード

PARSER_BEGIN(Eg1)

/** An Arithmetic Grammar. */
public class Eg1 {

  /** Main entry point. */
  public static void main(String args[]) {
    System.out.println("Reading from standard input...");
    Eg1 t = new Eg1(System.in);
    try {
      SimpleNode n = t.Start();
      n.dump("");
      System.out.println("Thank you.");
    } catch (Exception e) {
      System.out.println("Oops.");
      System.out.println(e.getMessage());
      e.printStackTrace();
    }
  }
}

PARSER_END(Eg1)


SKIP :
{
  " "
| "\t"
| "
" | "\r" // // /* | <"//" (~["
","\r"])* ("
"|"\r"|"\r
")> | <"/*" (~["*"])* "*" (~["/"] (~["*"])* "*")* "/"> } TOKEN : /* LITERALS */ { < INTEGER_LITERAL: <DECIMAL_LITERAL> (["l","L"])? | <HEX_LITERAL> (["l","L"])? | <OCTAL_LITERAL> (["l","L"])? > | // TOKEN < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | // TOKEN < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | // TOKEN < #OCTAL_LITERAL: "0" (["0"-"7"])* > } TOKEN : /* IDENTIFIERS */ { < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* > | < #LETTER: ["_","a"-"z","A"-"Z"] > | < #DIGIT: ["0"-"9"] > } /** Main production. */ SimpleNode Start() : {} { Expression() ";" //jjthis simplenode { return jjtThis; } } /** An Expression. */ void Expression() : {} { AdditiveExpression() } /** An Additive Expression. */ void AdditiveExpression() : {} { MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )* } /** A Multiplicative Expression. */ void MultiplicativeExpression() : {} { // (IDENTIFIER|INTEGER_LITERAL)*( ( "*" | "/" | "%" ) (IDENTIFIER|INTEGER_LITERAL)* UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )* } /** TOKEN */ void UnaryExpression() : {} { "(" Expression() ")" | Identifier() | Integer() } /** An Identifier. */ void Identifier() : {} { <IDENTIFIER> } /** An Integer. */ void Integer() : {} { <INTEGER_LITERAL> }