1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
/*
Copyright (C) 2017 Christoph Berg
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
%{
#include <strings.h> /* bzero */
#include "numeral.h"
/* flex/bison prototypes */
int yyzahllex (void);
struct yyzahl_buffer_state *yyzahl_scan_string(char *str);
void yyzahl_delete_buffer(struct yyzahl_buffer_state *buffer);
void yyzahlerror (char const *s);
static Zahl *numeral_parse_result; /* parsing result gets stored here */
%}
%define parse.error verbose
%define api.prefix {yyzahl}
%define api.value.type {Zahl}
%token INT
%token MINUS
%token ZERO
%token AND
%token ONE
%token TEN
%token HUNDRED
%token THOUSAND
%token ZILLION
%token ERR
%%
input: /* parser entry */
INT { *numeral_parse_result = $1; }
| ZERO { *numeral_parse_result = 0; }
| expr { *numeral_parse_result = $1; }
| MINUS expr { *numeral_parse_result = -$2; }
;
expr: /* general number */
xxxxxx
| prefix_xxx ZILLION expr_tail { $$ = $1 * $2 + $3; }
;
expr_tail: /* tail of general number */
%empty { $$ = 0; }
| xxxxxx
| prefix_xxx ZILLION expr_tail { $$ = $1 * $2 + $3; }
xxxxxx: /* 6-digit number */
xxx
| prefix_xxx THOUSAND suffix_xxx { $$ = $1 * $2 + $3; }
;
prefix_xxx: /* N-thousand */
%empty { $$ = 1; }
| xxx
;
suffix_xxx: /* thousand-and-N */
%empty { $$ = 0; }
| xxx
;
xxx: /* 3-digit number */
xx
| prefix_x HUNDRED suffix_xx { $$ = $1 * $2 + $3; }
;
xx: /* 2-digit number */
ONE
| TEN
| ONE AND TEN { $$ = $1 + $3; }
;
prefix_x: /* N-hundred */
%empty { $$ = 1; }
| ONE
;
suffix_xx: /* hundred-and-N */
%empty { $$ = 0; }
| xx
;
%%
/* parse a given string and return the result via the second argument */
int
zahl_parse (char *s, Zahl *zahl)
{
struct yyzahl_buffer_state *buf;
int ret;
numeral_parse_result = zahl;
buf = yyzahl_scan_string(s);
ret = yyzahlparse();
yyzahl_delete_buffer(buf);
return ret;
}
|