#ifndef PARSER_H #define PARSER_H #include #include #include "token.h" #include "number.h" namespace Bu { class Stream; }; class Lexer; /** * * 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, Bu::Stream &rOut ); virtual ~Parser(); void parse(); Number getVariable( const Bu::String &sName ); void setVariable( const Bu::String &sName, const Number &rValue ); private: void expr(); void exprP(); private: void unwind(); int reqTokens( Token::Type eType ); int getPriority( Token::Type eType ); Number &deref( Token &t ); private: Lexer &lex; Bu::Stream &rOut; typedef Bu::List TokenStack; typedef Bu::Hash VarHash; TokenStack tsStack; // TokenStack tsNonTerminal; Number nZero; VarHash hVars; }; #endif