This is a flashcard version of Rats! original documentation. Refer there for more details.

Rats! Body

Intro         <- ModuleDecl Dependency* Header? Body? Footer? Option?

Rats! Syntax

Production    <- Full / Addition / Removal / Override
Full          <- PAttributes QName Identifier EQUAL Choice SEMICOLON
Addition      <- QName Identifier PLUSEQUAL
                 ( SName ELLIPSIS SLASH Choice SEMICOLON
                 / Choice SLASH SName ELLIPSIS SEMICOLON )
Removal       <- QName Identifier MINUSEQUAL
                 SName ( COMMA SName )* SEMICOLON
Override      <- QName Identifier COLONEQUAL Choice SEMICOLON
                 / QName Identifier COLONEQUAL ELLIPSIS SLASH Choice SEMICOLON
                 / QName Identifier COLONEQUAL Choice SLASH ELLIPSIS SEMICOLON
                 / PAttributes QName Identifier COLONEQUAL ELLIPSIS SEMICOLON
PAttributes   <- &(QName Identifier EQUAL) / Attribute PAttributes
Choice        <- Sequence (SLASH Sequence)*
Sequence      <- !(SName ELLIPSIS / ELLIPSIS) SName? Voided*
Voided        <- ("void" Spacing COLON)? Prefix
Prefix        <- (AND / NOT / CARET / Identifier COLON
                 / StringLit Spacing COLON)? Suffix
Suffix        <- Primary (QUESTION / STAR / PLUS)?
Primary       <- QName / Literal / Action / OPEN Choice CLOSE

Action        <- '{' ActionBody* '}' Spacing
ActionBody    <- Action / CharLit / StringLit / MLComment / SLComment / !'}' .

Attribute     <- Identifier (OPEN AttValue CLOSE)?
AttValue      <- Integer / QName / StringLit Spacing

Remarks and common pitfalls


