⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isomod2.atg

📁 一个Pascal语言分析器
💻 ATG
📖 第 1 页 / 共 2 页
字号:
  VarDecl      =  IdList ":"           (. Append(' : ') .)
                  Type .

  ProcDecl     =  ProcHead ";"         (. Append(';') .)
                  (                    (. IndentNextLine .)
                    ProcBlock Ident
                  | "FORWARD"          (. Append(' FORWARD'); Indent .)
                  ) .

  ProcHead     =  "PROCEDURE"          (. Append('PROCEDURE ') .)
                  Ident [ FormalParams ] .

  FormalParams =  "("                  (. Append(' (') .)
                  [ FormalGroup { ";"  (. Append('; ') .)
                  FormalGroup } ] ")"  (. Append(')') .)
                  [ ":"                (. Append(' : ') .)
                  QualId ] .

  FormalGroup =  [ "VAR"               (. Append('VAR ') .)
                  ] IdList ":"         (. Append(' : ') .)
                  FormalType .

  FormalType  =  [ "ARRAY" "OF"        (. Append('ARRAY OF ') .)
                  ] QualId .

  ProcBlock    =  { Declaration }
                  [ InitialPart ]
                  "END"                (. Append('END ') .) .

  ModDecl      =  "MODULE"             (. Append('MODULE ') .)
                  Ident
                  [ Priority ] ";"     (. Append(';'); IndentNextLine .)
                  { Import }
                  [ Export ]
                  ModBlock Ident .

  StatSeq      =  Statement { WEAK ";" (. Append(';'); NewLine .)
                  Statement } .

  Statement    =  SYNC [
                    Designator
                    (   ":="           (. Append(' := ') .)
                        Expression
                      | [ ActualParams ]
                    )
                  | IfStat | CaseStat
                  | LoopStat | ForStat
                  | RepeatStat | WhileStat
                  | WithStat
                  | "EXIT"             (. Append('EXIT') .)
                  | "RETRY"            (. Append('RETRY') .)
                  | "RETURN"           (. Append('RETURN') .)
                    [                  (. Append(' ') .)
                    Expression ] ] .

  WithStat     =  "WITH"               (. Append('WITH ') .)
                  Designator "DO"      (. Append(' DO'); IndentNextLine .)
                  StatSeq "END"        (. ExdentNextLine; Append('END') .) .

  LoopStat     =  "LOOP"               (. Append('LOOP'); IndentNextLine .)
                  StatSeq
                  "END"                (. ExdentNextLine; Append('END') .) .

  ForStat      =  "FOR"                (. Append('FOR ') .)
                  Ident ":="           (. Append(' := ') .)
                  Expression "TO"      (. Append(' TO ') .)
                  Expression [ "BY"    (. Append(' BY ') .)
                  ConstExpr ] "DO"     (. Append(' DO'); IndentNextLine .)
                  StatSeq "END"        (. ExdentNextLine; Append('END') .) .

  RepeatStat   =  "REPEAT"             (. Append('REPEAT'); IndentNextLine .)
                  StatSeq
                  "UNTIL"              (. ExdentNextLine; Append('UNTIL ') .)
                  Expression .

  WhileStat    =  "WHILE"              (. Append('WHILE ') .)
                  Expression "DO"      (. Append(' DO'); IndentNextLine .)
                  StatSeq "END"        (. ExdentNextLine; Append('END') .) .

  CaseStat     =  "CASE"               (. Append('CASE ') .)
                  Expression "OF"      (. Append(' OF'); IndentNextLine .)
                  OneCase { "|"        (. Append('| ') .)
                  OneCase }
                  [ "ELSE"             (. Append('ELSE '); Indent (* NewLine *) .)
                  StatSeq              (. Exdent .)
                  ] "END"              (. ExdentNextLine; Append('END') .) .

  OneCase      =  [ CaseLabList
                  ":"                  (. Append(' : '); Indent (* NewLine *) .)
                  StatSeq              (. ExdentNextLine .)
                  ] .

