-
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
-
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. :)
-
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?
-
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.
-
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;
*/
}