summaryrefslogtreecommitdiff
path: root/test1
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-01-01 19:31:12 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2017-01-01 19:31:12 +0100
commit69fe7b182a1eedfb75c611f7dd35fa60200426f4 (patch)
tree329a0c6cc9b06c23d8782ece09f0f7dfa9b16b13 /test1
downloadcompilertests-69fe7b182a1eedfb75c611f7dd35fa60200426f4.tar.gz
compilertests-69fe7b182a1eedfb75c611f7dd35fa60200426f4.tar.bz2
initial checkin
Diffstat (limited to 'test1')
-rw-r--r--test1/Makefile10
-rw-r--r--test1/parse.c57
2 files changed, 67 insertions, 0 deletions
diff --git a/test1/Makefile b/test1/Makefile
new file mode 100644
index 0000000..d9854f1
--- /dev/null
+++ b/test1/Makefile
@@ -0,0 +1,10 @@
+CFLAGS=-std=c99 -Wall -Werror
+
+all: parse
+
+test: parse
+ printf "9-5+2" | ./parse
+ printf "9-5+2+" | ./parse
+
+clean:
+ -rm -f parse
diff --git a/test1/parse.c b/test1/parse.c
new file mode 100644
index 0000000..1b92490
--- /dev/null
+++ b/test1/parse.c
@@ -0,0 +1,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 );
+}