Guest User

Untitled

a guest
Jun 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. # JSON-AST Format
  2.  
  3. ## Purpose
  4. The purpose of this format is to provide a language-agnostic way of representing source code that can be compiled to an Abstract Syntax Tree (AST). The format is represented using JSON and should be able to capture the important properties of most code.
  5.  
  6. ## Format
  7. All code in these datasets are represented as abstract syntax trees (ASTs), stored in a JSON format. Each JSON object represents a node in the AST, and has the following properties:
  8. * `type` [required]: The type of the node (e.g. "if-statement", "expression", "variable-declaration", etc.). In Snap, this could be the name of a built-in block (e.g. "forward", "turn"). The set of possible types is pre-defined by a given programming language, as they generally correspond to keywords. The possible types for a given language are defined in the grammar file for the dataset, discussed later.
  9. * `value` [optional]: This contains any user-defined value for the node, such as the identifier for a variable or function, the value of a literal, or the name of an imported module These are things the student names, and they could take any value. **Note**: In the Snap datasets, string literal values have been removed to anonymize the dataset; however, these values are generally not relevant for hint generation.
  10. * `children` [optional]: A map of this node's children, if any. In Python, the keys of the map indicate the relationship of the parent/child (e.g. a while loop might have a "condition" child). In the Snap dataset, they are simply numbers indicating the ordering of the children (e.g. arguments "0", "1" and "2"). The values are objects representing the children.
  11. * `children-order` [optional]: The order of this node's children, represented as an array of keys from the `children` map. This is necessary because JSON maps have no ordering, though the order of the children in the map should correspond to the correct order.
  12. * `id` [optional]: A trace-unqiue ID for the node that will be kept constant across ASTs in this trace. This is useful in block-based languages, for example, to identify a given block, even if it moves within the AST.
  13.  
  14. ## Example
  15.  
  16. Python Code:
  17. ```python
  18. def helloWorld():
  19. return 'Hello World!'
  20. ```
  21.  
  22. JSON-AST Output:
  23. ```json
  24. {
  25. "children": {
  26. "body": {
  27. "children": {
  28. "0": {
  29. "children": {
  30. "decorator_list": {
  31. "type": "list"
  32. },
  33. "args": {
  34. "children": {
  35. "defaults": {
  36. "type": "list"
  37. },
  38. "kwarg": null,
  39. "vararg": null,
  40. "kwonlyargs": {
  41. "type": "list"
  42. },
  43. "kw_defaults": {
  44. "type": "list"
  45. },
  46. "args": {
  47. "type": "list"
  48. }
  49. },
  50. "type": "arguments",
  51. "childrenOrder": [
  52. "args",
  53. "vararg",
  54. "kwonlyargs",
  55. "kw_defaults",
  56. "kwarg",
  57. "defaults"
  58. ]
  59. },
  60. "body": {
  61. "children": {
  62. "0": {
  63. "children": {
  64. "value": {
  65. "children": {},
  66. "value": "Hello World!",
  67. "type": "Str",
  68. "childrenOrder": []
  69. }
  70. },
  71. "type": "Return",
  72. "childrenOrder": [
  73. "value"
  74. ]
  75. }
  76. },
  77. "type": "list",
  78. "childrenOrder": [
  79. "0"
  80. ]
  81. },
  82. "returns": null
  83. },
  84. "value": "helloWorld",
  85. "type": "FunctionDef",
  86. "childrenOrder": [
  87. "args",
  88. "body",
  89. "decorator_list",
  90. "returns"
  91. ]
  92. }
  93. },
  94. "type": "list",
  95. "childrenOrder": [
  96. "0"
  97. ]
  98. }
  99. },
  100. "id": "97ea8451df0420e0ec5e262a30365e3b",
  101. "correct": true,
  102. "type": "Module",
  103. "childrenOrder": [
  104. "body"
  105. ]
  106. }
  107. ```
Add Comment
Please, Sign In to add comment