// // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // cntparens.cfa -- match left/right parenthesis // // Author : Peter A. Buhr // Created On : Sat Apr 20 11:04:45 2019 // Last Modified By : Peter A. Buhr // Last Modified On : Sat Apr 20 11:06:21 2019 // Update Count : 1 // #include #include enum Status { Cont, Match, Error }; coroutine CntParens { char ch; // used for communication Status status; unsigned int cnt; }; // CntParens void main( CntParens & cpns ) with( cpns ) { for ( ; ch == '('; cnt += 1 ) { // left parenthesis suspend; } for ( ; ch == ')' && cnt > 1; cnt -= 1 ) { // right parenthesis suspend; } status = ch == ')' ? Match : Error; } // main void ?{}( CntParens & cpns ) with( cpns ) { status = Cont; cnt = 0; } Status next( CntParens & cpns, char c ) with( cpns ) { ch = c; resume( cpns ); return status; } int main() { CntParens cpns; char ch; for () { // read until end of file sin | ch; // read one character if ( eof( sin ) ) { sout | "Error"; break; } // eof ? Status ret = next( cpns, ch ); // push character for formatting if ( ret == Match ) { sout | "Match"; break; } if ( ret == Error ) { sout | "Error"; break; } } // for } // main // Local Variables: // // tab-width: 4 // // compile-command: "cfa -g -Wall -Wextra cntparens.cfa" // // End: //