(* Alternative
  IfStat       =  "IF"                 (. Append('IF ') .)
                  Expression "THEN"    (. IndentNextLine; Append('THEN '); Indent .)
                  StatSeq              (. Exdent .)
                  { "ELSIF"            (. NewLine; Append('ELSIF ') .)
                  Expression "THEN"    (. Append(' THEN '); Indent .)
                  StatSeq              (. Exdent .)
                  }
                  [ "ELSE"             (. NewLine; Append('ELSE '); Indent .)
                     StatSeq              (. Exdent .)
                  ] "END"              (. ExdentNextLine; Append('END') .) .
*)

  IfStat       =  "IF"                 (. Append('IF ') .)
                  Expression "THEN"    (. IndentNextLine; Append('THEN'); IndentNextLine .)
                  StatSeq              (. Exdent .)
                  { "ELSIF"            (. NewLine; Append('ELSIF ') .)
                  Expression "THEN"    (. Append(' THEN'); IndentNextLine .)
                  StatSeq              (. Exdent .)
                  }
                  [ "ELSE"             (. NewLine; Append('ELSE'); IndentNextLine .)
                    StatSeq            (. Exdent .)
                  ] "END"              (. ExdentNextLine; Append('END') .) .

  Expression   =  SimExpr [ RelOp SimExpr SYNC ] .

  SimExpr      =  SYNC [ "+"           (. Append(' + ') .)
                        | "-"          (. Append(' - ') .)
                  ] Term { AddOp Term } .

  Term         =  Factor { MulOp Factor } .

  Factor       =  SYNC (
                    integer            (. WriteToken .)
                  | real               (. WriteToken .)
                  | string             (. WriteToken .)
                  | SetRest
                  | Ident
                    { SetRest
                      | "["            (. Append('[') .)
                        ExpList "]"    (. Append(']') .)
                      | "^"            (. Append('^') .)
                      | "."            (. Append('.') .)
                        Ident
                    } [ ActualParams ]
                  | "("                (. Append('(') .)
                    Expression ")"     (. Append(')') .)
                  | ( "NOT"            (. Append('NOT ') .)
                      | "~"            (. Append('~ ') .)
                    ) Factor
                  ) .

  ActualParams =  "("                  (. Append('(') .)
                  [ ExpList ] ")"      (. Append(')') .) .

  SetRest      =  "{"                  (. Append('{') .)
                  [ Element { ","      (. Append(', ') .)
                  Element } ] "}"      (. Append('}') .) .

  Element      =  ConstExpr
                  [ ".."               (. Append(' .. ') .)
                  ConstExpr ] .

  MulOp        =    "*"                (. Append(' * ') .)
                  | "/"                (. Append(' * ') .)
                  | "DIV"              (. Append(' DIV ') .)
                  | "REM"              (. Append(' REM ') .)
                  | "MOD"              (. Append(' MOD ') .)
                  | "AND"              (. Append(' AND ') .)
                  | "&"                (. Append(' & ') .) .

  AddOp        =    "+"                (. Append(' + ') .)
                  | "-"                (. Append(' - ') .)
                  | "OR"               (. Append(' OR ') .) .

  RelOp        =    "="                (. Append(' = ') .)
                  | "#"                (. Append(' # ') .)
                  | "<>"               (. Append(' <> ') .)
                  | "<"                (. Append(' < ') .)
                  | "<="               (. Append(' <= ') .)
                  | ">"                (. Append(' > ') .)
                  | ">="               (. Append(' >= ') .)
                  | "IN"               (. Append(' IN ') .) .

  Designator   =  Ident { "["          (. Append('[') .)
                      ExpList "]"      (. Append(']') .)
                    | "^"              (. Append('^') .)
                    | "."              (. Append('.') .)
                      Ident } .

  IdList       =  Ident { ","          (. Append(', ') .)
                  Ident } .

  ExpList      =  Expression { ","     (. Append(', ') .)
                  Expression } .

  QualId       =  Ident { "."          (. Append('.') .)
                  Ident } .

  Ident        = identifier            (. WriteToken .) .

END Mod2.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -