c.atg 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. COMPILER C
  2. (* An attempt to describe a small subset of C *)
  3. CHARACTERS
  4. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  5. digit = "0123456789" .
  6. hexdigit = digit + "ABCDEFabcdef" .
  7. tab = CHR(9) .
  8. lf = CHR(10) .
  9. eol = CHR(13) .
  10. files = letter + digit + ":\." .
  11. chars = CHR(32) .. CHR(255) - "'" .
  12. strings = CHR(32) .. CHR(255) - '"'.
  13. macros = ANY - eol .
  14. COMMENTS FROM "/*" TO "*/"
  15. IGNORE
  16. tab + eol + lf
  17. TOKENS
  18. identifier = ( "_" | letter) { "_" | letter | digit } .
  19. number = digit { digit } [ "." { digit }] [ "U" | "u" | "L" | "l" ] .
  20. hexnumber = "0" ( "x" | "X" ) hexdigit { hexdigit } [ "U" | "u" | "L" | "l" ] .
  21. string = '"' { strings } '"' .
  22. char = "'" [ "\" ] chars "'" .
  23. library = "<" files { files } ">" .
  24. PRAGMAS
  25. PreProcessor = "#" {macros} .
  26. PRODUCTIONS
  27. C = { Definition } EOF .
  28. (* Variable and Type Definitions *)
  29. Definition = [ StorageClass ] Type { "*" } identifier
  30. ( FunctionDefinition | VarList ";" ) .
  31. StorageClass = "auto" | "extern" | "register" | "static" .
  32. Type = "short" [ "int" ]
  33. | "long" [ "int" | "float" ]
  34. | "unsigned" [ "char" | "int" | "long" ]
  35. | "char" | "int" | "float" | "double" | "void" .
  36. VarList = ArraySize { "," { "*" } identifier ArraySize } .
  37. ArraySize = { "[" [ ConstExpression ] "]" } .
  38. (* Function Definitions *)
  39. FunctionDefinition = FunctionHeader ( ";" | FunctionBody ) .
  40. FunctionHeader = "(" [ FormalParamList ] ")" .
  41. FunctionBody = CompoundStatement .
  42. FormalParamList = FormalParameter { "," FormalParameter } .
  43. FormalParameter = Type { "*" } [ identifier ] ArraySize .
  44. (* Statements *)
  45. Statement = { Label }
  46. ( AssignmentExpression | BreakStatement
  47. | CompoundStatement | ContinueStatement
  48. | DoStatement | ForStatement
  49. | IfStatement | NullStatement
  50. | ReturnStatement | SwitchStatement
  51. | WhileStatement ) .
  52. Label = "case" ConstExpression ":" | "default" ":" .
  53. (* There is no requirement that a switch statement be followed by a compound
  54. statement. Actually labels may be even more general *)
  55. AssignmentExpression = Expression ";" .
  56. BreakStatement = "break" ";" .
  57. CompoundStatement = "{" { LocalDeclaration } { Statement } "}" .
  58. ContinueStatement = "continue" ";" .
  59. DoStatement = "do" Statement "while" "(" Expression ")" ";" .
  60. ForStatement = "for" "(" [ Expression ] ";" [ Expression ] ";" [ Expression ] ")" Statement .
  61. IfStatement = "if" "(" Expression ")" Statement [ "else" Statement ] .
  62. NullStatement = ";" .
  63. ReturnStatement = "return" [ Expression ] ";" .
  64. (* Expression usually in parentheses *)
  65. SwitchStatement = "switch" "(" Expression ")" Statement .
  66. WhileStatement = "while" "(" Expression ")" Statement .
  67. (* LocalDeclarations *)
  68. LocalDeclaration = [ StorageClass ] Type { "*" } identifier
  69. ( FunctionHeader | VarList ) ";" .
  70. (* Expressions, based on Kernighan and Ritchie: "The C Programming Language".
  71. There does not seem to be a way to make this work in an LL(1) fashion,
  72. but this generates a "working" parser *)
  73. ConstExpression = Expression .
  74. Expression = Conditional { AssignmentOperator Expression }.
  75. Conditional = LogORExp .
  76. LogORExp = LogANDExp { "||" LogANDExp } .
  77. LogANDExp = InclORExp { "&&" InclORExp }.
  78. InclORExp = ExclORExp { "|" ExclORExp } .
  79. ExclORExp = ANDExp { "^" ANDExp } .
  80. ANDExp = EqualExp { "&" EqualExp } .
  81. EqualExp = RelationExp { ( "==" | "!=" ) RelationExp } .
  82. RelationExp = ShiftExp { ( "<" | ">" | "<=" | ">=" ) ShiftExp }.
  83. ShiftExp = AddExp { ( "<<" | ">>" ) AddExp } .
  84. AddExp = MultExp { ( "+" | "-" ) MultExp } .
  85. MultExp = CastExp { ( "*" | "/" | "%" ) CastExp } .
  86. CastExp = UnaryExp .
  87. (* we should really add
  88. | "(" identifier ")" CastExp .
  89. but this breaks it badly *)
  90. UnaryExp = PostFixExp
  91. | ( "++" | "--" ) UnaryExp
  92. | UnaryOperator CastExp .
  93. (* we should really add
  94. | "sizeof" ( UnaryExp | "(" Type ")" ) .
  95. but this breaks it badly *)
  96. PostFixExp = Primary
  97. { "[" Expression "]"
  98. | FunctionCall
  99. | "." identifier
  100. | "->" identifier
  101. | "++"
  102. | "--"
  103. } .
  104. Primary = identifier | string | char | number | "(" Expression ")" .
  105. FunctionCall = "(" [ ActualParameters ] ")" .
  106. ActualParameters = Expression { "," Expression } .
  107. AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "&="
  108. | "^=" | "|=" | "<<=" | ">>=" .
  109. UnaryOperator = "+" | "-" | "*" | "!" | "&" | "~" .
  110. END C.