Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all I want to say that C++ grammar is too complex for Lex/Bison. The problem here is primarily in the grammar conflicts. It is not possible to write C++ grammar that does not have them. C++ standard explicitly states this and contains some guidelines on how to resolve them.</p> <p>There is no generic solution for resolving grammar conflicts. In particular grammar conflicts resolution for C++ requires detailed knowledge about already defined identifiers. This means that you need to have bigger part of the C++ front end. Having just the grammar is not enough.</p> <p>Nevertheless building an AST is possible. Look at a small sample program.</p> <pre><code>class HashEntry { private: int key; int value; public: HashEntry(int key, int value) { this-&gt;key = key; this-&gt;value = value; } int getKey() { return key; } int getValue() { return value; } }; const int TABLE_SIZE = 128; class HashMap { private: HashEntry **table; public: HashMap() { table = new HashEntry*[TABLE_SIZE]; for (int i = 0; i &lt; TABLE_SIZE; i++) table[i] = NULL; } int get(int key) { int hash = (key % TABLE_SIZE); while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key) hash = (hash + 1) % TABLE_SIZE; if (table[hash] == NULL) return -1; else return table[hash]-&gt;getValue(); } void put(int key, int value) { int hash = (key % TABLE_SIZE); while (table[hash] != NULL &amp;&amp; table[hash]-&gt;getKey() != key) hash = (hash + 1) % TABLE_SIZE; if (table[hash] != NULL) delete table[hash]; table[hash] = new HashEntry(key, value); } ~HashMap() { for (int i = 0; i &lt; TABLE_SIZE; i++) if (table[i] != NULL) delete table[i]; delete[] table; } }; </code></pre> <p>And this is an AST for this program: <img src="https://i.stack.imgur.com/S6kW9.jpg" alt="enter image description here"></p> <p>This tree is severely zoomed out. Yellow circles at the leafs (very small) are terminal symbols, green circles in the middle are non terminals. Pink circle in the center is the TranslationUnit. This tree has 2009 nodes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload