void Node::printTree(eqnForm frm) {
	if (frm == prefix) {printToken();}
	if (left != nullptr) {left->printTree(frm);}
	if (frm == infix) {printToken();}
	if (right != nullptr) {right->printTree(frm);}
	if (frm == postfix) {printToken();}
}
Exemple #2
0
int main(int argc, char *argv[]) {
  std::string input;
  std::list<std::string> words;
  std::string token;
  int num = 0;
  while(true) {
    std::getline(std::cin, input);
    if(input == "0") {
      break;
    }
    for(std::string::iterator it=input.begin(); it != input.end(); it++) {
      if(isalpha(*it)) {
        std::cout << *it;
        if(token.empty()) {
          token = *it;
        } else {
        token += *it;
        }
      } else if(isdigit(*it)){
        num = ((num * 10) + (*it - '0'));
      } else if(isspace(*it)) {
        if(num > 0) {
          printToken(words, num);
          num = 0;
        } else {
          if(!token.empty()) {
            words.push_front(token);
            token.clear();
          }
        }
        std::cout << *it;
      } else {
        if(num > 0) {
          printToken(words, num);
          num = 0;
        }
        if(!token.empty()) {
          words.push_front(token);
          token.clear();
        }
        std::cout << *it;
      }
    }
    if(num != 0) {
      printToken(words, num);
      num = 0;
    }
    if(!token.empty()) {
      words.push_front(token);
      token.clear();
    }
    std::cout << std::endl;
  }

  return 0;
}
Exemple #3
0
/*
  Print the table for debugging purposes.
*/
void printTable(HashTable* table){
  if (table){
    int i;
    for (i=0;i<table->capacity;i++){
      if ((table->entries)[i].car){
	printToken((table->entries)[i].car);
	printToken((table->entries)[i].cdr);	
      }  
    }
  }
}
void processToken(const char *token)
{
    if(strcmp(token, "TRY") == 0)
    {
        ++try_scope;
        printToken("TRY");
    }
    else if(strcmp(token, "try") == 0)
    {
        ++try_scope;
        printToken("try");
        printError("Using try instead of TRY!");
    }
    else if(strcmp(token, "throw") == 0)
    {
        printToken("throw");
        printError("Using throw instead of EXCEPTION macro!");
    }
    else if(strcmp(token, "rethrow") == 0)
    {
        printToken("rethrow");
        printError("Using rethrow instead of RETHROW macro!");
    }
    else if(strcmp(token, "ENDTRY") == 0)
    {
        printToken("ENDTRY");
        --try_scope;
    }
    else if(strcmp(token, "CATCH") == 0)
    {
        printToken("CATCH");
    }
    else if(strcmp(token, "catch") == 0)
    {
        printToken("catch");
        printError("Using catch instead of CATCH or CATCH2 macros!");
    }
    else if(strcmp(token, "CATCH2") == 0)
    {
        printToken("CATCH2");
    }
    else if(strcmp(token, "CATCHALL") == 0)
    {
        printToken("CATCHALL");
    }
    else if(strcmp(token, "return") == 0)
    {
        printToken("return");
        if(try_scope > 0)
            printError("Unwrapped return statement in exception handler!");
    }
}
Exemple #5
0
int main( int argc, char* argv[] ) {
    yyin = stdin;
    yyout = stdout;
    stack<Tag*> tagStack;
    Tag * newTag;
    token_t tokenType;
    while( (tokenType = getToken()) != ENDFILE ) {
        if ( tokenType == OPEN_TAG ) { //open tags get pushed onto the stack
            newTag = new Tag(yylval);
            if ( !tagStack.empty() && tagStack.top()->tagType == IRRELEVANT  ) { //tags wrapped in IRRELEVANT tags are also IRRELEVANT
                newTag->tagType = IRRELEVANT;
            }
            tagStack.push(newTag);
            //DEBUG fprintf(stdout, "OPEN TAG: %s\n", newTag->value.c_str() );
            if ( newTag->tagType != IRRELEVANT ) { //only relevant tags are printed
                printToken(OPEN_TAG, newTag->value);
            }
        } else if ( tokenType == CLOSE_TAG ) { //close tags pop top of stack off and checks if it matches an open tag
            newTag = new Tag(yylval);
            if ( !tagStack.empty() && tagStack.top()->tagType == IRRELEVANT ) { //tags wrapped in IRRELEVANT tags are also IRRELEVANT
                newTag->tagType = IRRELEVANT;
            }
            //DEBUG fprintf(stdout, "CLOSE TAG: %s\n", newTag->value.c_str() );
            if ( newTag->value.compare(tagStack.top()->value) == 0 ) { //close tag matches an open tag
                if ( newTag->tagType != IRRELEVANT ) { //only relevant tags are printed
                    printToken( CLOSE_TAG, newTag->value );
                }
                delete newTag;
                newTag = tagStack.top();
                tagStack.pop();
                delete newTag;
            } else { //close tag doesn't match an open tag; indicating a syntax error
                fprintf( stderr, "ERROR: Invalid Syntax on close tag; Expecting %s, but got %s\n", tagStack.top()->value.c_str(), newTag->value.c_str() );
                delete newTag;
            }
        } else if ( tagStack.top()->tagType != IRRELEVANT ) { //print the token if its not wrapped in an IRRELEVANT tag
            printToken( tokenType, yylval );
        }
    }
    if(!tagStack.empty()) {
        fprintf( stderr, "ERROR: Unmatched tags remain:\n" );
        while(!tagStack.empty()) {
            fprintf( stderr, "%s ", tagStack.top()->value.c_str() );
            newTag = tagStack.top();
            tagStack.pop();
            delete newTag;
        }
        fprintf(stderr, "\n" );
    }
    return 0;
}
Exemple #6
0
TreeNode* statement(void)
{
    TreeNode* t = NULL;

    switch(s_token) {
        case IF:
            t = if_stmt();
            break;
        case REPEAT:
            t = repeat_stmt();
            break;
        case ID:
            t = assign_stmt();
            break;
        case READ:
            t = read_stmt();
            break;
        case WRITE:
            t = write_stmt();
            break;
        default:
            syntaxError("unexpected token -> ");
            printToken(s_token, g_tokenString);
            s_token = getToken();
            break;
    }
    return t;
}
Exemple #7
0
TreeNode * factor(void)
{ TreeNode * t = NULL;
  switch (token) {
    case NUM :
      t = newExpNode(ConstK);
      if ((t!=NULL) && (token==NUM))
        t->attr.val = atoi(tokenString);
      match(NUM);
      break;
    case ID :
      t = newExpNode(IdK);
      if ((t!=NULL) && (token==ID))
        t->attr.name = copyString(tokenString);
      match(ID);
      break;
    case LPAREN :
      match(LPAREN);
      t = expres();
      match(RPAREN);
      break;
    default:
      syntaxError("unexpected token -> ");
      printToken(token,tokenString);
      token = getToken();
      break;
    }
  return t;
}
//lookup uuid and token if we have them and send in for validation
void SkynetClient::processIdentify(char *data, jsmntok_t *tok)
{
	char temp[UUIDSIZE];

    DBGCS("Sending: ");

    DBGC((char)0);
	client->print((char)0);

    DBGC(EMIT);	
	client->print(EMIT);

	printByByteF(IDENTIFY1);
	printToken(data, tok[7]);
	
	if( EEPROM.read( (uint8_t)EEPROMBLOCKADDRESS) == EEPROMBLOCK )
	{
		getUuid(temp);

		printByByteF(IDENTIFY2);
		printByByte(temp);

		getToken(temp);

		printByByteF(IDENTIFY3);
		printByByte(temp);
	}
	printByByteF(CLOSE);
  
	DBGCN((char)255);
	client->print((char)255);
}
Exemple #9
0
static void match(TokenType expected)
{ if (token == expected) token = getToken();
  else {
    syntaxError("unexpected token -> ");
    printToken(token,tokenString);
    fprintf(listing,"      ");
  }
}
Exemple #10
0
void DumpToken(                 // DUMP A TOKEN
    void )
{
    if( PragDbgToggle.dump_tokens ) {
        printf( "Token(%3d) Line(%4d) Column(%3d) ", CurToken, TokenLine, TokenColumn );
        printToken();
    }
}
Exemple #11
0
void DumpMacToken(              // DUMP A MACRO TOKEN
    void )
{
    if( PragDbgToggle.dump_mtokens ) {
        printf( "MacroToken(%3d) Line(%4d) Column(%3d) ", CurToken, TokenLine, TokenColumn );
        printToken();
    }
}
Exemple #12
0
static void match(TOKEN_CLASS expected){
	if (token.type == expected)
		token = getToken();
	else {
		syntax_error("Unexpected Token ->");
		printToken(token);
	}
}
Exemple #13
0
/* procedure printTree prints a syntax tree to the
 * listing file using indentation to indicate subtrees
 */
void printTree( TreeNode *tree )
{
    int i;
    INDENT;
    while (tree != NULL)
    {
        printSpaces();
        if (tree->nodekind == StmtK)
        {
            switch (tree->kind.stmt)
            {
                case IfK:
                    fprintf(listing, "If\n");
                    break;
                case RepeatK:
                    fprintf(listing, "Repeat\n");
                    break;
                case AssignK:
                    fprintf(listing, "Assign to: %s\n", tree->attr.name);
                    break;
                case ReadK:
                    fprintf(listing, "Read: %s\n", tree->attr.name);
                    break;
                case WriteK:
                    fprintf(listing, "Write\n");
                    break;
                default:
                    fprintf(listing, "Unknown ExpNode kind\n");
                    break;
            }
        }
        else if (tree->nodekind==ExpK)
        {
            switch (tree->kind.exp)
            {
                case OpK:
                    fprintf(listing, "Op: ");
                    printToken(tree->attr.op, "\0");
                    break;
                case ConstK:
                    fprintf(listing, "Const: %d\n",tree->attr.val);
                    break;
                case IdK:
                    fprintf(listing, "Id: %s\n",tree->attr.name);
                    break;
                default:
                    fprintf(listing, "Unknown ExpNode kind\n");
                    break;
            }
        }
        else
            fprintf(listing, "Unknown node kind\n");
        for (i=0; i<MAXCHILDREN; i++)
            printTree(tree->child[i]);
        tree = tree->sibling;
    }
    UNINDENT;
}
Exemple #14
0
main( int argc, char * argv[] ) { 
  yyin = stdin;
  yyout = stdout;
  TokenType ttype;
  LoadStopwords("stopwords.txt");
  while( (ttype = getToken()) != ENDFILE )
    printToken( yyout, ttype, yylval );
  return 0;
}
Exemple #15
0
/*
 * prints a list of all tokens in the token list
 */
