Skip to content

Instantly share code, notes, and snippets.

@markmarch
Created September 21, 2011 21:45
Show Gist options
  • Save markmarch/1233408 to your computer and use it in GitHub Desktop.
Save markmarch/1233408 to your computer and use it in GitHub Desktop.
Rats! example
/**
* Syntax Analysis production
* void means do not create AST
* / means ordered alternative
*/
module Sample;
header { import java.util.*;}
body { public static final Set<String>
KEYWORDS = new HashSet<String>(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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment