Reputation: 25950
I have to parse by hand a Clang AST, however I have some trouble understanding the indentation rules supposed to describe the links between the nodes. Let's take a simple example. For the following code,
int f(int x) {
int result = (x / 42);
return result;
}
the generated AST is
... cutting out internal declarations of clang ...
`-FunctionDecl 0x5aeab50 <test.cc:1:1, line:4:1> f 'int (int)'
|-ParmVarDecl 0x5aeaa90 <line:1:7, col:11> x 'int'
`-CompoundStmt 0x5aead88 <col:14, line:4:1>
|-DeclStmt 0x5aead10 <line:2:3, col:24>
| `-VarDecl 0x5aeac10 <col:3, col:23> result 'int'
| `-ParenExpr 0x5aeacf0 <col:16, col:23> 'int'
| `-BinaryOperator 0x5aeacc8 <col:17, col:21> 'int' '/'
| |-ImplicitCastExpr 0x5aeacb0 <col:17> 'int' <LValueToRValue>
| | `-DeclRefExpr 0x5aeac68 <col:17> 'int' lvalue ParmVar 0x5aeaa90 'x' 'int'
| `-IntegerLiteral 0x5aeac90 <col:21> 'int' 42
`-ReturnStmt 0x5aead68 <line:3:3, col:10>
`-ImplicitCastExpr 0x5aead50 <col:10> 'int' <LValueToRValue>
`-DeclRefExpr 0x5aead28 <col:10> 'int' lvalue Var 0x5aeac10 'result' 'int'
Note : for avoiding formatting problems with the quote symbol, I will replace it with \
I don't really understand when we can tell that two nodes have the same parent. I obviously understand that it depends on the identation level, but for example the third line begins with \-
so I guess the \
means that the CompoundStmt
is a child of ParmVarDecl
. Yet, the indentation level is the same.
Line 4, the DeclStmt
is more indented than the previous line, however there is no \
sign so is it considered a subnode of the CompoundStmt
or its child ?
Finally, in the DeclStmt
we have more and more indented lines beginning with \-
, what is the difference between this and the line 3 (starting by \-
too but having the same indentation level as the previous line) ?
Could someone explain me the semantics of this starting symbols and indentation level ?
Upvotes: 2
Views: 359
Reputation: 45654
Replace their corner-symbol (they restrict themselves to ASCII) with unicode U+2514 Box drawings light up and right └
, and you'll see it better:
... cutting out internal declarations of clang ...
└─FunctionDecl 0x5aeab50 <test.cc:1:1, line:4:1> f 'int (int)'
├─ParmVarDecl 0x5aeaa90 <line:1:7, col:11> x 'int'
└─CompoundStmt 0x5aead88 <col:14, line:4:1>
├─DeclStmt 0x5aead10 <line:2:3, col:24>
│ └─VarDecl 0x5aeac10 <col:3, col:23> result 'int'
│ └─ParenExpr 0x5aeacf0 <col:16, col:23> 'int'
│ └─BinaryOperator 0x5aeacc8 <col:17, col:21> 'int' '/'
│ ├─ImplicitCastExpr 0x5aeacb0 <col:17> 'int' <LValueToRValue>
│ │ └─DeclRefExpr 0x5aeac68 <col:17> 'int' lvalue ParmVar 0x5aeaa90 'x' 'int'
│ └─IntegerLiteral 0x5aeac90 <col:21> 'int' 42
└─ReturnStmt 0x5aead68 <line:3:3, col:10>
└─ImplicitCastExpr 0x5aead50 <col:10> 'int' <LValueToRValue>
└─DeclRefExpr 0x5aead28 <col:10> 'int' lvalue Var 0x5aeac10 'result' 'int'
Used some more box-drawing characters to prettify the lines...
Upvotes: 3