void printAllTokens(TokenList *current)
{
  fprintf(stdout, "Category                  Text  Lineno        Filename              Ival/Sval\n");
  fprintf(stdout, "-----------------------------------------------------------------------------\n");
  while(current != NULL){
    printToken(current->t);
    current = current->next;
  }
}
int main (int argc, char **argv)
  {
  int tok = 0;
  init(stdin);
  while (!feof(stdin))
    {
    tok = getToken(stdin);
    printToken(tok);
    }
  }
Exemple #17
0
static void unexpectedTokenHandling (void)
{ if (token == COMMENT)
  { token = getToken();
    return ;
  }
  syntaxError ("unexpected token -> ");
  printToken (token, tokenString);
  fprintf (listing, "\n");
  token = getToken();
}
Exemple #18
0
long int evalTree(tree *t,bool debug) {
	if (t->action->type==INTEGER) return getValue(t->action);
	if (t->action->type==VARIABLE) return getValue(t->action);
	if (t->action->type==EOS) {
		if (debug) printf("Statement result: %ld\n",evalTree(t->left,debug));
		else evalTree(t->left,debug);
		return evalTree(t->right,debug);
	}
	if (t->action->type==SET) {
		token * var=t->left->action;
		long int newval= evalTree(t->right,debug);
		if (debug) printf("Set %s=%ld\n",var->name,newval);
		setValue(var,newval);
		return newval;
	}
	if (t->action->type==WHILE) {
		tree * cond = t->left;
		tree * body = t->right;
		long int cval,bval=0;
		cval=evalTree(cond,debug);
		while(cval>0) {
			bval=evalTree(body,debug);
			if (debug) printf("While %ld : %ld\n",cval,bval);
			cval=evalTree(cond,debug);
			/* cval = bval; */
		}
		if (debug) printf("While 0 ... final value is %ld\n",bval);
		return bval;
	}
	long int left=evalTree(t->left,debug);
	long int right=evalTree(t->right,debug);
	switch (t->action->type) {
		case PLUS:
			if (debug) printf("%ld + %ld = %ld\n",left,right,left+right);
			return left+right;

		case MINUS:
			if (debug) printf("%ld - %ld = %ld\n",left,right,left-right);
			return left-right;

		case MULTIPLY:
			if (debug) printf("%ld * %ld = %ld\n",left,right,left*right);
			return left*right;

		case DIVIDE:
			if (debug) printf("%ld / %ld = %ld\n",left,right,left/right);
			return left/right;

		default:
			printf("Unrecognized action for subtree Token: ");
			printToken(t->action);
	}
	return 0;
}
Exemple #19
0
void decrementScope(void)
{
    --scope_count;
    printToken("}");
    if(scope_count == 0)
    {
        if(try_scope > 0)
            printError("End of function reached without seeing enough ENDTRY macros!");
        try_scope = 0;
        if(verboseflag)printf("\n");
    }
}
Exemple #20
0
void CqASCII::printArray ( RtInt n, RtToken *p )
{
	OUT << "[ ";
	for ( RtInt i = 0; i < n; i++ )
	{
		printToken(p[ i ]);
		OUT << ' ';
		//if ( (i+1) % maxLineLength == 0 )
			//printEOL();
	}
	OUT << ']';
}
Exemple #21
0
int main( int argc, char * argv[] )
{ TreeNode * syntaxTree;
  char pgm[120]; /* source code file name */
  hash_new(&h);
  if (argc < 2)
    { fprintf(stderr,"usage: %s <filename>\n",argv[0]);
      exit(1);
    }
  if (argc == 3)
  {
    if(strcmp(argv[2], "-a") == 0)
    {
      TraceScan = FALSE;
      TraceAnalyze = FALSE;	
    }
    else if(strcmp(argv[2], "-s") == 0)
    {
      TraceScan = FALSE;
      TraceParse = FALSE;
    }
  }
  strcpy(pgm,argv[1]) ;
  if (strchr (pgm, '.') == NULL)
     strcat(pgm,".cm");
  source = fopen(pgm,"r");
  if (source==NULL)
  { fprintf(stderr,"File %s not found\n",pgm);
    exit(1);
  }
  listing = stdout; /* send listing to screen */
  fprintf(listing,"\nC Minus compilation: %s\n",pgm);
#if NO_PARSE
  //if(a_flag == 0)
  while( (ttype=getToken())!= 0 )
    printToken( ttype, tokenString );
#else
  syntaxTree = parse();
  if (TraceParse) {
    fprintf(listing,"\nSyntax tree:\n");
    printTree(syntaxTree);
  }

  if(TraceAnalyze) {
    fprintf(listing, "\nSymbol Table:\n");
    print_hash(h);
  }
  list_kill(l);
  hash_kill(&h);
#endif
  fclose(source);
  return 0;
}
int
yylex(void)
{
	int	result;

	result = lexer();
	if (yydebug) {
		printf("lexer returns ");
		printToken(result);
		printf(", value=%d (0x%x)\n", yylval, yylval);
	}
	return(result);
}
Exemple #23
0
static inline BOOL match(TokenType expected)
{
	if (TEST(expected)) {
		getsym();
		return TRUE;
	} else {
		fprintf(tiplist, "********************************\n");
		fprintf(tiplist, "missing match at line %d\n", lineno);
		printToken(expected, prevTokenString);
		fprintf(tiplist, "********************************\n");
		return FALSE;
	}
}
Exemple #24
0
static void match(TokenType expected)
{
    if (s_token == expected)
    {
        s_token = getToken();
    }
    else
    {
        syntaxError("unexpected token -> ");
        printToken(s_token, g_tokenString);
        print("        ");
    }
}
Exemple #25
0
void printTree(tree * t,char * prefix) {
	char newPrefix[100];
	printf("%-15s: ",prefix);
	printToken(t->action);
	if (t->action->type==INTEGER) return;
	strcpy(newPrefix,prefix);
	strcat(newPrefix,".L");
	if (t->left==NULL) printf("%s NULL\n",newPrefix);
	else printTree(t->left,newPrefix);
	strcpy(newPrefix,prefix);
	strcat(newPrefix,".R");
	if (t->right==NULL) printf("%s NULL\n",newPrefix);
	else printTree(t->right,newPrefix);
}
Exemple #26
0
TreeNode * statement(void)
{ TreeNode * t = NULL;
  switch (token) {
    case IF : t = if_stmt(); break;
    case REPEAT : t = repeat_stmt(); break;
    case FOR : t = for_stmt(); break; //ADICIONADO O FOR COMO UMA DECLARAÇÃO
    case ID : t = assign_stmt(); break;
    case READ : t = read_stmt(); break;
    case WRITE : t = write_stmt(); break;
    default : syntaxError("unexpected token -> ");
              printToken(token,tokenString);
              token = getToken();
              break;
  } /* end case */
  return t;
}
//Credentials have been invalidted, send blank identify for new ones
void SkynetClient::processNotReady(char *data, jsmntok_t *tok)
{
    DBGCS("Sending: ");

    DBGC((char)0);
	client->print((char)0);

    DBGC(EMIT);	
	client->print(EMIT);

	printByByteF(IDENTIFY1);
	printToken(data, tok[11]);
	printByByteF(CLOSE);
  
	DBGCN((char)255);
	client->print((char)255);
}
Exemple #28
0
int main(int argc, char * argv[]) { 
  TokenType ttype;
  source = stdin;
  listing = stdout;

  struct stack_t *scannerStack = newStack(); //track tags

  int relevant = 0; //track if current tag section is relevant to print -- 0 is relevant, > 0 is irrelevant, < 0 is no tags

  while((ttype=getToken()) != ENDFILE) {
    /* if open tag, push onto stack */
    if(ttype == 1) {
      push(scannerStack, formatToken(ttype, tokenString));
      if(relevant > 0) relevant++; //increment relevant flag for any tag inside irrelevant tag
      else if(!compareToken(top(scannerStack))) relevant++; //increment relevant flag on irrelevant tag
    }
    /* if close tag, check top of stack for matching open tag */
    if(ttype == 2) {
      /* if close tag does not match top of stack, report error */
      if(top(scannerStack) == NULL || strcmp(top(scannerStack), formatToken(ttype, tokenString))) {
        if(relevant == 0) printf("-- Error: CLOSE-TAG </%s> with no matching open tag at top of stack at line %d\n", formatToken(ttype, tokenString), lineno);
        continue;
      }
      /* if close tag matches top of stack, pop */
      else {
        pop(scannerStack);
        /* decrement relevant tag on pop if greater than 0 */
        if(relevant > 0) {
          relevant--;
          continue;
        }
      }
    }
    /* print tokens if relevant and tag is on stack */
    if(relevant == 0 && top(scannerStack) != NULL) printToken(ttype, tokenString);
  }

  /* print off remaining tags in stack as errors */
  while(top(scannerStack)) {
    printf("-- Error: OPEN-TAG <%s> with no closing tag left over in stack\n", top(scannerStack));
    pop(scannerStack);
  }

  destroyStack(&scannerStack);
  return 0;
}
Exemple #29
0
int scan(char *fileName) {
  Token *token;

  if (openInputStream(fileName) == IO_ERROR)
    return IO_ERROR;

  token = getToken();
  while (token->tokenType != TK_EOF) {
    printToken(token);
    free(token);
    token = getToken();
  }

  free(token);
  closeInputStream();
  return IO_SUCCESS;
}
Exemple #30
0
TreeNode * statement(void) //statement  -> expression| if_stmt| while_stmt|for_stmt|var_stmt|assign_stmt
{ TreeNode * t = NULL;
  switch (token) {
    case IF : t = if_stmt(); break;
    case FOR : t = for_stmt(); break;
    case ID : t = assign_stmt(); break;
    case WHILE : t = while_stmt(); break;
    case INT :
	case CHAR :
		t = var_stmt(); break;
	case RBRACE : match(RBRACE); break;
    default : syntaxError("unexpected token -> ");
              printToken(token,tokenString);
              token = getToken();
              break;
  } /* end case */
  return t;
}