cminus1.atg 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. COMPILER CMinus
  2. (* Simple CMinus grammar. P.D. Terry, Rhodes University, 1995 *)
  3. CHARACTERS
  4. eol = CHR(13) .
  5. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  6. digit = "0123456789" .
  7. noquote1 = ANY - "'" - eol .
  8. noquote2 = ANY - '"' - eol .
  9. IGNORE CHR(9) .. CHR(13)
  10. COMMENTS FROM "//" TO eol
  11. TOKENS
  12. identifier = letter {letter | digit} .
  13. number = digit { digit} .
  14. string = '"' (noquote2 | '\"') { noquote2 | '\"' } '"' .
  15. char = "'" ["\"] noquote1 "'" | "'\''" .
  16. PRODUCTIONS
  17. (* The main program can nest procedures, but procedures cannot nest procedures
  18. themselves. This can be handled in a context free way by having a
  19. MainBlock and a ProcBlock non-terminal. *)
  20. CMinus = "void" identifier FormalParameters
  21. MainBlock .
  22. MainBlock = "{"
  23. { ConstDeclaration
  24. | ProcDeclaration
  25. | VarDeclarations }
  26. { Statement }
  27. "}" .
  28. ConstDeclaration = "const" identifier "=" number ";" .
  29. VarDeclarations = "int"
  30. identifier [ UpperBound ]
  31. { "," identifier [ UpperBound ] } ";" .
  32. UpperBound = "[" ( number | identifier ) "]" .
  33. ProcDeclaration = ( "function" | "void" ) identifier FormalParameters
  34. ( ProcBlock | ";" ) .
  35. FormalParameters = "(" [ FormalPar { "," FormalPar } ] ")" .
  36. FormalPar = identifier [ "[" "]" ] .
  37. ProcBlock = "{"
  38. { ConstDeclaration | VarDeclarations }
  39. { Statement }
  40. "}" .
  41. (* The rule for semicolons is very different from Pascal/Modula, where they
  42. are separators. *)
  43. CompoundStatement = "{" { Statement } "}" .
  44. Statement = CompoundStatement | AssignmentOrCall
  45. | IfStatement | WhileStatement | ForStatement
  46. | LoopStatement | ExitStatement | WriteStatement
  47. | ReadStatement | ReturnStatement | ";" .
  48. AssignmentOrCall = Designator
  49. ( "=" Expression | ActualParameters | "++" | "--" ) ";" .
  50. Designator = identifier [ "[" Expression "]" ] .
  51. ActualParameters = "(" [ ActualPar { "," ActualPar } ] ")" .
  52. ActualPar = Expression .
  53. IfStatement = "if" "(" Expression ")" Statement
  54. [ "else" Statement ] .
  55. WhileStatement = "while" "(" Expression ")" Statement .
  56. ForStatement = "for" "(" [ Initial ] ";" Expression ";" [ Final ] ")"
  57. Statement .
  58. Initial = Designator "=" Expression .
  59. Final = Designator ( "++" | "--" ) .
  60. LoopStatement = "loop" Statement .
  61. ExitStatement = "exit" ";" .
  62. WriteStatement = "cout" "<<" Expression { "<<" Expression } ";" .
  63. ReadStatement = "cin" ">>" Designator { ">>" Designator } ";" .
  64. ReturnStatement = "return" [ Expression ] ";" .
  65. Expression = SimpleExpression [ RelOp SimpleExpression ] .
  66. SimpleExpression = [ Sign ] Term { AddOp Term } .
  67. Term = Factor { MulOp Factor } .
  68. Factor = Designator [ ActualParameters ]
  69. | number | char | string
  70. | "(" Expression ")"
  71. | "!" Factor .
  72. Sign = "+" | "-" .
  73. AddOp = "+" | "-" | "||" .
  74. MulOp = "*" | "/" | "%" | "&&" .
  75. RelOp = "==" | "!=" | "<" | "<=" | ">" | ">=" .
  76. END CMinus.