cminus.atg 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 | Assignment
  45. | ProcedureCall | IncOrDecStatement
  46. | IfStatement | WhileStatement | ForStatement
  47. | LoopStatement | ExitStatement | WriteStatement
  48. | ReadStatement | ReturnStatement | ";" .
  49. Assignment = Designator "=" Expression ";" .
  50. ProcedureCall = Designator ActualParameters ";" .
  51. IncOrDecStatement = Designator ( "++" | "--" ) ";" .
  52. Designator = identifier [ "[" Expression "]" ] .
  53. ActualParameters = "(" [ ActualPar { "," ActualPar } ] ")" .
  54. ActualPar = Expression .
  55. IfStatement = "if" "(" Expression ")" Statement
  56. [ "else" Statement ] .
  57. WhileStatement = "while" "(" Expression ")" Statement .
  58. ForStatement = "for" "(" [ Initial ] ";" Expression ";" [ Final ] ")"
  59. Statement .
  60. Initial = Designator "=" Expression .
  61. Final = Designator ( "++" | "--" ) .
  62. LoopStatement = "loop" Statement .
  63. ExitStatement = "exit" ";" .
  64. WriteStatement = "cout" "<<" Expression { "<<" Expression } ";" .
  65. ReadStatement = "cin" ">>" Designator { ">>" Designator } ";" .
  66. ReturnStatement = "return" [ Expression ] ";" .
  67. Expression = SimpleExpression [ RelOp SimpleExpression ] .
  68. SimpleExpression = [ Sign ] Term { AddOp Term } .
  69. Term = Factor { MulOp Factor } .
  70. Factor = Designator [ ActualParameters ]
  71. | number | char | string
  72. | "(" Expression ")"
  73. | "!" Factor .
  74. Sign = "+" | "-" .
  75. AddOp = "+" | "-" | "||" .
  76. MulOp = "*" | "/" | "%" | "&&" .
  77. RelOp = "==" | "!=" | "<" | "<=" | ">" | ">=" .
  78. END CMinus.