/media/sda-magnetic/david/Dokumente-15/fernuni-hagen/cs-i-ii/fsm/fsm/lex-yacc-bash-etc/yacc.cc


%{
#include <ctype.h>
#include <stdio.h>
%}
%token NUMBER
%token VAR
%%
statement       : assignment
                | cond
                | loop
                ;
assignment      : VAR ":=" expr                                             { $1 = $3; }
                ;
cond            : "if" boolexpr "then" statement "fi"                       { if ($2) { $4; }} 
                | "if" boolexpr "then" statement "else" statement "fi"      { if ($2) { $4; } else { $6; }}
                ;
loop            : "while" boolexpr "do" statement "od"                      { while ($2) {$3;}}
                ;
expr            : numexpr
                | boolexpr
                ;
boolexpr        : numexpr '<' numexpr                                       { $1 < $3; }
                ;
lines           : lines statement '\n'  
                | lines '\n'
                ;
numexpr         : numexpr '+' term { $$ = $1 + $3; }
                | numexpr '-' term { $$ = $1 - $3; }
                | term
                ;
term            : term '*' factor { $$ = $1 * $3; }
                | term '/' factor { $$ = $1 * $3; }
                | factor
                ;
factor          : '(' numexpr ')' { $$ = $2; }
                | NUMBER
                ;
%%
int yylex() {
    int c;
    c = getchar();
    if (isdigit(c)) {
        ungetc(c, stdin); 
        scanf("%d", &yylval);
        return NUMBER;
    }
    return c;
}