Loading ...
Sorry, an error occurred while loading the content.

symbol table in C++

Expand Messages
  • Fan Yang
    Hi, I want to reimplement the symbol table by using C++ and STL, which is described on http://developer.java.sun.com/developer/technicalArticles/Parser/Serie
    Message 1 of 4 , May 29 12:04 PM
    • 0 Attachment
      Hi,

      I want to reimplement the symbol table by using C++ and STL, which is
      described on
      http://developer.java.sun.com/developer/technicalArticles/Parser/Serie
      sPt4.

      I'm stuck at compiling this implementation. The problem seems forward
      reference or compile order. For example JavaHashtable, Definition and
      SymbolTable, they use each other in the declarations of their class
      members. it compiles correctly in java. but c++ is not the case.

      Is there a way to compile it without major modification of the
      design. Or how to modify the symbol table design in order to comform
      with c++ compier?

      thank you at advance.
    • lgcraymer
      The trick is that Java uses references--each of the cross-referenced items is actually a pointer, but the compiler hides this--and all pointers are of fixed
      Message 2 of 4 , May 29 10:40 PM
      • 0 Attachment
        The trick is that Java uses references--each of the cross-referenced
        items is actually a pointer, but the compiler hides this--and all
        pointers are of fixed size which makes memory allocation easy. To
        do the same thing in C++, you will have to make the references
        explicit--"HashTable& foo" for example instead of "Hashtable foo".
        You may also need to declare a forward reference to the class--that
        should just be a line of the form
        class Hashtable;

        --Loring

        --- In antlr-interest@yahoogroups.com, "Fan Yang" <yhhf_dy@y...>
        wrote:
        > Hi,
        >
        > I want to reimplement the symbol table by using C++ and STL, which
        is
        > described on
        >
        http://developer.java.sun.com/developer/technicalArticles/Parser/Seri
        e
        > sPt4.
        >
        > I'm stuck at compiling this implementation. The problem seems
        forward
        > reference or compile order. For example JavaHashtable, Definition
        and
        > SymbolTable, they use each other in the declarations of their
        class
        > members. it compiles correctly in java. but c++ is not the case.
        >
        > Is there a way to compile it without major modification of the
        > design. Or how to modify the symbol table design in order to
        comform
        > with c++ compier?
        >
        > thank you at advance.
      • Fan Yang
        Thank you for your help. ... referenced ... That s exactly what I did. I have used pointer/reference throughout the new implementation. ... I have used forward
        Message 3 of 4 , May 30 1:16 PM
        • 0 Attachment
          Thank you for your help.

          > The trick is that Java uses references--each of the cross-
          referenced
          > items is actually a pointer, but the compiler hides this--and all
          > pointers are of fixed size which makes memory allocation easy. To
          > do the same thing in C++, you will have to make the references
          > explicit--"HashTable& foo" for example instead of "Hashtable foo".

          That's exactly what I did. I have used pointer/reference throughout
          the new implementation.

          > You may also need to declare a forward reference to the class--that
          > should just be a line of the form class Hashtable;

          I have used forward references in the new implementation. But the
          problem is that you can't reference members of the type which is
          defined by forward reference. For example, there are compile errors
          if you use forward referennce for hashtable or symboltable. The
          reason is that JavaHashtable calls SymbolTable.LookupDummy(), and
          SymbolTable calls JavaHashtable.get().

          The following code snip simplifies the former problem.
          class B;
          class A{
          private:
          B*pb;
          public:
          A(B*b):pb(b){}
          void hello(){
          pb->hello(); // complie error
          }
          };

          class B{
          private:
          A*pa;
          public:
          ...
          void hello(){
          }
          }

          I know Template can overcome this simple problem. but the symbol
          table program is much more complex than above example. I'm not sure
          it's a right way to do it. Does there exist some kind of patterns to
          deal with conversion from java to c++ code. thanks.

          Fan
        • lgcraymer
          If you follow the canonical C++ paradigm, non-inlined method code should be defined in .cpp files, with class definitions, including method prototypes, in a .h
          Message 4 of 4 , May 31 11:26 AM
          • 0 Attachment
            If you follow the canonical C++ paradigm, non-inlined method code
            should be defined in .cpp files, with class definitions, including
            method prototypes, in a .h file. For your example, A::hello() might
            be defined in an A.cpp file that looks like

            #include "A.h" // defines class A
            #include "B.h" // defines calss B

            void A::hello() {
            pb->hello();
            }

            with A.h something like

            class B;
            class A {
            private:
            B *pb;
            public:
            A(B *);

            void hello();
            };

            --Loring


            --- In antlr-interest@yahoogroups.com, "Fan Yang" <yhhf_dy@y...>
            wrote:
            > Thank you for your help.
            >
            > > The trick is that Java uses references--each of the cross-
            > referenced
            > > items is actually a pointer, but the compiler hides this--and
            all
            > > pointers are of fixed size which makes memory allocation easy.
            To
            > > do the same thing in C++, you will have to make the references
            > > explicit--"HashTable& foo" for example instead of "Hashtable
            foo".
            >
            > That's exactly what I did. I have used pointer/reference
            throughout
            > the new implementation.
            >
            > > You may also need to declare a forward reference to the class--
            that
            > > should just be a line of the form class Hashtable;
            >
            > I have used forward references in the new implementation. But the
            > problem is that you can't reference members of the type which is
            > defined by forward reference. For example, there are compile
            errors
            > if you use forward referennce for hashtable or symboltable. The
            > reason is that JavaHashtable calls SymbolTable.LookupDummy(), and
            > SymbolTable calls JavaHashtable.get().
            >
            > The following code snip simplifies the former problem.
            > class B;
            > class A{
            > private:
            > B*pb;
            > public:
            > A(B*b):pb(b){}
            > void hello(){
            > pb->hello(); // complie error
            > }
            > };
            >
            > class B{
            > private:
            > A*pa;
            > public:
            > ...
            > void hello(){
            > }
            > }
            >
            > I know Template can overcome this simple problem. but the symbol
            > table program is much more complex than above example. I'm not
            sure
            > it's a right way to do it. Does there exist some kind of patterns
            to
            > deal with conversion from java to c++ code. thanks.
            >
            > Fan
          Your message has been successfully submitted and would be delivered to recipients shortly.