summaryrefslogtreecommitdiff
path: root/test1/parse.c
blob: 1b9249045e70ade25af48d395512d1ddd2c016be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int c;

void match( char t )
{
	if( c == t ) {
		c = getc( stdin );
	} else {
		fprintf( stderr, "\nERR: expecting '%c', but got '%c'\n", t, c );
		exit( 1 );
	}
}

void term( void )
{
	if( isdigit( c ) ) {
		int t = c;
		match( c );
		putchar( t );
	} else {
		fprintf( stderr, "\nERR: expecting digit, but got '%c'\n", c );
		exit( 1 );
	}
}

void rest( void )
{
	if( c == '+' ) {
		match( '+' ); term( ); putchar( '+' ); rest( );
	} else if( c == '-' ) {
		match( '-' ); term( ); putchar( '-' ); rest( );
	}
}

void expr( void )
{
	term( ); rest( );
}

int main( void )
{
	c = getc( stdin );

	expr( );

	if( ( c = getc( stdin ) ) != EOF ) {
		fprintf( stderr, "\nERR: superfluos input found\n" );
		exit( 1 );
	}

	puts( "" );

	exit( 0 );
}