-3

i am learning Common LISP at the moment and i encountered a huge roadblock. I've had an assignment that we were to learn how to create a parser in Common LISP. I have managed to implement everything from the grammar rules to the lexer with alot of help from different sources online. I cant on the other hand seem to figure out how to implement a symbol table.

This is what i have so far in regards of the symbol table.

(defun symtab-add (state id)
;; *** add symbols to symbol table ***
)

(defun symtab-member (state id)
;; *** look up symbols in symbol table ***
)

(defun symtab-display (state)
   (format t "------------------------------------------------------~%")
   (format t "Symbol Table is: ~S ~%" (pstate-symtab state))
   (format t "------------------------------------------------------~%")
)

As you can see ive only managed with the display part, if someone could link me a tutorial or give me a code example or just help me with this i would be super thankful.

All source code for my assignment: http://www.cs.kau.se/cs/education/courses/dvgc01/LISP/newstart.lsp

Bart van Nierop
  • 4,130
  • 2
  • 28
  • 32
Joo223
  • 1
  • 1
  • 1
    The code is from here: http://www.cs.kau.se/cs/education/courses/dvgc01/LISP/newstart.lsp – Rainer Joswig Jul 21 '16 at 18:56
  • "How to simulate a symbol table in Common LISP" 1) It's "Common Lisp", not "Common LISP" (no need to shout). 2) As Rainer points out, that code is taken from elsewhere. You should provide attribution for code that you post, and *certainly* shouldn't claim that it's your own. 3) Why "simulate" a symbol table? Wouldn't you just want to implement one? This phrasing suggests that a symbol table couldn't be implemented in Common Lisp, which is silly. – Joshua Taylor Jul 21 '16 at 19:21
  • Yes that is indeed my assignment, i am sorry if that was unclear. Ok so i want to implement a symbol table (the assignment is to parse a simple pascal program). Can you give me some pointers or point me in the right direction? As you can see from the link you sent its unfinished code. It's my assignment to finish it. Everything is done except for the symbol table. – Joo223 Jul 22 '16 at 01:58

2 Answers2

1

There are multiple ways of implementing a symbol table, with varying levels of "suitable for purpose" depending on your exact needs. At the end of the day, a symbol table is, effectively, just a mapping from "symbol name" to something.

So any data structure that allows you to add things to it as well as looking things up should work. Fairly common implementations would be "use a hash table" or "use an alist" (the latter is essentially a list of pairs on the form (<symbol> . <data>)).

Vatine
  • 20,782
  • 4
  • 54
  • 70
  • Ok so hash table is the way to go? Thats food cause ive found alot on hash tables and clisp. Thanks for the answer! – Joo223 Jul 24 '16 at 14:56
-3

First you will have to create the symbol table, say:

(setq my-symbol-table nil)

To keep it simple, we will ignore packages; you can learn about that later.

Next, you have to decide how you are going to store the symbols in the table. Again, keeping it simple, you can store them as an association list, with pairs of symbol name and symbol value. For example, if you store the symbols a and b with the values 3 and 5, you would have the following symbol table:

> my-symbol-table

> ((a . 3) (b . 5))

To use this association list, you can use the functions assoc, push, rplacd.

Example:

  • Add a new symbol

    (push '(c . 0) my-symbol-table)

  • Look up a symbol:

    (assoc 'c my-symbol-table)

  • Change the value of an existing symbol:

    (rplacd (assoc 'c my-symbol-table) 18)

I hope this is enough to get you going.

Leo
  • 1,869
  • 1
  • 13
  • 19
  • 2
    `setq` is for changing existing bindings. Where do you define `my-symbol-table` and where are the `*earmuffs*`? – Sylwester Jul 21 '16 at 20:37
  • I don't see anything in OP's question that looked like symbols needed to be associated to particular values. It looked more like there just needed to be a list of "in scope names" (which makes a bit more sense for lexer/parser, though it's still a bit more than pure lexing and parsing would need; semantic analysis should be responsible for checking variables scopes, etc.). – Joshua Taylor Jul 21 '16 at 21:00