/** * Syntax Analysis production * void means do not create AST * / means ordered alternative */ module Sample; header { import java.util.*;} body { public static final Set KEYWORDS = new HashSet(Arrays.asList("if", "else"));} public void program = WS statement EOF; void statement = IF LPAREN expr RPAREN statement ELSE statement / IF LPAREN expr RPAREN statement / SEMI; void expr = ID REL_OP ID; // simple production , are all void , and will be dropped in AST void IF = "if" void ELSE = "else" void LPAREN = "(" void RPAREN = ")" void SEMI = ";" // whitespace void WS = ([ \t\n\f]/COMMENT)˚ /** &: positive predicate !: negative predicate neither predicate will actually consume the character **/ void COMMENT = "/*"(!"*"_/"*"!"/")˚"*/" void EOF = !_; void REL_OP = ("<="/"<"/">="/">") WS; // order alternation, cause "<" is the prefix of "<=" void ID = i:ID_INTERNAL WS &{!KEYWORDS.contains(i)}; void ID_INTERNAL = [a-zA-Z][a-zA-Z0-9]