c - Creating a cons cell structure via recursive descent parsing -


i'm working on scheme interpreter written in c. i'm trying create cons cell structure via recursive descent parsing, except instead of having car , cons, have field holds token receive lexical analyzer (which provided us). struct i'm describing such:

typdef struct node node;  typedef node* list;  struct node {      char* symbol;     list car;     list cdr; }; 

thus cons cell (with node represented [symbol][car][cdr]), [null][car][cdr], while symbol [symbol][null][null].

for example, (a b c) represented as: enter image description here

and (a (b c) d), represented as: enter image description here

this previous post on stack overflow: cons cell data structure in c pretty dealing same assignment. in top answer jason post, 1 of suggestions put tokens stack input recursively parsed, , stack input cons cell structure.

this i'm working towards now, easier me understand , have implemented stack in c before, know can build structure recursively, i'm not sure how. following pseudo code have:

list s_expression() {     list local;     list temp;      if (token == "(") {         token = gettoken();         local = new node;         local -> car = s_expression()         temp = local;          while (token != ")") {             temp -> rest = new node;             temp = temp -> rest;             temp -> first = s_expression()         }          temp -> rest = null;         token = gettoken();     } else if (token == symbol) {         list symbolnode = new node;         symbolnode -> symbol = token;         token = gettoken()         return symbolnode;     } else {         return local;     } } 

s_expression supposed return pointer recursively built cons cell structure. i'm having issues figuring out when call gettoken(), either call gettoken in wrong spot , unintentionally skip on token, or call gettoken() when i'm done getting of tokens, causing program continue searching token user input instead of continuing on rest of program.

when should calling gettoken()?

in addition, better, recursively building cons cell structure go through user's input, or putting of tokens stack , building cons cell structure using stack?

if needed, can post lexical analyzer that's been provided us.

i'm thinking if first thing read symbol there no other tokens symbol reading should not new token. true when finished reading list.

when token "(" , read either symbol or expression not have new token after that. before predicate in while need read new token since know searching end of list. means before enter while loop , after read s_expression in body.

other observations

the predicate token == symbol seems strange. allowing same symbol? there other data types lists , symbols justify not having else here?

the last else (alternative) never hit. when reading list never return anything.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -