%{
#include <stdlib.h>
#include "life1.h"

long lineno = 1;

int yylex(void) {
    int c = fgetc(infile);
    if (c=='\n') {
        lineno++;
    }
    if (c==EOF) {
        return 0;
    }
    else {
        return c;
    }
}

void yyerror(char const *s) {
  fprintf (stderr, "%ld: %s\n", lineno, s);
  exit(1);
}
%}

%union {
  long num;
}

%type <num> digit unum num

%%
start: aheader lines
     ;

aheader:
        | '#''A''\n'
        ;

white:
     | white ' '
     ;

lines:
     | lines line
     ;

line: white num white num white '\n' { setAlive($2, $4); }
    ;

num: '-' unum { $$ = - $2; }
   | unum     { $$ = $1; }
   ;

unum: digit      { $$ = $1; }
    | unum digit { $$ = $1 * 10 + $2; }
    ;

digit: '0' { $$ = 0; }
     | '1' { $$ = 1; }
     | '2' { $$ = 2; }
     | '3' { $$ = 3; }
     | '4' { $$ = 4; }
     | '5' { $$ = 5; }
     | '6' { $$ = 6; }
     | '7' { $$ = 7; }
     | '8' { $$ = 8; }
     | '9' { $$ = 9; }
     ;
%%

