help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Read and write to files within Bison


From: Travis Fischer
Subject: Read and write to files within Bison
Date: Mon, 17 Feb 2003 21:58:05 -0800

I am trying to get a small translator working with bison. When I execute my
parser, I want to say 3 things on the command line:
> parser inputfile outputfile

I have been trying to do this, but I keep getting a core dump when I test my
program.

Here is my lex file:
%{
#include <stdlib.h>
#include "y.tab.h"
/* extern int yylval;*/
%}
digit [0-9][0-9]*
id [a-zA-Z][a-zA-Z0-9]*
%%
"#"[^\n]* ; /* Ignore single-line comments */
"forward" { return FORWARD; }
"right"  { return RIGHT; }
"jump"  { return JUMP; }
"repeat" { return REPEAT; }
"to"  { return TO; }
"end"  { return END; }
"if"  { return IF;}

"+"      { return PLUS; }
"*"      { return MULT; }
"/"      { return DIV; }
"<"  { return LESS; }
"="  { return EQUAL; }
"~="  { return NOT_EQUAL; }
">"  { return GREATER; }
">="    { return GREATER_EQUAL; }
"<="  { return LESS_EQUAL; }
"["  { return OPEN_ARRAY; }
"]"  { return CLOSE_ARRAY; }
"("  { return OPEN_PAREN; }
")"  { return CLOSE_PAREN; }
":"  { return COLON; }
{digit}         { yylval = atoi((char *)yytext); return NUMBER; }

{id}          { yylval.varindex = yytext[0] - 'a'; return NAME; }

\n|.   { return yytext[0]; }
%%

Here is my bison file:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern int yylex();
extern FILE *yyin;
extern FILE *yyout;

void yyerror (const char *msg) {
  printf("ERROR(PARSER): %s\n", msg);
}

char text[256];
char vars[26];
int test;
%}

%token NUMBER
%token NAME
%token FORWARD
%token RIGHT
%token JUMP
%token REPEAT
%token TO
%token END
%token IF
%token PLUS
%token MULT
%token MINUS
%token DIV
%token LESS
%token EQUAL
%token NOT_EQUAL
%token GREATER
%token GREATER_EQUAL
%token LESS_EQUAL
%token OPEN_ARRAY
%token CLOSE_ARRAY
%token OPEN_PAREN
%token CLOSE_PAREN
%token COLON

%%

program  : block  {fprintf(yyout, "stroke\nshowpage\n");}
  ;

block   : block action
  | action
  | parameterlist
  | block parameterlist
  ;

action  : function {}
  | statement {}
  ;

function : TO NAME block END
  ;

statement   : word expression {}
  | IF expression body  {}
  | body
  ;

expression : addexp boolean addexp
  | addexp
  ;

addexp  : addexp addop term
  | addop term
  | term
  ;

addop  : PLUS
  | MINUS
  ;

term  : term mulop factor
  | factor
  ;

mulop  : MULT
  | DIV
  ;

factor  : OPEN_PAREN expression CLOSE_PAREN
  | NAME
  | num
  | COLON NAME
  | factor num
  | MINUS num
  ;

boolean  : GREATER
  | LESS
  | GREATER_EQUAL
  | LESS_EQUAL
  | NOT_EQUAL
  | EQUAL
  ;

body  : OPEN_ARRAY block CLOSE_ARRAY
  ;

parameterlist : COLON NAME
  | COLON NAME addexp
  ;


word  : FORWARD  {strcpy (text, "rlineto");}
  | RIGHT  {strcpy (text, "rotate");}
  | JUMP  {strcpy (text, "moveto");}
  | REPEAT
  | NAME
  ;

num  : NUMBER { if (strcmp (text, "rotate"))
        fprintf(yyout, "0 %i %s\n", $1, text);
      if (strcmp (text, "rlineto"))
        fprintf(yyout, "%d %s\n", $1, text);
/*      if (strcmp (text, "moveto"))
        fprintf(yyout, "0 %i %s\n", $1, text);
*/    }
  ;
%%

 int main(int argc, char *argv[]) {
  int i;
  FILE *yyout;

  for (i=0; i<26; i++)

  vars[i] = 1;  //initialize all variables to true

  fprintf(yyout, "0 0 moveto\n");

  if (argc == 1)
    yyparse();

  if (argc == 2) {
    yyin = fopen(argv[1], "r");
    yyparse();
  }

  if (argc == 3) {

   yyout = fopen(argv[2],"w");

   yyin = fopen(argv[1], "r");
   yyparse();
   fclose(yyout);
  }

  return 0;
}


Why do I keep getting core dumps? There is not a lot of info on the web
about this stuff.

Travis





reply via email to

[Prev in Thread] Current Thread [Next in Thread]