// // 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 : Thu Aug 15 20:39:34 2024 // Update Count : 2 // #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; try { for () { // read until end of file sin | ch; // read one character Status ret = next( cpns, ch ); // push character for formatting if ( ret == Match ) { sout | "Match"; break; } if ( ret == Error ) { sout | "Error"; break; } } // for } catch( end_of_file * ) { sout | "Error"; } // try } // main // Local Variables: // // tab-width: 4 // // compile-command: "cfa -g -Wall -Wextra cntparens.cfa" // // End: //