Comment
/* comment */
// comment
Character literal 'a'
String literal "hello"
Escape sequence \n, \\, \u00ff, \[, \-, \]
Any character constant _
Character class, grouping [], ()
Option operator, zero-or-more and one-or-more operators ?,*,+
Matching syntactic predicate
transient void Directive =
    [#] [ ] line:DecimalConstant [ ] ["] file:FileName ["]
      f1:" 1"? f2:" 2"? f3:" 3"? f4:" 4"? &LineTerminator
      { yyState.lineMarker(line, file, f1, f2, f3, f4); }
  / [#] Space* "pragma " pragma:Pragma &LineTerminator
      { yyState.pragma(pragma); }

  / [#] Space* "ident" Space+ s:StringLiteral &LineTerminator
      { yyState.ident(s); }
  / /* Empty */
  ;
Not matching syntactic predicate
transient String Pragma   = ( ![\n\r] _ )* ;
Semantic predicate
generic AbstractDeclarator =
  p:Pointer? 
  d:DirectAbstractDeclarator 
  &{ (null != p) || (null != d) } ;
generic String Keyword = vtext:Identifier &{ "begin".equals (vtext) }
Voiding operator
generic ParameterList =
   ParameterDeclaration (void:",":Symbol ParameterDeclaration)* ;
Parser actions
public String ByteString =
  n:Integer ':' ^{ yyResult = parseChars(n, yyStart, yyBase); } ;
Production, binding, and
Text-matching expression
public FullProduction FullProduction =
    atts : ProductionAttributes
    type : Name
      nt : UnqualifiedNonTerminal
     "=" : Symbol
  choice : Choice
     ";" : Symbol
  {
    yyValue = new FullProduction (atts.list (), 
                                  type, 
                                  nt, 
                                  choice);
  }
Passing the semantic value through
Action Header = "header":Word yyValue:Action ;
Options, repetitions and nested choices
   / '['
      l:( c1:ClassChar '-' c2:ClassChar
         { 
            yyValue = new CharRange(Utilities.unescape(c1).charAt(0),
                                    Utilities.unescape(c2).charAt(0));
         }
        / c1:ClassChar
         {
            yyValue = new CharRange(Utilities.unescape(c1).charAt(0));
         }
        )*
     ']' Spacing
      { yyValue = new CharClass(l.list()); }
Generic type
generic LogicalAndExpression =
     LogicalAndExpression void:"&&":Symbol BitwiseOrExpression
   / BitwiseOrExpression
   ;
Void and text-only productions
Transient productions
transient void Spacing =
  (Space / LineTerminator / TraditionalComment/ EndOfLineComment)*
  ;
String Symbol = SymbolCharacters Spacing ;

Rats! Module

ModuleDecl    <- "module" FSpacing ModuleRef SEMICOLON
Dependency    <- Modification / Instantiation / Import
Modification  <- "modify" FSpacing ModuleRef ModuleTarget? SEMICOLON
Instantiation <- "instantiate" FSpacing ModuleRef ModuleTarget? SEMICOLON
Import        <- "import" FSpacing ModuleRef ModuleTarget? SEMICOLON
ModuleRef     <- QName ModuleParams?
ModuleParams  <- OPEN ( QName (COMMA QName)* )? CLOSE
ModuleTarget  <- "as" FSpacing QName


Rats! Global Actions and Options

Header        <- "header" Spacing Action
Body          <- "body" Spacing Action
Footer        <- "footer" Spacing Action
Option        <- "option" FSpacing Attribute (COMMA Attribute)* SEMICOLON


Rats! Lexer

QName         <- Id ('.' Id)* Spacing
SName         <- LESS Id GREATER
Identifier    <- Id Spacing
Id            <- [a-zA-Z] [a-zA-Z0-9]*

Literal       <- ('_' / CharLit / StringLit / Class) Spacing
CharLit       <- ['] (Escape / !['\\] .)  [']
StringLit     <- ["] (Escape / !["\\] .)* ["]
Class         <- '[' (Char '-' Char / Char)* ']'
Char          <- Escape / ![-\]\\] .
Escape        <- '\\' [btnfr"'\[\\\]-] / '\\' 'u' HexQuad / OctalEscape
OctalEscape   <- '\\' ([0-3] OctDigit OctDigit / OctDigit OctDigit / OctDigit)

Integer       <- (HexNumber / OctNumber / Number) Spacing
HexNumber     <- '0' [xX] HexDigit+
HexQuad       <- HexDigit HexDigit HexDigit HexDigit
HexDigit      <- [0-9a-fA-F]
Number        <- '0' / NZDigit Digit*
NZDigit       <- [1-9]
Digit         <- [0-9]
OctNumber     <- '0' OctDigit+
OctDigit      <- [0-7]

ELLIPSIS      <- "..." Spacing
PLUSEQUAL     <- "+=" Spacing
MINUSEQUAL    <- "-=" Spacing
COLONEQUAL    <- ":=" Spacing
COMMA         <- ',' Spacing
EQUAL         <- '=' Spacing
SLASH         <- '/' ![/*] Spacing
AND           <- '&' Spacing
NOT           <- '!' Spacing
CARET         <- '^' Spacing
COLON         <- ':' Spacing
QUESTION      <- '?' Spacing
STAR          <- '*' Spacing
PLUS          <- '+' Spacing
OPEN          <- '(' Spacing
CLOSE         <- ')' Spacing
SEMICOLON     <- ';' Spacing
LESS          <- '<'
GREATER       >- '>' Spacing

Spacing       <- (Space / SLComment / MLComment)*
FSpacing      <- (Space / SLComment / MLComment)+
Space         <- ' ' / '\t' / '\f' / EOL
SLComment     <- "//" (![\n\r] .)* EOL
MLComment     <- "/*" ('*' !'/' / !'*' .)* "*/"
EOL           <- '\r' '\n' / '\r' / '\n'
EOF           <- !.