ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change
on this file since a254fa56 was
8f194ee,
checked in by Peter A. Buhr <pabuhr@…>, 6 years ago
|
add coroutine to parse balance parenthesis
|
-
Property mode set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[8f194ee] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // cntparens.cfa -- match left/right parenthesis |
---|
| 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Sat Apr 20 11:04:45 2019 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sat Apr 20 11:06:21 2019 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include <fstream.hfa> |
---|
| 17 | #include <coroutine.hfa> |
---|
| 18 | |
---|
| 19 | enum Status { Cont, Match, Error }; |
---|
| 20 | coroutine CntParens { |
---|
| 21 | char ch; // used for communication |
---|
| 22 | Status status; |
---|
| 23 | unsigned int cnt; |
---|
| 24 | }; // CntParens |
---|
| 25 | |
---|
| 26 | void main( CntParens & cpns ) with( cpns ) { |
---|
| 27 | for ( ; ch == '('; cnt += 1 ) { // left parenthesis |
---|
| 28 | suspend(); |
---|
| 29 | } |
---|
| 30 | for ( ; ch == ')' && cnt > 1; cnt -= 1 ) { // right parenthesis |
---|
| 31 | suspend(); |
---|
| 32 | } |
---|
| 33 | status = ch == ')' ? Match : Error; |
---|
| 34 | } // main |
---|
| 35 | |
---|
| 36 | void ?{}( CntParens & cpns ) with( cpns ) { status = Cont; cnt = 0; } |
---|
| 37 | |
---|
| 38 | Status next( CntParens & cpns, char c ) with( cpns ) { |
---|
| 39 | ch = c; |
---|
| 40 | resume( cpns ); |
---|
| 41 | return status; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | int main() { |
---|
| 45 | CntParens cpns; |
---|
| 46 | char ch; |
---|
| 47 | |
---|
| 48 | for () { // read until end of file |
---|
| 49 | sin | ch; // read one character |
---|
| 50 | if ( eof( sin ) ) { sout | "Error"; break; } // eof ? |
---|
| 51 | Status ret = next( cpns, ch ); // push character for formatting |
---|
| 52 | if ( ret == Match ) { sout | "Match"; break; } |
---|
| 53 | if ( ret == Error ) { sout | "Error"; break; } |
---|
| 54 | } // for |
---|
| 55 | } // main |
---|
| 56 | |
---|
| 57 | // Local Variables: // |
---|
| 58 | // tab-width: 4 // |
---|
| 59 | // compile-command: "cfa -g -Wall -Wextra cntparens.cfa" // |
---|
| 60 | // End: // |
---|
Note: See
TracBrowser
for help on using the repository browser.