Thread: Writing an interpreter and parsing

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Tucson, Arizona, United States
    Posts
    10

    Writing an interpreter and parsing

    Hi guys,

    so for this assignment I have to implement a BASIC like interpreter. I have 4 files, and I know how to handle 2 of them with some ideas on the other 2, but I'm having trouble with my very first file implement.

    input.c reads in a Stmt* from main and returns a Stmt pointer for context, I supply the main.c and interpret.h files

    Code:
    
    interpreter.h
    
    enum stmt {
            PRINT = 1,
            IF,
            LET,
            END,
            RUN,
            LIST
    };
    
    
    enum term {
            VAR = 1,
            INT
    };
    
    
    typedef struct termNode {
            enum term type;
            union {
                    char *var;
                    int intlit;
            } u;
    } Term;
    
    
    typedef struct stmtNode {
            int line;
            enum stmt type;
            struct stmtNode *next;
            union {
                    struct {
                            char *var;
                    } print;
                    struct {
                            Term *term;
                            int target;
                    } ifs;
                    struct {
                            char *var;
                            Term *left;
                            char op;
                            Term *right;
                    } let;
            } u;
    } Stmt;
    
    
    typedef struct value {
            char *name;
            int value;
            struct value *next;
    } Value;
    
    
    void run(Stmt *);
    void list(Stmt *);
    Stmt * input();
    Stmt *add(Stmt *head, Stmt *p);
    
    main.c
    #include <std.lib>
    #include "interpreter.h"
    
    int main(int argc, char *argv[]) {
            Stmt *p;
            Stmt *head;
    
    
            head = NULL;
    
    
            while ((p = input())) {
                    switch (p->type) {
                    case RUN:
                            free(p);
                            run(head);
                            break;
                    case LIST:
                            free(p);
                            list(head);
                            break;
                    default:
                            head = add(head, p);
                            break;
                    }
            }
    }
    What I'm trying to do is take a string from stdin, parse it to a Stmt structure, and return a pointer to it.
    Code:
    #include <stdio.h
    #include <stdlib.h>
    #include <string.h>
    #include <main.c>
    
    
    
    
    char *input(){
            char *string;
             *fgets(char *s, int 100, stdin);
    }
    so if my input is as follows:
    "1 LET a = 1 + 2"
    How would I parse for this in c? I read up on a function strtok, that uses a delimiter to split a string into tokens, would that help?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shoone
    How would I parse for this in c? I read up on a function strtok, that uses a delimiter to split a string into tokens, would that help?
    You first need to read the line. What you have now is wrong:
    Code:
    char *string;
    *fgets(char *s, int 100, stdin);
    The above does not allocate space for the string, then the function call looks like it cannot decide if it is a function call or a function declaration. Rather, you might write something like this:
    Code:
    char line[BUFSIZ];
    if (fgets(line, BUFSIZ, stdin))
    {
        /* ... parse the line ... */
    }
    Resist the temptation to proceed too fast without compiling and testing. So, given my example above, try getting your program to just read and output the lines of BASIC-like code input as-is without parsing them. Once you can do this right, go on to the next step, i.e., figure out how you can parse a statement as given in a line, keeping in mind that a statement has different possible types.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Tucson, Arizona, United States
    Posts
    10
    Well, I got it compiling and logically working, but my other problem is I don't know how to feed in a .in file from stdin in order to do testing

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Shoone View Post
    Well, I got it compiling and logically working, but my other problem is I don't know how to feed in a .in file from stdin in order to do testing
    run your program from command line like this

    myprog.exe < myfile.in
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 11-11-2012, 11:06 PM
  2. interpreter
    By abuashraf in forum C Programming
    Replies: 4
    Last Post: 04-01-2008, 09:10 AM
  3. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  4. Help with an Interpreter
    By flaran in forum C Programming
    Replies: 3
    Last Post: 10-19-2005, 08:28 PM
  5. C CGI Interpreter
    By johnc in forum C Programming
    Replies: 5
    Last Post: 07-19-2002, 12:56 PM

Tags for this Thread