%{
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "scan.h"

char * strconcat( char *s1, char *s2, char c ) {
	char *s;
	int l1 = strlen(s1), l2=strlen(s2);

	s = (char*)malloc( l1+l2+2 );
	memcpy(s, s1, l1);
	if ( c )
		*(s+l1) = c;
	memcpy( (s+ (l1+((c)?1:0)) ) , s2, l2);
	*( s+(l1+l2+((c)?1:0)) ) = '\0'; 

	return s;	
}

int yyerror(char*);
int yylex(void);
int yyparse(void);

%}
%error-verbose

%union { 
        char    *str;
        u_int32_t  opr;
}

%type <str> version 
%type <str> integer 
%type <opr> symbol 
%type <opr> dot

%token <str> VERSION_MINOR
%token <str> INTEGER 
%token <opr> DOT
%token <opr> CHAR
%token <str> ENDOF

%%

input:
        | input data
;

data:   ENDOF 	{ return 0; }
	| symbol
	| dot
	| version { printf("VERSION:\t'%s'\n", $1); }
	| integer
;

version: 
	INTEGER VERSION_MINOR    { $$ = strconcat($1, $2, '-'); } 
	| version VERSION_MINOR  { $$ = strconcat($1, $2, '+'); }
; 

integer:
	INTEGER	{ printf("INTEGER:\t'%s'\n", $1); }
;

symbol:
	CHAR { printf("CHAR:\t'%c'\n", $1); }
;

dot:
	 DOT   { printf("DOT:\t'%c'\n", $1); }
;

%%

int yyerror(char *s) {
        printf("yyerror: %s\n",s);
        return 0;
}

int main(void) {
        yyparse();
        return 0;
}

