瀏覽代碼

second commit

Eric Streit 4 周之前
當前提交
b803698de4
共有 2 個文件被更改,包括 229 次插入 和 0 次删除
  1. 112 0
      coco/mod2.atg
  2. 117 0
      coco/pimmod2.atg

+ 112 - 0
coco/mod2.atg

@@ -0,0 +1,112 @@
+COMPILER Mod2
+(* Alternative grammar for PIM style Modula-2 *)
+
+CHARACTERS
+  eol      = CHR(13) .
+  letter   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
+  octDigit = "01234567" .
+  digit    = octDigit + '89'.
+  hexDigit = digit + 'ABCDEF'.
+  noQuote1 = ANY - "'" - eol .
+  noQuote2 = ANY - '"' - eol .
+
+IGNORE  CHR(9) .. CHR(13)
+
+COMMENTS
+  FROM '(*' TO '*)' NESTED
+
+TOKENS
+  id      =  letter { letter | digit }.
+  integer =    digit { digit }
+             | digit { digit }  CONTEXT ("..")
+             | octDigit { octDigit } ("B" | "C")
+             | digit { hexDigit } "H".
+  real    =  digit { digit } "." { digit }
+             [ "E" [ "+" | "-" ] digit { digit } ].
+  string  =    "'" { noQuote1 } "'"
+             | '"' { noQuote2 } '"'.
+
+PRODUCTIONS
+  Mod2         =  DefMod | [ "IMPLEMENTATION" ] ProgMod .
+  ProgMod      =  "MODULE" id [ Priority ] ";" { Import } Block id "." .
+  Def          =    "CONST" { ConstDecl ";" }
+                  | "TYPE"  { id [ "=" Type ] ";" }
+                  | "VAR"   { VarDecl ";" }
+                  | ProcHead ";" .
+  DefMod       =  "DEFINITION" "MODULE" id ";" { Import } { Def } "END" id "." .
+  Import       =  "FROM" id "IMPORT" IdList ";" | "IMPORT" id { "," id } ";" .
+  Export       =  "EXPORT" [ "QUALIFIED" ] IdList ";" .
+  Priority     =  "[" ConstExpr "]" .
+  ModDecl      =  "MODULE" id [ Priority ] ";" { Import } [ Export ] Block id .
+  FormType     =  [ "ARRAY" "OF" ] QualId .
+  QualId       =  id { "." id } .
+  FPSect       =  [ "VAR" ] IdList ":" FormType .
+  FormPar      =  "(" [ FPSect { ";" FPSect } ] ")" [ ":" QualId ] .
+  Decl         =    "CONST" { ConstDecl ";" }
+                  | "TYPE"  { TypeDecl  ";" }
+                  | "VAR"   { VarDecl   ";" }
+                  | ProcDecl ";"
+                  | ModDecl  ";" .
+  Block        =  { Decl } [ "BEGIN"  StatSeq ] "END" .
+  StatSeq      =  Stat { ";" Stat } .
+  Stat         =  [ Design ( ":=" Expr | [ ActPar ] )
+                  | IfStat  | CaseStat | LoopStat | ForStat
+                  | RepeatStat | WhileStat | WithStat
+                  | "EXIT" | "RETURN" [ Expr ]
+                  ] .
+  ProcHead     =  "PROCEDURE" id [ FormPar ] .
+  ProcDecl     =  ProcHead ";" ( Block id | "FORWARD" ) .
+  WithStat     =  "WITH" Design "DO" StatSeq "END" .
+  LoopStat     =  "LOOP" StatSeq "END" .
+  ForStat      =  "FOR" id ":=" Expr "TO" Expr [ "BY" ConstExpr ]
+                  "DO" StatSeq "END".
+  RepeatStat   =  "REPEAT" StatSeq "UNTIL" Expr  .
+  WhileStat    =  "WHILE" Expr "DO" StatSeq "END" .
+  Case         =  [ CaseLabList ":" StatSeq ] .
+  CaseStat     =  "CASE" Expr "OF" Case { "|" Case } [ "ELSE" StatSeq ] "END" .
+  IfStat       =  "IF" Expr "THEN" StatSeq
+                  { "ELSIF" Expr "THEN" StatSeq }
+                  [ "ELSE" StatSeq ]
+                  "END" .
+  ActPar       =  "(" [ ExpList ] ")" .
+  Elem         =  ConstExpr [ ".." ConstExpr ] .
+  Fact         =    integer | real | string | SetRest
+                  | id { SetRest | "." id | "[" ExpList "]" | "^" } [ ActPar ]
+                  | "(" Expr ")"
+                  | ( "NOT" | "~" ) Fact.
+  SetRest      =  "{" [ Elem { "," Elem } ] "}" .
+  MulOp        =  "*" | "/" | "DIV" | "REM" | "MOD" | "AND" | "&" .
+  Term         =  Fact { MulOp Fact }.
+  AddOp        =  "+" | "-" | "OR" .
+  SimExpr      =  [ "+" | "-" ] Term { AddOp Term }.
+  Rel          =  "=" | "#" | "<>" | "<" | "<=" | ">" | ">=" | "IN" .
+  Expr         =  SimExpr [ Rel SimExpr ] .
+  ConstDecl    =  id "=" ConstExpr .
+  ConstExpr    =  Expr.
+  TypeDecl     =  id "=" Type .
+  Type         =    SimType | ArrayType | RecType
+                  | SetType | PointerType | ProcType .
+  SimType      =    QualId [ "[" ConstExpr ".." ConstExpr "]" ]
+                  | Enum | "[" ConstExpr ".." ConstExpr "]" .
+  Enum         =  "(" IdList ")" .
+  IdList       =  id { "," id } .
+  ArrayType    =  "ARRAY" SimType { "," SimType } "OF" Type.
+  RecType      =  "RECORD" FieldListSeq "END" .
+  FieldListSeq =  FieldList { ";" FieldList } .
+  FieldList    =  [ IdList ":" Type
+                   | "CASE" [ id ] ":" QualId "OF" Variant { "|" Variant }
+                     [ "ELSE" FieldListSeq ] "END"
+                  ].
+  Variant      =  [ CaseLabList ":" FieldListSeq ] .
+  CaseLabList  =  CaseLabs { "," CaseLabs } .
+  CaseLabs     =  ConstExpr [ ".." ConstExpr ] .
+  SetType      =  "SET" "OF" SimType .
+  PointerType  =  "POINTER" "TO" Type .
+  ProcType     =  "PROCEDURE" [ FormTypeList ] .
+  FormTypeList =  "(" [ [ "VAR" ] FormType { "," [ "VAR" ] FormType } ] ")"
+                  [ ":" QualId ] .
+  VarDecl      =  IdList ":" Type .
+  Design       =  id { "[" ExpList "]" | "^" | "." id }.
+  ExpList      =  Expr { "," Expr }.
+
+END Mod2.

