summaryrefslogtreecommitdiff
path: root/src/parser.h
blob: cc97222f806c929746bfb632eff8b7f3e3fc6177 (plain)
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
#ifndef PARSER_H
#define PARSER_H

#include <bu/list.h>
#include <bu/hash.h>
#include "token.h"
#include "number.h"

namespace Bu
{
    class Stream;
};

class Lexer;
class Expression;

/**
 *
 * expr: literal
 *     | variable
 *     | expr '+' expr
 *     | expr '-' expr
 *     | expr '*' expr
 *     | expr '/' expr
 *     | '(' expr ')'
 *     | '-' expr
 *     | variable '=' expr
 *     ;
 *
 * -----
 * 
 * expr': literal
 *      | variable
 *      | '(' expr ')'
 *      | '-' expr'
 *      ;
 *
 * expr: expr' '+' expr
 *     | expr' '-' expr
 *     | expr' '*' expr
 *     | expr' '/' expr
 *     | expr'
 *     | variable '=' expr
 *     ;
 */
class Parser
{
public:
    Parser( Lexer &lex );
    virtual ~Parser();

    Expression *parse();

private:
    void expr();
    void exprP();
    void exprR();
    void statement();

private:
    void shift( const Token &t );
    void reduce();
    void output( const Token &t );
    int reqTokens( Token::Type eType );
    int getPriority( Token::Type eType );

    void printState( const Bu::String &sTag );

private:
    Lexer &lex;
    typedef Bu::List<Token> TokenStack;
    TokenStack tsTerminal;
    TokenStack tsNonTerminal;
    Expression *pCurExp;
};

#endif