/* * Copyright (C) 2019 Vivien Kraus * * 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 3 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see * . */ %{ #include #include #include #include #define _(String) String int yylex (void); void yyerror (const char *error); int main (int argc, char *argv[]); void command (const char *format, ...); int prompt_type; int yywrap () { return 1; } %} %define api.value.type union %token THAT %token WILL %token BE %token ALL %token A %token KG %token BUNCH %token BUNCHES %token OF %token POTATOES %token RADISHES %token END_OF_PHRASE %token INTEGER %token DECIMAL %token UNKNOWN_WORD %destructor { free ($$); } UNKNOWN_WORD %type bunches %type weight %% interaction: %empty | interaction command | interaction error END_OF_PHRASE { prompt_type = 1; marchande_syntax_error (); yyerrok; } ; command: THAT WILL BE ALL { command (_ ("that will be all")); YYACCEPT; } | weight OF POTATOES { marchande_buy_potatoes ($1); command (_ ("%f kg of potatoes"), $1); } | bunches OF RADISHES { marchande_buy_radishes ($1); command (_ ("%d bunches of radishes"), $1); } | weight OF UNKNOWN_WORD { marchande_value_error ($3); command (_ ("%f kg of %s"), $1, $3); free ($3); } | bunches OF UNKNOWN_WORD { marchande_value_error ($3); command (_ ("%d bunches of %s"), $1, $3); free ($3); } ; weight: DECIMAL KG { $$ = $1; } | INTEGER KG { $$ = (double) $1; } ; bunches: A BUNCH { $$ = 1; } | INTEGER BUNCHES { $$ = $1; } ; %% int prompt_type; void yyerror (const char *error) { (void) error; } int main (int argc, char *argv[]) { (void) argc; (void) argv; if (marchande_init () != 0) { fprintf (stderr, "Could not initialize marchande.\n"); return EXIT_FAILURE; } if (yyparse () == 0) { marchande_exit (); return EXIT_SUCCESS; } return EXIT_FAILURE; } int prompt_type; void command (const char *format, ...) { (void) format; prompt_type = 1; }