Steven T. Hatton hattons at globalsymmetry.com
Thu Aug 25 06:19:05 UTC 2005

I have so many thoughts and questions on this subject that I could write a 
book.  I guess I should probably take it one piece at a time.  First let me 
point out that I have found a very nice discussion of the subject of syntax 
and parsing here:


When I went to copy the link, I tried to get to the top of the book and found 
there is more here than I originally thought.  What I had been reading looks 
to be an older version.  There appears to be two (or more) separate books, 
and at least two version of the Programming Languages book.

What I had been looking at is rooted here:

There seems to be a more recent version here:

(I don't like the info-style layout as much as the free-flow of the older 
version FWIW.)

Then there's another book:

The Logical Foundations of Computer Science and Mathematics

That is the Queen of the Sciences!

Now, here's my question for Robe, or for anyone else who cares to address it.  
I now realize there are two distinct sets of nodes in a generalized parse 
tree.  There are abstract nodes, and there are concrete nodes.  The abstract 
nodes do not immediately represent tokens in the parsed language.  

Interestingly, two abstract nodes can represent the exact same range of 
terminals.  To expose this I hacked on Robe's DumpTree code a bit to create a 
poor-man's XML dumper:

// XmlDumpTree.h

#include "default_visitor.h"
#include "XmlDumpTree.h"
#include <iostream>

class XmlDumpTree: protected DefaultVisitor {
  XmlDumpTree(std::ostream &out_ = std::cout, unsigned char indent_ = 2);
  void dump(AST *node) { visit(node); }

  virtual void visit(AST *node);

  std::ostream &_out;
  unsigned char _indent;    


// XmlDumpTree.cpp
#include "XmlDumpTree.h" //N.B. the mixed case.
#include <iostream>

static char const * const names[] = {

XmlDumpTree::XmlDumpTree(std::ostream &out_, unsigned char indent_)
  : _out(out_)
  , _indent(indent_)

void XmlDumpTree::visit(AST *node) {
  static int _indent = 0;
  if (node) _out
    <<std::string(_indent * 2, ' ')<<"<"<<names[node->kind]
    <<" \"start="<<node->start_token<<"\""
    <<" \"end="<<node->end_token<<"\">"<<"\n";

  if (node) _out<<std::string(_indent * 2, ' 


$ diff main.cpp /download/org/kdevelop/kdevelop-4/parser/main.cpp 
< #include "XmlDumpTree.h"
> #include "dumptree.h"
<   if (ast && dump) {
<     XmlDumpTree dump(std::cout);
<     dump.dump(ast);
<   }
>   if (ast && dump)
>     {
>       DumpTree dump(std::cout);
>       dump.dump(ast);
>     }

// HelloWorld.cpp 
#include <iostream>

int main () {
  std::cout<<"Hello World"<<std::endl;

 ./r++0 -dump HelloWorld.cpp
<TranslationUnit "start=0" "end=16">
  <FunctionDefinition "start=0" "end=16">
    <SimpleTypeSpecifier "start=0" "end=1">
    <InitDeclarator "start=1" "end=4">
      <Declarator "start=1" "end=4">
        <Name "start=1" "end=2">
          <UnqualifiedName "start=1" "end=2">
        <ParameterDeclarationClause "start=3" "end=3">
    <CompoundStatement "start=4" "end=16">
      <ExpressionStatement "start=5" "end=15">
        <BinaryExpression "start=5" "end=14">
          <BinaryExpression "start=5" "end=10">
            <PostfixExpression "start=5" "end=8">
              <PrimaryExpression "start=5" "end=8">
                <Name "start=5" "end=8">
                  <UnqualifiedName "start=5" "end=6">
                  <UnqualifiedName "start=7" "end=8">
            <PostfixExpression "start=9" "end=10">
              <PrimaryExpression "start=9" "end=10">
                <StringLiteral "start=9" "end=10">
          <PostfixExpression "start=11" "end=14">
            <PrimaryExpression "start=11" "end=14">
              <Name "start=11" "end=14">
                <UnqualifiedName "start=11" "end=12">
                <UnqualifiedName "start=13" "end=14">

Notice how 
<PostfixExpression "start=11" "end=14">
  <PrimaryExpression "start=11" "end=14">
     <Name "start=11" "end=14">
all share the same token range.  I assume that means [11,14).

My question is this: do any of the nodes in the parse tree formally represent 
the concrete tokens in the source code being parsed?  Can these be easily 
identified?  Is it simply a matter of determining if they are terminals?

More information about the KDevelop-devel mailing list