Yacc Interpreter

This is a discussion on Yacc Interpreter within the C Programming forums, part of the General Programming Boards category; I've written a file parser.y which I ran throught bison to get parser.tab.c and then compiled that to get an ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    Question Yacc Interpreter

    I've written a file parser.y which I ran throught bison to get parser.tab.c and then compiled that to get an executable ./parser which I can run and it works fine, but what I want to do is build an interpreter with a prompt such as '$ ' or '% ' or something like that.

    Also I want it to have the ability to quit when I type the command quit.

    A final functionality that I want to build into it is a history, so that you can hit the up arrow key to back in the history of commands.

    Can someone provide info on how to do this? I searched all over google and was unable to find out how to do this. I have the O'Reilly book of lex&yacc and I don't think that explains it either.

    Thanks,
    Discostu

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    Question

    Please excuse my ignorance, I am new to yacc (I have only written interpreters in Scheme before).

    Where do you put the input from strings code? what about the while loop? Also, what is function is myparse(buff) referring to (is is something that I need to write)?

    Thanks.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    Question error in flex code

    I've typed in the code from page 156 of the book and I get an error "invalid operands to binary -"

    Code:
    extern char myinput[];
    extern char *myinputptr;
    extern int *myinputlim;
    
    int my_yyinput(char *buf, int max_size) {
      int n = min(max_size, myinputlim - myinputptr); //this is the error line
      if(n >0 {
        memcpy(buf, myinputptr, n);
        myinputptr += n;
      }
      return n;
    }
    I've used both cc & gcc to compile it with -lfl for flex library. Is there a library I need to min?
    Why am I getting this error?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    my copy of the book says it is a pointer:
    extern char myinput[];
    extern char *myinputptr;
    extern int *myinputlim;

    weird. Maybe it's a typo in my copy?

    Anyway, I changed it to just be "extern int myinputlim;" but I still get that error.

    I downloaded the progs.tar.gz file and looked and none of those files has that code (that I could find). I couldn't even get them to make. It says undefined reference to yywrap() in main. I tried it with both bison and yacc.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    yeah, I think that's right. I'm having trouble with the main function even though I made my own main function flex is generating the default one also in #if YY_MAIN tag. I've tried setting #define YY_MAIN 0 and manually removing that extra main function from lex.yy.c, but it still says there are multiple main functions. I did a grep main * and the only places it comes up are in mylex.l and lex.yy.c. Here is mylex.l:
    Code:
    %{
    #include <stdio.h>
    #include "y.tab.h"
                                                                                                                                                                
    #define YY_MAIN 0
    #undef YY_INPUT
    #define YY_INPUT(b, r, ms) (r = my_yyinput(b, ms))
    #define MAXIMO 60
    unsigned verbose;
    char *progName;
    %}
                                                                                                                                                                
    %%
                                                                                                                                                                
    -h      |
    "-?"    |
    --help  { printf("usage is: %s [--help | -h | -? ] [--verbose | -v] [(--file | -f) filename]\n", progName); }
    -v      |
    --verbose { printf("verbose mode is on\n"); verbose = 1; }
                                                                                                                                                                
    %%
                                                                                                                                                                
    char myinput[80]; /* input buffer */
    char *myinputptr; /* current position in buffer */
    char *myinputlim; /* end of data */
                                                                                                                                                                
    main(int argc, char **argv)
    {
            char buff[BUFSIZ];
            fgets( buff, BUFSIZ, stdin );
            myinputptr = buff;
            myinputlim = buff + strlen(buff);
            yylex();
    }
                                                                                                                                                                
                                                                                                                                                                
    int yyerror (char *s) /* Called by yyparse on error */
    {
            printf ("%s\n", s);
    }
                                                                                                                                                                
    int yywrap ()
    {
    }
                                                                                                                                                                
    int min(int x, int y)
    {
            if(x < y)
                    return x;
            else
                    return y;
    }
                                                                                                                                                                
    int my_yyinput(char *buf, int max_size)
    {
            /* compiler $$$$$es about subtracting int from ptr, so myinputlim is ptr now
            */
            /* int n = min(max_size, myinputlim - myinputptr); */
            int n = myinputlim - myinputptr;
            if (n > 0) {
                    memcpy(buf, myinputptr, n);
                    myinputptr += n;
            }
            return n;
            /*
            strcpy(buf, "5+5\n");
            return 5;
            */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter Features Question
    By audinue in forum C Programming
    Replies: 0
    Last Post: 10-19-2008, 07:29 AM
  2. Replies: 0
    Last Post: 04-06-2007, 04:55 PM
  3. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 01:33 AM
  4. flex & YACC question
    By cboard_member in forum Tech Board
    Replies: 2
    Last Post: 03-23-2006, 03:58 AM
  5. using YACC and lex to make your own shell...
    By YankeePride13 in forum Linux Programming
    Replies: 2
    Last Post: 12-28-2005, 09:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21