SymTypeStruct* Parser::ParseStructSpecifier() { SymTypeStruct* result = NULL; if (*lexer.Peek() != IDENTIFIER) { string n = to_string((long double)counter++); BaseToken* dummy = new BaseToken("abstract-struct-"+n, 0, 0, IDENTIFIER, IDENTIFIER); result = new SymTypeStruct(dummy); } else { BaseToken* name = lexer.Get(); result = new SymTypeStruct(name); } if (symStack.Find(result->name->GetText())) { Expected(*lexer.Peek() != FIGURE_LEFT_BRACKET, "redefinition"); return dynamic_cast<SymTypeStruct*>(symStack.Find(result->name->GetText())); } //--------------------------------------------------------------------------- symStack.Push(result->fields); //expected either a definition or a tag name Expected(lexer.Get()->GetSubType(), FIGURE_LEFT_BRACKET); ParseStructDeclaration(); symStack.Pop(); //--------------------------------------------------------------------------- symStack.Add(result); return result; }
void DoProgram() { Block(); if(Look != 'e') { Expected("End"); } EmitLn("END"); }
// Get the escape section void GetEscapePart() { Match('\\'); switch (Look) { case ' ': Look=' '; break; case 'r': Look='\r'; break; case 'n': Look='\n'; break; case 't': Look='\t'; break; case 'b': Look='\b'; break; case '\'': Look='\''; break; case '"': Look='"'; break; default: Expected("Known Escape Sequence"); } }
// Get an Identifier char * GetToken() { int i=0, j, length; if (isspace(Look)) Expected("Command String"); while (!isspace(Look) && (Look != '\0') && (i < MAX_BUF)) { switch (Look) { case '"': GetQuotePart('"'); length=strlen(QuotePart); for (j=0; j<length; j++) Token[i++]=QuotePart[j]; continue; case '\'': GetQuotePart('\''); length=strlen(QuotePart); for (j=0; j<length; j++) Token[i++]=QuotePart[j]; continue; case '\\': GetEscapePart(); break; } Token[i]=Look; GetChar(); i++; } Token[i]='\0'; SkipWhite(); return (Token); }
void MatchString(char *str) { if (strcmp(Value, str) != 0) { sprintf(tmp, "\"%s\"", Value); Expected(tmp); } }
// Bellman recursion using row rearrangement //[[Rcpp::export]] Rcpp::List Bellman(const arma::mat& grid, Rcpp::NumericVector reward_, const arma::cube& scrap, Rcpp::NumericVector control_, const arma::cube& disturb, const arma::vec& weight) { // Passing R objects to C++ const std::size_t n_grid = grid.n_rows; const std::size_t n_dim = grid.n_cols; const arma::ivec r_dims = reward_.attr("dim"); const std::size_t n_pos = r_dims(3); const std::size_t n_action = r_dims(2); const std::size_t n_dec = r_dims(4) + 1; const arma::cube reward(reward_.begin(), n_grid, n_dim * n_action * n_pos, n_dec - 1, false); const arma::ivec c_dims = control_.attr("dim"); arma::cube control2; arma::imat control; bool full_control; if (c_dims.n_elem == 3) { full_control = false; arma::cube temp_control2(control_.begin(), n_pos, n_action, n_pos, false); control2 = temp_control2; } else { full_control = true; arma::mat temp_control(control_.begin(), n_pos, n_action, false); control = arma::conv_to<arma::imat>::from(temp_control); } const std::size_t n_disturb = disturb.n_slices; // Bellman recursion arma::cube value(n_grid, n_dim * n_pos, n_dec); arma::cube cont(n_grid, n_dim * n_pos, n_dec - 1, arma::fill::zeros); arma::mat d_value(n_grid, n_dim); Rcpp::Rcout << "At dec: " << n_dec - 1 << "..."; for (std::size_t pp = 0; pp < n_pos; pp++) { value.slice(n_dec - 1).cols(n_dim * pp, n_dim * (pp + 1) - 1) = scrap.slice(pp); } for (int tt = (n_dec - 2); tt >= 0; tt--) { Rcpp::Rcout << tt; // Approximating the continuation value for (std::size_t pp = 0; pp < n_pos; pp++) { cont.slice(tt).cols(n_dim * pp, n_dim * (pp + 1) - 1) = Expected(grid, value.slice(tt + 1).cols(pp * n_dim, n_dim * (pp + 1) - 1), disturb, weight); } Rcpp::Rcout << ".."; // Optimise value function if (full_control) { BellmanOptimal(grid, control, value, reward, cont, tt); } else { BellmanOptimal2(grid, control2, value, reward, cont, tt); } Rcpp::Rcout << "."; } return Rcpp::List::create(Rcpp::Named("value") = value, Rcpp::Named("expected") = cont); }
char GetName(){ char GetName; if (!IsAlpha(Look[0])) Expected("Name"); GetName = Look[0]; GetChar(); return GetName; }
char GetNum (){ char GetNum; if (!IsDigit(Look[0])) Expected("Integer"); GetNum = Look[0]; GetChar(); return GetNum; }
//match a specific input character void Match(char c){ if(Look == c) GetChar(); else { char buf[MAX_STR_LEN]; snprintf(buf, sizeof(buf), "\"%c\"", c); Expected(buf); } }
int GetBoolean() { if (!IsBoolean(Look)) { Expected("Boolean Literal"); } int ret = uppercase(Look) == 'T'; GetChar(); return ret; }
char GetNum(void) { char num; if (!IsDigit(Look)) { Expected("Integer"); } num = Look; GetChar(); }
void Match(char x) { if(Look == x) { GetChar(); } else { sprintf(tmp, "' %c ' ", x); Expected(tmp); } }
void Match(char x) { if(Look != x) { sprintf(tmp, "' %c '", x); Expected(tmp); } else { GetChar(); SkipWhite(); } }
void Match(const char tok) { char msg[MAXMSG]; if (Look == tok) { GetChar(); } else { snprintf(msg, MAXMSG, "\"%c\"", tok); Expected(msg); } }
/* Get a number */ char GetNum(void) { if (!IsDigit(Look)) { Expected("Integer"); } char num = Look; GetChar(); SkipWhite(); return num; }
/* Get an identifier */ char GetName(void) { if (! IsAlpha(Look)) { Expected("Name"); } char name = upcase(Look); GetChar(); SkipWhite(); return name; }
char GetName(void) { char name; if (!IsAlpha(Look)) { Expected("Name"); } name = toupper(Look); GetChar(); return name; }
// // Parse any 'include "filename"; ' statements // Statement * Parser::ParseInclude() { char fileName[MAX_STRING_LEN]; // 'include' if(!Expected(TOK_INCLUDE)) return 0; // "filename" strcpy(fileName, tstr); if(!Expected(TOK_STRINGBUF)) return 0; // terminating semi-colon if(t == ';') { // initialize the lexical-analyser with this new file if(file_included(fileName) == false) { Parser p(this); p.SetErrorStream(fperr); if(!p.Ooof(fileName)) { errcount += p.errcount; lasterr = p.lasterr; strcpy(errstr, p.errstr); return 0; } } Expected(';'); } else { Expected(';'); return 0; } // start parsing! //t = gettok(); return new Statement(_strdup(fileName)); }
/* match a specific input character */ void Match(char c) { if (Look == c) { GetChar(); } else { char tmp_buf[MAX_BUF]; sprintf(tmp_buf, "'%c'", c); Expected(tmp_buf); } SkipWhite(); }
int GetNum (){ int x=0; if (!IsDigit(Look[0])) Expected("Integer"); while (IsDigit(Look[0])){ x = x*10 + Look[0]-'0'; GetChar(); } SkipWhite(); return x; }
// Match a Specific Input Character void Match( char x ) { char x1[2]; x1[0]=x; x1[1]='\0'; if (Look != x) Expected(x1); GetChar(); }
char GetName() { char c = Look; if (!isalpha(Look)) { sprintf(tmp, "Name"); Expected(tmp); } Getchar(); return toupper(c); }
char GetNum() { char c = Look; if (!isdigit(Look)) { sprintf(tmp, "Integer"); Expected(tmp); } GetChar(); return c; }
// Match a Specific Input Character int Match( char x ) { char x1[2]; x1[0]=x; x1[1]='\0'; if (Look != x) Expected(x1); GetChar(); SkipWhite(); }
void Match(char x){ if (Look[0] == x) GetChar(); else { char error[4]; error[0]='\''; error[0]=x; error[0]='\''; error[0]='\0'; Expected(error); } }
char GetName() { char c = Look; if( !IsAlpha(Look)) { sprintf(tmp, "Name"); Expected(tmp); } GetChar(); return UPCASE(c); }
void Term (){ Factor(); EmitLn("MOVE D0,-(SP)"); while (Look[0] == '*' || Look[0] == '/'){ switch (Look[0]){ case '*': Multiply(); break; case '/': Divide(); break; default: Expected("Mulop"); } } }
bool Resource::Match(char c) { bool r = Is(c); if (!r) { Expected(c); } Lex(); return r; }
void Alloc() { char name[MAX_BUF]; Next(); if (Token != 'x') { Expected("Variable Name"); } CheckDup(Value); sprintf(name, Value); AddEntry(name, 'v'); Next(); if (Token == '=') { Next(); if (Token != '#') { Expected("Integer"); } Allocate(name, Value); Next(); } else { Allocate(name, "0"); } }
void Parser::ParseDeclaration() { SymType* type = ParseTypeSpecifier(); Symbol* symbol = NULL; if (*lexer.Peek() == SEMICOLON) { lexer.Get(); return; } else { symbol = ParseDeclarator(type); } while (true) { if (!parseFunc.empty()) { symStack.Add(symbol, 0); } else { symStack.Add(symbol); } if (*lexer.Peek() == ASSIGN) { BaseToken* oper = lexer.Get(); SyntaxNode* left = new NodeVar(counter++, symbol); symStack.SetUsed(left->token->GetText()); SyntaxNode* right = ParseExpression(precedences[COMMA]+1); NodeBinaryOp* node = new NodeBinaryOp(counter++, left, oper, right); stmtStack.push_back(new StmtExpr(node)); node->GetType(); } if (*lexer.Peek() == SEMICOLON || *lexer.Peek() == FIGURE_RIGHT_BRACKET) { lexer.Get(); break; } Expected(lexer.Peek()->GetSubType(), COMMA); lexer.Get(); symbol = ParseDeclarator(type); } }