diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | examples/example.imp | 12 | ||||
-rw-r--r-- | examples/factorial.imp | 1 | ||||
-rw-r--r-- | src/lexer.l | 83 |
4 files changed, 51 insertions, 47 deletions
@@ -4,7 +4,7 @@ A small interpreter of the IMP programming language, written in *C*, with *flex* Currently, the core functionality of IMP is implemented, additionally, the *local variable extension*, and the *procedure extension* are also implemented. -There are some syntactic enhancements, such as the boolean constants `true` and `false`, omitting parenthesis around expressions and sequential composition, omitting the else block of an if statment, and the ability to add arbitrary many semicolons after each statement. +There are some syntactic enhancements, such as the boolean constants `true` and `false`, omitting parenthesis around expressions and sequential composition, omitting the else block of an if statment, support for C-style comments of the form `/* ... */`, and the ability to add arbitrary many semicolons after each statement. ## Dependencies diff --git a/examples/example.imp b/examples/example.imp index de76b27..c60caa3 100644 --- a/examples/example.imp +++ b/examples/example.imp @@ -1,11 +1,13 @@ -f1 := 0; -f2 := 1; +/* computes the n-th fibonacci number */ + +fib := 0; +fibnext := 1; n := 5; while n # 0 do - var tmp := f2 in - f2 := f1 + f2; - f1 := tmp; + var tmp := fibnext in + fibnext := fib + fibnext; + fib := tmp; end; n := n - 1; end;
\ No newline at end of file diff --git a/examples/factorial.imp b/examples/factorial.imp index 0ab74cd..4cb47ed 100644 --- a/examples/factorial.imp +++ b/examples/factorial.imp @@ -1,3 +1,4 @@ +/* computes n!, result is stored in r. */ procedure factorial(n;r) begin if n <= 0 then r := 1; diff --git a/src/lexer.l b/src/lexer.l index 285cbf7..8a8c5b7 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -10,46 +10,47 @@ WHITESPACE [ \t\r\n]+ %% -"skip" { return T_SKIP; } -"if" { return T_IF; } -"then" { return T_THEN; } -"else" { return T_ELSE; } -"end" { return T_END; } -"while" { return T_WHILE; } -"do" { return T_DO; } -"var" { return T_VAR; } -"in" { return T_IN; } -"procedure" { return T_PROC; } -"begin" { return T_BEGIN; } - -"(" { return T_LPAREN; } -")" { return T_RPAREN; } -";" { return T_SEM; } -"," { return T_COM; } -":=" { return T_ASSIGN; } - -"+" { return T_PLUS; } -"-" { return T_MINUS; } -"*" { return T_STAR; } - -"or" { return T_OR; } -"and" { return T_AND; } -"not" { return T_NOT; } - -"=" { return T_EQ; } -"#" { return T_NE; } -"<=" { return T_LE; } -"<" { return T_LT; } -">=" { return T_GE; } -">" { return T_GT; } - -"true" { return T_TRUE; } -"false" { return T_FALSE; } - -{DIGIT}+ { yylval.num = atoi(yytext); return T_NUM; } -{IDENT} { yylval.id = strdup(yytext); return T_ID; } - -{WHITESPACE} { } -. { fprintf(stderr, "Unknown char: %s\n", yytext); } +\/\*([^*]|\*+[^*/])*\*+\/ { /* ignore comments */ } +"skip" { return T_SKIP; } +"if" { return T_IF; } +"then" { return T_THEN; } +"else" { return T_ELSE; } +"end" { return T_END; } +"while" { return T_WHILE; } +"do" { return T_DO; } +"var" { return T_VAR; } +"in" { return T_IN; } +"procedure" { return T_PROC; } +"begin" { return T_BEGIN; } + +"(" { return T_LPAREN; } +")" { return T_RPAREN; } +";" { return T_SEM; } +"," { return T_COM; } +":=" { return T_ASSIGN; } + +"+" { return T_PLUS; } +"-" { return T_MINUS; } +"*" { return T_STAR; } + +"or" { return T_OR; } +"and" { return T_AND; } +"not" { return T_NOT; } + +"=" { return T_EQ; } +"#" { return T_NE; } +"<=" { return T_LE; } +"<" { return T_LT; } +">=" { return T_GE; } +">" { return T_GT; } + +"true" { return T_TRUE; } +"false" { return T_FALSE; } + +{DIGIT}+ { yylval.num = atoi(yytext); return T_NUM; } +{IDENT} { yylval.id = strdup(yytext); return T_ID; } + +{WHITESPACE} { /* ignore whitespace */ } +. { fprintf(stderr, "Unknown char: %s\n", yytext); } %% |