+ 117 - 0
coco/pimmod2.atg

@@ -0,0 +1,117 @@
+COMPILER Mod2
+(* COCO/R Grammar for Modula-2 based on "Programming in Modula-2 *)
+
+CHARACTERS
+  eol      = CHR(13) .
+  letter   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
+  octDigit = "01234567" .
+  digit    = octDigit + '89' .
+  hexDigit = digit + 'ABCDEF' .
+  noQuote1 = ANY - "'" - eol .
+  noQuote2 = ANY - '"' - eol .
+
+IGNORE  CHR(9).. CHR(13)
+
+COMMENTS
+  FROM '(*' TO '*)' NESTED
+
+TOKENS
+  ident   =  letter { letter | digit } .
+  integer =    digit { digit }
+             | digit { digit }  CONTEXT ("..")
+             | octDigit { octDigit } ("B" | "C")
+             | digit { hexDigit } "H" .
+  real    =  digit { digit } "." { digit }
+             [ "E" [ "+" | "-" ] digit { digit } ] .
+  string  =    "'" { noQuote1 } "'"
+             | '"' { noQuote2 } '"' .
+
+PRODUCTIONS
+  Mod2                 = DefinitionModule | [ "IMPLEMENTATION" ] ProgramModule .
+  Number               = integer | real .
+  QualIdent            = ident { "." ident } .
+  ConstantDeclaration  = ident "=" ConstExpression .
+  ConstExpression      = Expression .
+  TypeDeclaration      = ident "=" Type .
+  Type                 =   SimpleType | ArrayType | RecordType | SetType
+                         | PointerType | ProcedureType .
+  SimpleType           = QualIdent | Enumeration | SubrangeType .
+  Enumeration          = "(" IdentList ")" .
+  IdentList            = ident { "," ident } .
+  SubrangeType         = [ ident ] "[" ConstExpression ".." ConstExpression "]" .
+  ArrayType            = "ARRAY" SimpleType { "," SimpleType } "OF" Type .
+  RecordType           = "RECORD" FieldListSequence "END" .
+  FieldListSequence    = FieldList { ";" FieldList } .
+  FieldList            =   [ IdentList ":" Type
+                         | "CASE" [ ident ] ":" QualIdent "OF"
+                           Variant { "|" Variant }
+                           [ "ELSE" FieldListSequence ] "END" ] .
+  Variant              = [ CaseLabelList ":" FieldListSequence ] .
+  CaseLabelList        = CaseLabels { "," CaseLabels } .
+  CaseLabels           = ConstExpression [ ".." ConstExpression ] .
+  SetType              = "SET" "OF" SimpleType .
+  PointerType          = "POINTER" "TO" Type .
+  ProcedureType        = "PROCEDURE" [ FormalTypeList ] .
+  FormalTypeList       = "(" [ [ "VAR" ] FormalType
+                         { "," [ "VAR" ] FormalType } ] ")" [ ":" QualIdent ] .
+  VariableDeclaration  = IdentList ":" Type .
+  Designator           = QualIdent { "." ident | "[" ExpList "]" | "^" } .
+  ExpList              = Expression { "," Expression } .
+  Expression           = SimpleExpression [ Relation SimpleExpression ] .
+  Relation             = "=" | "#" | "<" |"<=" | ">" | ">=" | "IN" .
+  SimpleExpression     = [ "+" | "-" ] Term { AddOperator Term } .
+  AddOperator          = "+" | "-" | "OR" .
+  Term                 = Factor { MulOperator Factor } .
+  MulOperator          = "*" |"/" | "DIV" | "MOD" | "AND" .
+  Factor               =   Number | string | Set | "NOT" Factor
+                         | Designator [ ActualParameters ]
+                         | "(" Expression ")" .
+  Set                  = [ QualIdent ] "{" [ Element { "," Element } ] "}" .
+  Element              = Expression [ ".." Expression ] .
+  ActualParameters     = "(" [ ExpList ] ")" .
+  Statement            = [  Assignment | ProcedureCall | IfStatement
+                          | CaseStatement | WhileStatement | RepeatStatement
+                          | LoopStatement | ForStatement | WithStatement
+                          | "EXIT" | "RETURN" [ Expression ] ] .
+  Assignment           = Designator ":=" Expression .
+  ProcedureCall        = Designator [ ActualParameters ] .
+  StatementSequence    = Statement { ";" Statement } .
+  IfStatement          = "IF" Expression "THEN" StatementSequence
+                         { "ELSIF" Expression "THEN" StatementSequence }
+                         [ "ELSE" StatementSequence ] "END" .
+  CaseStatement        = "CASE" Expression "OF" Case { "|" Case }
+                         [ "ELSE" StatementSequence ] "END" .
+  Case                 = [ CaseLabelList ":" StatementSequence ] .
+  WhileStatement       = "WHILE" Expression "DO" StatementSequence "END" .
+  RepeatStatement      = "REPEAT" StatementSequence "UNTIL" Expression .
+  ForStatement         = "FOR" ident ":=" Expression "TO" Expression
+                         [ "BY" ConstExpression ] "DO" StatementSequence "END" .
+  LoopStatement        = "LOOP" StatementSequence "END" .
+  WithStatement        = "WITH" Designator "DO" StatementSequence "END" .
+  ProcedureDeclaration = ProcedureHeading ";" Block ident .
+  ProcedureHeading     = "PROCEDURE" ident [ FormalParameters ] .
+  Block                = { Declaration } [ "BEGIN" StatementSequence ] "END" .
+  Declaration          =   "CONST" { ConstantDeclaration ";" }
+                         | "TYPE" { TypeDeclaration ";" }
+                         | "VAR" { VariableDeclaration ";" }
+                         | ProcedureDeclaration ";"
+                         | ModuleDeclaration ";" .
+  FormalParameters     = "(" [ FPSection { ";" FPSection } ] ")"
+                         [ ":" QualIdent ] .
+  FPSection            = [ "VAR" ] IdentList ":" FormalType .
+  FormalType           = [ "ARRAY" "OF" ] QualIdent .
+  ModuleDeclaration    = "MODULE" ident [ Priority ] ";"
+                         [ Import ] [ Export ] Block ident .
+  Priority             = "[" ConstExpression "]" .
+  Export               = "EXPORT" [ "QUALIFIED" ] IdentList ";" .
+  Import               = [ "FROM" ident ] "IMPORT" IdentList ";" .
+  DefinitionModule     = "DEFINITION" "MODULE" ident ";"
+                         { Import } { Definition } "END" ident "." .
+  Definition           =   "CONST" { ConstantDeclaration ";" } |
+                         | "TYPE" { ident [ "=" Type ] ";" }
+                         | "VAR" { VariableDeclaration ";" }
+                         | ProcedureHeading ";" .
+  ProgramModule        = "MODULE" ident [ Priority ] ";"
+                         { Import } Block ident "." .
+
+END Mod2.