The correct way to parse S-expressions in OOP

I am looking for a way to implement an S-expression reader (which will be used later with both the Scheme interpreter and the compiler), but I asked myself how (if at all) I should write an AST for it.

I read SICP, and it is quite simple from inside the Scheme, but I am looking for an OO-style interpreter and compiler in C ++.

Please keep in mind that I am only doing this for educational purposes, so I am not looking for the easiest or fastest way to do this, but rather the right and reusable way to do this.

I saw in some implementations of the Scheme that people parse s-expressions and easily output cons-cells, something like this:

  struct Sexpr
  {
  };

  struct Cons : public Sexpr
  {
    Sexpr* left;
    Sexpr* right;
  };

  struct IntAtom : Sexpr
  {
    int value;
  };

And one subclass of Sexpr for each type of Schema Atom, or something on these lines.

, ... , ?

, ( ) S-, , ? , cons-?

+5
4

Scheme/Racket :

Racket ( ) , , ( , Racket), , , , (. " " Racket).

, , .

, " " " ", - .

--- , ---, , ; , , .

+3

,

sexpr ::= atom | sexpr sexpr
atom ::= nil | intatom | etc.

, sexpr, . S-expr, LISP/ (a b c d), a, b, c, d . [a [b [c [d nil]]]], , conses .

, ,

class sexpr {};
class atom : sexpr {};
class s_list : forward_list<smart_ptr<sexpr>> {};
+3

, , , "" , , , , , , , - , , Lisp "code is data", , , quote ( ).

, , Lisps (, ).

, Lisp : conses, , Lisp , .., , Lisp. .

+3

s-expr c/++ , .

, :

struct elt {
  int type;
  char *val;
  struct elt *list; 
  struct elt *next;
};

:

, , , LIST, VALUE. , "" , . VALUE, "val" , . "next" s-.

Additionally, here is a list of other implementations of s-expr readers in many languages ​​that may be of interest.

+1
source

All Articles