definition
module languages/sdf2/syntax/Aliases

imports languages/sdf2/syntax/Kernel

exports
  sorts Alias Aliases
  context-free syntax 
    "aliases" Aliases              -> Grammar  {cons("aliases")}
    Symbol arrow:"->" alias:Symbol -> Alias    {cons("alias")}
    list:Alias*                    -> Aliases



module languages/sdf2/syntax/Restrictions

imports languages/sdf2/syntax/CC

exports
  sorts Lookahead Lookaheads Restriction Restrictions

  context-free syntax
    CharClass                          -> Lookahead  {cons("char-class")}
    head:CharClass "." tail:Lookaheads -> Lookahead  {cons("seq")}

    Lookahead                                -> Lookaheads  {cons("single")}
    "left":Lookaheads "|" "right":Lookaheads -> Lookaheads  {cons("alt"),right}
    "(" Lookaheads ")"                       -> Lookaheads  {bracket}
    "[[" list:{Lookahead ","}* "]]"          -> Lookaheads  {cons("list")}

    Symbols "-/-" Lookaheads -> Restriction  {cons("follow")}

    list:Restriction* -> Restrictions

    "restrictions" Restrictions -> Grammar  {cons("restrictions")}

    "reject" -> Attribute  {cons("reject")}
    "prefer" -> Attribute  {cons("prefer")}
    "avoid"  -> Attribute  {cons("avoid")}

  context-free priorities    
    CharClass "." Lookaheads    -> Lookahead >
    Lookaheads "|" Lookaheads   -> Lookaheads


module languages/sdf2/syntax/Labels

imports languages/sdf2/syntax/Kernel 
        basic/StrCon

exports 
  sorts Label

  context-free syntax
    StrCon -> Label {cons("quoted")}
    IdCon  -> Label {cons("unquoted")}

    Label ":" Symbol -> Symbol  {cons("label")}

module languages/sdf2/syntax/Literals

imports languages/sdf2/syntax/Kernel 
imports basic/StrCon
imports basic/IdentifierCon

exports
  sorts FunctionName SingleQuotedStrCon SingleQuotedStrChar
  lexical syntax

    "\\n"                      -> SingleQuotedStrChar {cons("newline")}
    "\\t"                      -> SingleQuotedStrChar {cons("tab")}
    "\\'"                      -> SingleQuotedStrChar {cons("quote")}
    "\\\\"                     -> SingleQuotedStrChar {cons("backslash")}
    "\\" a:[0-9]b:[0-9]c:[0-9] -> SingleQuotedStrChar {cons("decimal")}
    ~[\0-\31\n\t\'\\]          -> SingleQuotedStrChar {cons("normal")}

    [\'] chars:SingleQuotedStrChar* [\']   -> SingleQuotedStrCon  {cons("default")}

  context-free syntax
    IdCon  -> FunctionName {cons("unquoted-fun")}
    StrCon -> FunctionName {cons("quoted-fun")}

  context-free syntax
    string:StrCon                -> Symbol {cons("lit")}
    ci-string:SingleQuotedStrCon -> Symbol {cons("ci-lit")}

    fun:FunctionName "(" arguments:{Symbol ","}* ")" "->" result:Symbol Attributes 
	-> Production  {avoid, cons("prefix-fun")}

module languages/sdf2/syntax/Start-Symbols

imports languages/sdf2/syntax/Kernel

exports
  context-free syntax
  
    "start-symbols" Symbols                -> Grammar {cons("kernel-start-symbols")} 
    "lexical" "start-symbols" Symbols      -> Grammar {cons("lexical-start-symbols")}
    "context-free" "start-symbols" Symbols -> Grammar {cons("context-free-start-symbols")}

module languages/sdf2/syntax/Sorts

imports languages/sdf2/syntax/Kernel

exports
  sorts Sort
  lexical syntax
    head:[A-Z]                          -> Sort {cons("one-char")}
    head:[A-Z] middle:[A-Za-z0-9\-]* last:[A-Za-z0-9] -> Sort 
	{cons("more-chars")}

  context-free syntax
    Sort                                    -> Symbol  {cons("sort")}
    Sort "[[" parameters:{Symbol ","}+ "]]" -> Symbol  {cons("parameterized-sort")}

    "sorts" Symbols -> Grammar {cons("sorts")}

  lexical restrictions
    Sort -/- [A-Za-z0-9]

module languages/sdf2/syntax/Character



exports
  sorts Character NumChar ShortChar
  lexical syntax
    [\\] number:[0-9]+                     -> NumChar   {cons("digits")}
    character:[a-zA-Z0-9]                  -> ShortChar {cons("regular")}
    [\\] escape:~[\0-\31A-Za-mo-qsu-z0-9]  -> ShortChar {cons("escaped")}

  context-free syntax
    NumChar   -> Character {cons("numeric")}
    ShortChar -> Character {cons("short")}
    "\\TOP"   -> Character {cons("top")}
    "\\EOF"   -> Character {cons("eof")}
    "\\BOT"   -> Character {cons("bot")}
    "\\LABEL_START" -> Character {cons("label_start")}

module languages/sdf2/syntax/Character-Class

imports languages/sdf2/syntax/Character

exports
  sorts CharClass CharRange CharRanges OptCharRanges 
  context-free syntax 
    Character                           -> CharRange
    start:Character "-" end:Character   -> CharRange            {cons("range")}

    CharRange                                   -> CharRanges
    "left":CharRanges "right":CharRanges        -> CharRanges   {cons("conc"),right,memo}
    "(" CharRanges ")"                          -> CharRanges   {bracket}

                              -> OptCharRanges  {cons("absent")}
    CharRanges                -> OptCharRanges  {cons("present")}

    "[" OptCharRanges "]"                    -> CharClass  {cons("simple-charclass")}
    "~" CharClass                            -> CharClass  {cons("comp")}
    "left":CharClass "/" "right":CharClass   -> CharClass  {cons("diff"),left,memo}
    "left":CharClass "/\\" "right":CharClass -> CharClass  {cons("isect"),left,memo}
    "left":CharClass "\\/" "right":CharClass -> CharClass  {cons("union"),left}
    "(" CharClass ")"                        -> CharClass  {bracket, avoid}

  context-free priorities
    "~" CharClass             -> CharClass >
    CharClass "/" CharClass   -> CharClass >
    CharClass "/\\" CharClass -> CharClass >
    CharClass "\\/" CharClass -> CharClass           


module languages/sdf2/syntax/CC

imports languages/sdf2/syntax/Character-Class 
        languages/sdf2/syntax/Kernel

exports
  context-free syntax
    CharClass -> Symbol {cons("char-class")}

module languages/sdf2/syntax/Priority

imports languages/sdf2/syntax/Kernel

exports
  sorts Associativity ArgumentIndicator Group Priority Priorities

  context-free syntax
    "left"      -> Associativity  {cons("left")}
    "right"     -> Associativity  {cons("right")}
    "non-assoc" -> Associativity  {cons("non-assoc")}
    "assoc"     -> Associativity  {cons("assoc")}

    "bracket"     -> Attribute  {cons("bracket")}
    Associativity -> Attribute  {cons("assoc")}

    "<" arguments:{NatCon ","}+ ">" -> ArgumentIndicator {cons("default")}

    Group ArgumentIndicator               -> Group {non-assoc,cons("with-arguments")}
    Group "."                             -> Group {non-assoc,cons("non-transitive")}

    Production                            -> Group  {cons("simple-group")}
    "{" Productions "}"                   -> Group  {cons("prods-group")}
    "{" Associativity ":" Productions "}" -> Group  {cons("assoc-group")}

    list:{Group ">"}+                                    -> Priority  {cons("chain")}
    "left":Group Associativity "right":Group             -> Priority  {cons("assoc")}
%%    Production "<" arguments:{NatCon ","}+ ">" ">" Group -> Priority  {cons("argument")}

    list:{Priority  ","}* -> Priorities

    "priorities" Priorities  -> Grammar  {cons("priorities")}

module languages/sdf2/syntax/Lifting

imports languages/sdf2/syntax/Kernel

exports
  context-free syntax
    left-quote:"`" Symbol right-quote:"`" -> Symbol  {cons("lifting")}

module languages/sdf2/syntax/Regular

imports languages/sdf2/syntax/Kernel 
imports languages/aterm/syntax/IntCon

exports
  context-free syntax
    "(" ")"                                       -> Symbol {cons("empty")}
    "(" head:Symbol tail:Symbol+ ")"              -> Symbol {cons("seq")}
    Symbol "?"                                    -> Symbol {cons("opt")}
    Symbol "+"                                    -> Symbol {cons("iter")}
    Symbol "*"                                    -> Symbol {cons("iter-star")}
    "{" Symbol sep:Symbol "}" "+"                 -> Symbol {cons("iter-sep")}
    "{" Symbol sep:Symbol "}" "*"                 -> Symbol {cons("iter-star-sep")}
    "<" head:Symbol "," rest:{Symbol ","}+ ">"    -> Symbol {cons("tuple")}
    "(" arguments:Symbols "=>" results:Symbol ")" -> Symbol {cons("func")}
    "left":Symbol "|" "right":Symbol              -> Symbol {right,cons("alt")}
    "(" "left":Symbol "->" "right":Symbol ")"     -> Symbol {cons("strategy")}

  context-free priorities
    {Symbol "?"               -> Symbol
    Symbol "*"                -> Symbol
    Symbol "+"                -> Symbol} >
    Symbol "|" Symbol         -> Symbol       


module languages/sdf2/syntax/Renaming

imports languages/sdf2/syntax/Kernel

exports
  sorts Renaming Renamings
  context-free syntax 
    "[" list:Renaming* "]" -> Renamings  {cons("renamings")}

    from:Symbol "=>" to:Symbol                   -> Renaming  {cons("symbol")}
    from-prod:Production "=>" to-prod:Production -> Renaming  {cons("production")}

module languages/sdf2/syntax/Modules

imports languages/sdf2/syntax/Kernel 
        languages/sdf2/syntax/Renaming

hiddens
  sorts ModuleWord
exports
  context-free start-symbols Module

  sorts ModuleId ModuleName Import Imports
        Section Sections Module Definition ImpSection

  lexical syntax    
    letters:[A-Za-z0-9\_\-]+ -> ModuleWord  {cons("word")}

    ModuleWord              -> ModuleId  {cons("leaf")}
    sep:"/" basename:ModuleId            -> ModuleId  {cons("root")}
    dirname:ModuleWord sep:"/" basename:ModuleId -> ModuleId  {cons("path")}
         
  context-free syntax
    list:Module* -> Definition

    "module" ModuleName list:ImpSection* Sections -> Module  {cons("module")}

    "exports" Grammar            -> Section  {cons("exports")}
    "hiddens" Grammar            -> Section  {cons("hiddens")}

    list:Section*                -> Sections

    ModuleId                        -> ModuleName  {cons("unparameterized")}
    ModuleId "[" params:Symbols "]" -> ModuleName  {cons("parameterized")}

    "id" "(" ModuleName ")" -> Attribute  {cons("id")}

    "imports" list:Imports -> ImpSection  {cons("imports")}

    ImpSection -> Grammar  {cons("imp-section")}

    list:Import* -> Imports

    ModuleName            -> Import  {cons("module")}
    ModuleName Renamings  -> Import  {cons("renamed-module")}
    "(" Import ")"        -> Import  {bracket}

  lexical restrictions
    ModuleWord -/- [A-Za-z0-9\_\-]
    ModuleId -/- [\/]

module languages/sdf2/syntax/Basic

imports languages/sdf2/syntax/Kernel

exports
  context-free syntax
    "lexical" "syntax"      Productions -> Grammar  {cons("lexical-syntax")}
    "context-free" "syntax" Productions -> Grammar  {cons("context-free-syntax")}
    "variables"             Productions -> Grammar  {cons("variables")}
    "lexical" "variables"   Productions -> Grammar  {cons("lexical-variables")}

    "<" Symbol "-CF"  ">"               -> Symbol  {cons("cf")}
    "<" Symbol "-LEX" ">"               -> Symbol  {cons("lex")}
    "<" Symbol "-VAR" ">"               -> Symbol  {cons("varsym")}

    "LAYOUT"                            -> Symbol  {cons("layout")}

module languages/sdf2/syntax/Grammars

imports basic/Comments

exports
  sorts Grammar
  context-free syntax    
    "(/)"                          -> Grammar  {cons("empty-grammar")}
    "left":Grammar "right":Grammar -> Grammar  {assoc,cons("conc-grammars")}
    "(" Grammar ")"                -> Grammar  {bracket}

module basic/Comments

imports
  basic/Whitespace

exports
  lexical syntax
    "%%" line:~[\n]* "\n"	-> LAYOUT {cons("line"),category("Comment")}
    "%" content:~[\%\n]+ "%"	-> LAYOUT {cons("nested"),category("Comment")}
  context-free restrictions
    LAYOUT? -/- [\%]

module languages/sdf2/syntax/Symbols

imports basic/Comments

exports
  sorts Symbol Symbols

  context-free syntax
    "(" Symbol ")" -> Symbol {bracket}

    list:Symbol* -> Symbols



module basic/IdentifierCon

exports

sorts IdCon

lexical syntax

head:[A-Za-z] tail:[A-Za-z\-0-9]* -> IdCon {cons("default")}

lexical restrictions

IdCon -/- [A-Za-z\-0-9]

module basic/StrCon

exports

sorts StrCon StrChar

lexical syntax
    "\\n"                      -> StrChar {cons("newline")}
    "\\t"                      -> StrChar {cons("tab")}
    "\\\""                     -> StrChar {cons("quote")}
    "\\\\"                     -> StrChar {cons("backslash")}
    "\\" a:[0-9]b:[0-9]c:[0-9] -> StrChar {cons("decimal")}
    ~[\0-\31\n\t\"\\]          -> StrChar {cons("normal")}

    [\"] chars:StrChar* [\"]   -> StrCon  {cons("default")}

module languages/aterm/syntax/RealCon

imports languages/aterm/syntax/IntCon

hiddens
  context-free start-symbols 
    RealCon

exports
  sorts OptExp RealCon

  context-free syntax
    "e" IntCon -> OptExp {cons("present")}
               -> OptExp {cons("absent")}

    base:IntCon "." decimal:NatCon exp:OptExp  -> RealCon {cons("real-con")}

module basic/NatCon

exports

sorts NatCon

lexical syntax
  
 [0-9]+ -> NatCon {cons("digits")}

lexical restrictions

 NatCon -/- [0-9]

module basic/Whitespace

exports
  lexical syntax
    [\ \t\n\r]	-> LAYOUT {cons("whitespace")}

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

module languages/aterm/syntax/IntCon

imports basic/Whitespace
	basic/NatCon

hiddens
  context-free start-symbols 
    IntCon

exports
  sorts IntCon
  context-free syntax
    NatCon         -> IntCon {cons("natural")}
    pos:"+" NatCon -> IntCon {cons("positive")}
    neg:"-" NatCon -> IntCon {cons("negative")}

module languages/aterm/syntax/ATerms

imports languages/aterm/syntax/IntCon 
        languages/aterm/syntax/RealCon
	basic/StrCon
	basic/IdentifierCon

exports
  sorts AFun ATerm Annotation

  context-free syntax
	StrCon 	-> AFun {cons("quoted")}
	IdCon 	-> AFun {cons("unquoted")}

  context-free syntax
	IntCon 					-> ATerm {cons("int")}
	RealCon					-> ATerm {cons("real")}
	fun:AFun 				-> ATerm {cons("fun")}
	fun:AFun "(" args:{ATerm ","}+ ")" 	-> ATerm {cons("appl")}
	"<" type:ATerm ">" 			-> ATerm {cons("placeholder")}
	"[" elems:{ATerm ","}* "]" 		-> ATerm {cons("list")}
	trm:ATerm Annotation 			-> ATerm {cons("annotated")}

  context-free syntax
	"{" annos:{ ATerm ","}+ "}" 		-> Annotation {cons("default")}

hiddens
  context-free start-symbols 
    ATerm

module languages/sdf2/syntax/Kernel

imports languages/aterm/syntax/ATerms 
        languages/sdf2/syntax/Symbols 
        languages/sdf2/syntax/Grammars

exports
  sorts Attribute ATermAttribute Attributes Production Productions
  context-free syntax
    aterm:ATerm    -> ATermAttribute  {cons("default")}
    ATermAttribute -> Attribute       {cons("term")}

    "{" list:{Attribute ","}* "}" -> Attributes  {cons("attrs")}
                                  -> Attributes  {cons("no-attrs")}

    Symbols "->" result:Symbol Attributes -> Production  {cons("prod")}

    list:Production* -> Productions

    "syntax" Productions -> Grammar {cons("syntax")}

module languages/sdf2/syntax/Sdf2-Syntax

imports languages/sdf2/syntax/Kernel
        languages/sdf2/syntax/Basic
        languages/sdf2/syntax/Modules
        languages/sdf2/syntax/Regular
        languages/sdf2/syntax/Lifting
        languages/sdf2/syntax/Priority
        languages/sdf2/syntax/CC
        languages/sdf2/syntax/Sorts
        languages/sdf2/syntax/Start-Symbols
        languages/sdf2/syntax/Literals
        languages/sdf2/syntax/Labels
        languages/sdf2/syntax/Restrictions
        languages/sdf2/syntax/Aliases

exports
  sorts SDF

  context-free syntax
    "<START>" -> Symbol {cons("start")}
    "<Start>" -> Symbol {cons("file-start")}

    "lexical"      "priorities" Priorities      -> Grammar {cons("lexical-priorities")}
    "context-free" "priorities" Priorities      -> Grammar {cons("context-free-priorities")}
    "lexical"      "restrictions" Restrictions  -> Grammar {cons("lexical-restrictions")}
    "context-free" "restrictions" Restrictions  -> Grammar {cons("context-free-restrictions")}

    "definition" Definition -> SDF {cons("definition")}

  context-free priorities
    Symbol "|" Symbol  -> Symbol >
    Label ":" Symbol -> Symbol                  


module languages/sdf2/syntax/Sdf2

imports languages/sdf2/syntax/Sdf2-Syntax

exports
  lexical syntax
    "LAYOUT"            -> Sort    {reject}

  context-free syntax
    "aliases"           -> ModuleName {reject}
    "lexical"           -> ModuleName {reject}
    "priorities"        -> ModuleName {reject}
    "context-free"      -> ModuleName {reject}
    "definition"        -> ModuleName {reject}
    "syntax"            -> ModuleName {reject}
    "variables"         -> ModuleName {reject}
    "module"            -> ModuleName {reject}
    "imports"           -> ModuleName {reject}
    "exports"           -> ModuleName {reject}
    "hiddens"           -> ModuleName {reject}
    "left"              -> ModuleName {reject}
    "right"             -> ModuleName {reject}
    "assoc"             -> ModuleName {reject}
    "non-assoc"         -> ModuleName {reject}
    "bracket"           -> ModuleName {reject}
    "sorts"             -> ModuleName {reject}
    "bracket"           -> ModuleName {reject}
    "sorts"             -> ModuleName {reject}
    "restrictions"      -> ModuleName {reject}

     StrCon "(" arguments:{Symbol ","}* ")" -> Symbols {reject}

     Associativity -> Label   {reject}
     Associativity -> ATermAttribute {reject}
     "reject"      -> ATermAttribute {reject}
     "prefer"      -> ATermAttribute {reject}
     "avoid"       -> ATermAttribute {reject}
     "bracket"     -> ATermAttribute {reject}
     "id" "(" ModuleName ")" -> ATermAttribute {reject}

  restrictions
    <Sort -CF>
    "aliases"
    "lexical"
    "priorities"
    "context-free"
    "definition"
    "syntax"
    "variables"
    "module"
    "imports"
    "exports"
    "hiddens"
    "left"
    "right"
    "assoc"
    "non-assoc"
    "bracket"
    "sorts"             -/- [A-Za-z0-9]
    <NatCon -CF>        -/- [0-9]
    <LAYOUT?-CF>        -/- [\ \t\n\%]
    <NumChar -CF>       -/- [0-9]
    <ModuleName -CF>    -/- [A-Za-z0-9\_\-]

exports
  context-free start-symbols
    SDF Module

module Sdf2
imports languages/sdf2/syntax/Sdf2
hiddens
  context-free start-symbols
    SDF Module

  %% HACK
  context-free syntax
    Group "." -> Group {non-assoc,cons("non-transitive")}