definition
module rg-layout
exports
  lexical syntax
    [\ \t\n] -> LAYOUT

  context-free restrictions
    LAYOUT? -/- [\ \t\n]

  sorts
    LongCom
    CommChar
    Asterisk

  lexical syntax
    "/*" CommChar+ "*/" -> LAYOUT
    ~[\*]    -> CommChar
    "*"      -> Asterisk
    Asterisk -> CommChar

  lexical restrictions
    Asterisk -/- [\/]

module rg-identifier
exports
  sorts Id QuotedIdPart
  syntax
    Id -> <Id-CF>

    <PlainIdChars-LEX>    -> Id {cons("Plain")}
    "[" QuotedIdPart* "]" -> Id {cons("Quoted")}

    <QuotedIdChars-LEX> -> QuotedIdPart {cons("Chars")}
     "\\" [\\\]]        -> QuotedIdPart {cons("EscapeSeq")}

  sorts PlainIdChars QuotedIdChars
  lexical syntax
    ~[\\\]]+           -> QuotedIdChars
    [a-zA-Z0-9\'\-\_]+ -> PlainIdChars

  lexical restrictions
    QuotedIdChars -/- ~[\\\]]
    PlainIdChars  -/- [a-zA-Z0-9\'\-\_]

module rg-nonterm
imports rg-identifier
exports
  sorts NonTerm
  context-free syntax
    Id         -> NonTerm {cons("Nonterm")}
    "<string>" -> NonTerm {cons("String")}
    "<int>"    -> NonTerm {cons("Int")}

module rg-term
imports rg-identifier
exports
  sorts Term IntConst
  context-free syntax
    Id       -> Term {cons("Term")}
    "<conc>" -> Term {cons("ConcTerm")}
    "<cons>" -> Term {cons("ConsTerm")}
    "<nil>"  -> Term {cons("NilTerm")}
    "<(" IntConst ")>" -> Term {cons("TupleTerm")}

    "<some>" -> Term {cons("SomeTerm")}
    "<none>" -> Term {cons("NoneTerm")}

    "<string>" -> Term {cons("StringTerm")}
    "<int>"    -> Term {cons("IntTerm")}

  lexical syntax
    [0-9]+   -> IntConst


module rtg
imports rg-term rg-nonterm rg-identifier rg-layout
hiddens
  context-free start-symbols RTG ProdRule TreeFN AnnoTreeFN NonTerm

exports
  sorts RTG Start ProdRule ProdRules AnnoTreeFN TreeFN Labelled 
  context-free syntax

    "regular" "tree" "grammar" Start ProdRules -> RTG {cons("RTG")}
    "start" NonTerm+ -> Start {cons("Start")}
    "productions" ProdRule+ -> ProdRules {cons("ProdRules")}

    NonTerm "->" {AnnoTreeFN "|"}+ -> ProdRule {cons("ProdRule")}

    TreeFN -> AnnoTreeFN
    TreeFN Labelled+ -> AnnoTreeFN {cons("Annotated")}

    Term "(" {AnnoTreeFN ","}* ")" -> TreeFN {cons("Appl")}
    NonTerm -> TreeFN {cons("Ref")}

    "." Term AnnoTreeFN -> Labelled {cons("Labelled")}

  %%%
   %% Special non-terminals used for tracing purposes
   %%%
  context-free syntax
    "{" {NonTerm ","}+ "}" -> NonTerm {cons("Set")}
    "<<" TreeFN ">>"       -> NonTerm {cons("Generated")}