Esempio n. 1
0
int compile()
{
	int i;
	printf("start compilation\n");
	initSource();				/* getSourceの初期設定 */
	token = nextToken();			/* 最初のトークン */
	blockBegin(FIRSTADDR);		/* これ以後の宣言は新しいブロックのもの */
	block(0);					/* 0 はダミー(主ブロックの関数名はない) */
	finalSource();
	i = errorN();				/* エラーメッセージの個数 */
	if (i!=0)
		printf("%d errors\n", i);
/*	listCode();	*/			/* 目的コードのリスト(必要なら) */
	return i<MINERROR;		/* エラーメッセージの個数が少ないかどうかの判定 */
}
Esempio n. 2
0
void funcDecl()			/* 関数宣言のコンパイル */
{
	int fIndex;
	if (token.kind==Id){
		setIdKind(funcId);				/* 印字のための情報のセット */
		fIndex = enterTfunc(token.u.id, nextCode());		/* 関数名をテーブルに登録 */
				/* その先頭番地は、まず、次のコードの番地nextCode()とする */
		token = checkGet(nextToken(), Lparen);
		blockBegin(FIRSTADDR);	/* パラメタ名のレベルは関数のブロックと同じ */
		while(1){
			if (token.kind==Id){			/* パラメタ名がある場合 */
				setIdKind(parId);		/* 印字のための情報のセット */
				enterTpar(token.u.id);		/* パラメタ名をテーブルに登録 */
				token = nextToken();
			}else
				break;
			if (token.kind!=Comma){		/* 次がコンマならパラメタ名が続く */
				if (token.kind==Id){		/* 次が名前ならコンマを忘れたことに */
					errorInsert(Comma);
					continue;
				}else
					break;
			}
			token = nextToken();
		}
		token = checkGet(token, Rparen);		/* 最後は")"のはず */
		endpar();				/* パラメタ部が終わったことをテーブルに連絡 */
		if (token.kind==Semicolon){
			errorDelete();
			token = nextToken();
		}
		block(fIndex);	/* ブロックのコンパイル、その関数名のインデックスを渡す */
		token = checkGet(token, Semicolon);		/* 最後は";"のはず */
	} else 
		errorMissingId();			/* 関数名がない */
}
Esempio n. 3
0
int ConfParser::parse_rec( ConfBlock* currBlock,
                           int level,
                           int nLine,
                           ifstream& configFile,
                           const string& configFileName ) {

  // Read in config file linewise
  string l;
  while ( getline( configFile, l ) ) {

    ++nLine;

    // Remove leading and trailing whitespace
    trim( l );
    // If the line contained only whitspace we can skip it
    if ( l.empty() ) continue;

    // Ignore lines beginning with # or // as comments
    if ( starts_with( l, "#") || starts_with( l, "//" ) ) continue;

    // Check whether it is the end of a block
    if ( l.length() == 1 && l[0] == '}' ) {

      // Syntax error if we are at level 0
      if ( level == 0 ) {
        throw BadSyntax( configFileName, nLine,
            "Found closing block at outermost level" );
      }

      return nLine;
    }

    cmatch m;
    regex blockBegin( "^(\\w+)\\s*\\{$" );
    regex keyVal( "^(\\w+)\\s+(.*);" );

    // Check whether it is the beginning of a new block
    if ( regex_match( l.c_str(), m, blockBegin ) ) {

#ifdef DEBUG
        cout << "Adding Block " << m[1] << " at level " << level << " (line " << nLine << ")" << endl;
#endif

      nLine = parse_rec( &currBlock->addChild( m[1] ), level + 1, nLine, configFile, configFileName );

    // Check whether it is a key / value pair
    } else if ( regex_match( l.c_str(), m, keyVal ) ) {

      currBlock->addParam( m[1], m[2] );

    // Else we have a malformed expression and throw an exception
    } else {

      throw BadSyntax( configFileName, nLine, "Malformed expression" );

    }

  }

  // check if we are at outermost level again at the end
  if ( level != 0 )
    throw BadSyntax( configFileName, nLine, "Unexpected end of configuration file" );

  return nLine;
}