OurC Project 2
Proj. 2 is the first part of OurC project (there are three parts). For Proj. 2, you are to implement a syntax checker
and a pretty printer that supports system-supported functions.
The system-supported functions of OurC system are listed below.
ListAllVariables(); // just the names of the (global) variables,
// sorted (from smallest to greatest)
ListAllFunctions(); // just the names of the (user-defined)
// functions, sorted
ListVariable(char name[]); // the definition of a particular variable
ListFunction(char name[]); // the definition of a particular function
Done(); // exit the interpreter
error message , error messages。:
Line 3 : unrecognized token with first char : '$' // lexical error
Line 2 : unexpected token : '*' // syntactical error (token recognized)
Line 5 : undefined identifier : 'bcd' // semantic error (grammar ok)
( error detection ):
get token, the first non-white-space char。
the first non-white-space char token first char, lexical level (scanner level)
error, unrecognized-token-message。
char token first char、 go for the longest match (and get that token)。
(syntactical level) error, unexpected-token-message。
error、 token identifier, undeclared identifier (if this identifier should
have been declared at this point), undeclared identifier (a semantic error), undefined-identifier-
message。
There is a total of 14 "test problems" for Proj. 2.
Each test problem contains several tests. The first few tests are "viewable", that is, you will be able to see what the
test datas are. The latter tests, however, are such that you will not see what the test data is.
Odd-numbered test problems are designed in such a way that the latter tests are more or less "isomorphic" to the
"structures" of the first few tests. Even-numberd test problems are such that their latter tests are slightly more
involved versions of their first few tests.
Though you can do the problems in any order, it is suggested that you do the problems by sequence. That is, you
do Problem No. 1, followed by Problem No. 2, followed by Problem No. 3, etc. Another alternative is to do the
odd-numbered problems first. Then, proceed to solve the even-numbered problems. As a general guideline, you
should always try the lower-numbered problems before you try the higher-numbered problems.
Below, we use example I/O to give you a "feel" of what your Proj. 2 should do.
OurC Project 2
// ============================================================
>>2
Done();
>Our-C running ...
> Our-C exited ...>2
int x ;
x=10;
cout >Our-C running ...
> Definition of x entered ...
> Statement executed ...
> Statement executed ...
> Our-C exited ...>3
string str ;
str = "This is a fine day.\n" ;
float y ;
y = 20 ;
Done();
>Our-C running ...
> Definition of str entered ...
> Statement executed ...
> Definition of y entered ...
> Statement executed ...
> Our-C exited ...>2
string str ;
str = "This is a fine day.\n" ;
str = str + "Isn't it?\n" ;
cout >Our-C running ...
> Definition of str entered ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Definition of y entered ...
> Definition of x entered ...
> Statement executed ...
> Statement executed ...
> Our-C exited ...>7
string str ;
char chArray[30] ;
cin >> chArray ;
str = "This is a fine day.\n" + chArray + "\n" ;
str = str + "Isn't it?\n" ;
cout >Our-C running ...
> Definition of str entered ...
> Definition of chArray entered ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
OurC Project 2
> Definition of hello entered ...
> Definition of x entered ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Our-C exited ...>2
String str ;
string str;//comment should be skipped
char chArray[30] ;
cin >> chArray1 ; // undeclared identifier
cin >> chArray ;
str = "This is a fine day.\n"
+ chArray
+ "\n"
;
str = str + "Isn't it?\n" ;
cout >Our-C running ...
> Line 1 : undefined identifier : 'String'
> Definition of str entered ...
> Definition of chArray entered ...
> Line 1 : undefined identifier : 'chArray1'
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Line 3 : undefined identifier : 'chArray1'
> Statement executed ...
> Definition of hello entered ...
> Definition of x entered ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Line 3 : undefined identifier : 'hello135'
> Line 3 : unexpected token : ')'
> Statement executed ...
> Our-C exited ...>3
int AddTwo( int x ) { return x + 2 ; } // comment
int AddFive( int x ) { int y ; y = AddTwo( x ) ; // comment
return y + 3 ; } // comment
ListAllFunctions() ;
ListFunction( "AddFive" ) ;
int x ;
x = 100 ;
x = x + AddFive( x ) ;
if ( x > 200 )
x = AddTwo( 300 ) ;
else
x = x + AddFive( 200 ) + 5 ;
if ( AddTwo( x ) > 200 )
OurC Project 2
x = 5 + AddThree( 300 ) ;
else
x = x + AddFive( 200 ) + 5 ;
Done() ;
>Our-C running ...
> Definition of AddTwo() entered ...
> Definition of AddFive() entered ...
> AddFive()
AddTwo()
Statement executed ...
> int AddFive( int x ) {
int y ;
y = AddTwo( x ) ;
return y + 3 ;
}
Statement executed ...
> Definition of x entered ...
> Statement executed ...
> Statement executed ...
> Statement executed ...
> Line 2 : undefined identifier : 'AddThree'
> Line 1 : unexpected token : 'else'
> Statement executed ...
> Our-C exited ...>3
int test ;
char test ; // re-define 'test'
int salary[30] ;
void InputSalary( int revenue[ 30 ] ) {
int i ;
i = 0 ;
while ( i > revenue[ i ]
i++ ;
i=i+1;
} // while ( i > revenue[ i ] ;
i++ ;
} // while ( i > revenue[ i ] ;
i++ ;
} // while ( i >Our-C running ...
> Definition of test entered ...
> New definition of test entered ...
> Definition of salary entered ...
> Line 7 : unexpected token : 'i'
> Line 1 : undefined identifier : 'i'
> Line 1 : unexpected token : '}'
> Line 1 : unexpected token : '}'
> Definition of InputSalary() entered ...
> Definition of Sort() entered ...
> New definition of InputSalary() entered ...
> New definition of InputSalary() entered ...
> Definition of OutputSalary() entered ...
> InputSalary()
OutputSalary()
Sort()
Statement executed ...
> void Sort( int intArray[ 30 ] ) {
int i ;
i = 0 ;
while ( i Statement executed ...
> Statement executed ...
> Statement executed ...
> Our-C exited ...<<
=========================================================================
Something to be aware of ...
Suppose the code user entered looks like this.
if ( ... ) // ... is a valid expression
... // this ... is a valid statement
// ... // this line is a comment
xxx // this line has an error
What is the line number of xxx?
Since the system (your program) has to see xxx before it can make the judgment of whether the IF-THEN-ELSE
has ended or not, the comment line seems to belong to the IF-THEN-ELSE statement.
Anyway, this is a controversial situation (what is the line number of xxx?). Therefore, after detecting the existence
of such code in the hidden test data, I have removed such cases so that the hidden test data no longer contain such
code.