topsy0.atg 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. COMPILER Topsy
  2. (* Topsy++ level 1 grammar - no procedures, functions, parameters
  3. Grammar only - no code generation or constraint analysis
  4. See "Compilers and Compiler generators - an introduction with C++"
  5. ISBN 1-85032-298-8, International Thomson Computer Press
  6. P.D. Terry, Rhodes University, 1996 *)
  7. PRAGMAS
  8. DebugOn = "$D+" .
  9. CHARACTERS
  10. cr = CHR(13) .
  11. lf = CHR(10) .
  12. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  13. digit = "0123456789" .
  14. graphic = ANY - '"' - "'" - "\" - cr - lf - CHR(0) .
  15. IGNORE CHR(9) .. CHR(13)
  16. COMMENTS FROM "//" TO lf
  17. COMMENTS FROM "/*" TO "*/"
  18. TOKENS
  19. identifier = letter { letter | digit | "_" } .
  20. number = digit { digit } .
  21. string = '"' { graphic | "\" graphic | "\'" | "\\" | '\"' } '"' .
  22. character = "'" ( graphic | "\" graphic | "\'" | "\\" | '\"' ) "'" .
  23. PRODUCTIONS
  24. Topsy = "void" "main" "(" "void" ")" Block .
  25. Block = "{" { Declaration | Statement } "}" .
  26. Declaration = ConstDeclaration | VarDeclarations .
  27. Statement
  28. = Block | Assignment
  29. | IfStatement | WhileStatement
  30. | ForStatement | RepeatStatement
  31. | ReadStatement | WriteStatement
  32. | ReturnStatement | EmptyStatement .
  33. ConstDeclaration
  34. = "const" identifier "=" ( number | character | "true" | "false" ) ";" .
  35. VarDeclarations = ( "int" | "bool" | "char" ) OneVar { "," OneVar } ";" .
  36. OneVar = identifier [ ArraySize | "=" Expression ] .
  37. ArraySize = "[" number "]" .
  38. Assignment = Variable ( "=" Expression | "++" | "--" ) ";" .
  39. Variable = Designator .
  40. Designator = identifier [ "[" Expression "]" ] .
  41. IfStatement = "if" "(" Condition ")" Statement [ "else" Statement ] .
  42. WhileStatement = "while" "(" Condition ")" Statement .
  43. RepeatStatement = "do" { Statement } "until" "(" Condition ")" ";" .
  44. Condition = Expression .
  45. ForStatement
  46. = "for" "(" identifier "=" Expression ";" Condition [ ";" Epilogue ] ")"
  47. Statement .
  48. Epilogue = identifier ( "=" Expression | "++" | "--" ) .
  49. ReadStatement = "cin" ">>" Variable { ">>" Variable } ";" .
  50. WriteStatement = "cout" "<<" WriteElement { "<<" WriteElement } ";" .
  51. WriteElement = string | Expression .
  52. ReturnStatement = "return" ";" .
  53. EmptyStatement = ";" .
  54. Expression = SimpleExpr [ RelOp SimpleExpr ] .
  55. SimpleExpr = ( "+" Term | "-" Term | Term ) { AddOp Term } .
  56. Term = Factor { MulOp Factor } .
  57. Factor = Designator | number | character | "true" | "false"
  58. | "!" Factor | "(" Expression ")" .
  59. AddOp = "+" | "-" | "||" .
  60. MulOp = "*" | "/" | "%" | "&&" .
  61. RelOp = "==" | "!=" | "<" | "<=" | ">" | ">=" .
  62. END Topsy.