%default-actions quiet
%filenames parser
%scanner ../scanner/scanner.h

                                // lowest precedence
%token  NUMBER                  // integral numbers
        EOLN                    // newline

%left   '+' '-' 
%left   '*' '/' 
%right  UNARY
                                // highest precedence 
%%

expressions:
    expressions 
    evaluate
|
    prompt
;

evaluate:
    alternative
    prompt
;

prompt:
    {
        prompt();
    }
;

alternative:
    expression
    EOLN
    {
        cout << $1 << '\n';
    }
|
    'q'
    done
|
    EOLN
|
    error
    EOLN
;

done:
    {
        cout << "Done.\n";
        ACCEPT();
    }
;

expression:
    expression 
    '+'
    expression
    {
        $$ = $1 + $3;
    }
|
    expression 
    '-'
    expression
    {
        $$ = $1 - $3;
    }
|
    expression 
    '*'
    expression
    {
        $$ = $1 * $3;
    }
|
    expression 
    '/'
    expression
    {
        $$ = $1 / $3;
    }
|
    '-'             
    expression      %prec UNARY
    {
        $$ = -$2;
    }
|
    '+'             
    expression      %prec UNARY
    {
        $$ = $2;
    }
|
    '('
    expression
    ')'
    {
        $$ = $2;
    }
|
    NUMBER
    {
        $$ = atoi(d_scanner.matched().c_str());
    }
;
