From 5a90102b20d18966b69496d85361d14c085479db Mon Sep 17 00:00:00 2001 From: Flavian Kaufmann Date: Wed, 21 May 2025 07:45:30 +0200 Subject: syntax doc --- README.md | 44 +++++++++++++++++++++++++++++++++++++++++++- res/syntax.ebnf | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/driver.c | 2 +- src/repl.c | 14 +++++++------- 4 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 res/syntax.ebnf diff --git a/README.md b/README.md index dc39ac2..a86f2df 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,46 @@ A small interpreter of the IMP programming language. - [flex](https://github.com/westes/flex) - [bison](https://www.gnu.org/software/bison) -- [readline](https://tiswww.case.edu/php/chet/readline/rltop.html) \ No newline at end of file +- [readline](https://tiswww.case.edu/php/chet/readline/rltop.html) + +## IMP + +[Syntax](/res/syntax.ebnf) + +**Statement ** +Variable assignment: +- ` := ` any variable not assigned, has the value 0. + +Local variable: +- `var := in end` + +Control flow: +- `if then else end` +- `while do end` +- `(; )` sequential composition, the first statement runs before the second +- `skip`, nop + +**Expression** +Arithmetic Expression : +- `` +- `` +- `( + ) +- `( - ) +- `( + ) + +Boolean Expression : +- `not +- `( or ) +- `( and ) +- ` = ` +- ` # ` not equals +- ` < ` +- ` <= ` +- ` > ` +- ` >= ` + +**Variable ** +- `[a-zA-Z][A-Za-z0-9]*` + +**Numeral ** +- `[0-9]+` diff --git a/res/syntax.ebnf b/res/syntax.ebnf new file mode 100644 index 0000000..85990c5 --- /dev/null +++ b/res/syntax.ebnf @@ -0,0 +1,47 @@ +letter_uppercase = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" + | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" + ; + +letter_lowercase = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" + | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" + ; + +letter = letter_uppercase | letter_lowercase + ; + +digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" + ; + +identifier = letter , { letter | digit } + ; + +numeral = digit | ( numeral , digit ) + ; + +variable = identifier + ; + +arithmetic_operation = "+" | "-" | "*" + ; + +arithmetic_expression = ( "(" , arithmetic_expression , arithmetic_operation , arithmetic_expression , ")" ) + | variable | numeral ; + +boolean_operation = "or" | "not" + ; + +relational_operation = "=" | "#" | "<" | "<=" | ">" | ">=" + ; + +boolean_expression = ( "(" , boolean_expression , boolean_operation , boolean_expression , ")" ) + | ( "not" , boolean_expression ) + | ( arithmetic_expression , relational_operation , arithmetic_expression ) + ; + +statement = "skip" + | ( variable , ":=" , arithmetic_expression ) + | ( "(" , statement , ";" , "statement" , ")" ) + | ( "if" , boolean_expression , "then" , statement , "else" , statement , "end" ) + | ( "while" , boolean_expression , "do" , statement , "end" ) + | ( "var" , variable , ":=", arithmetic_expression , "in" , statement , "end" ) + ; \ No newline at end of file diff --git a/src/driver.c b/src/driver.c index 60d3ce2..fc2a3ae 100644 --- a/src/driver.c +++ b/src/driver.c @@ -25,7 +25,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Usage: %s [-i program.imp]\n" " -i interpret program and exit\n" - " (no args) start REPL\n", + " (no args) start REPL\n", argv[0]); return (opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/repl.c b/src/repl.c index 9c61b98..5ea9792 100644 --- a/src/repl.c +++ b/src/repl.c @@ -7,14 +7,14 @@ #include static void print_help(void) { - puts( - "IMP REPL (type IMP statements or commands starting with '%')\n" + printf( + "IMP REPL (type IMP statements or commands starting with '%%')\n" "Commands:\n" - " %quit exit\n" - " %run interpret program\n" - " %set set variable\n" - " %print [] print variable, or all variables\n" - " %help show this message\n"); + " %%quit exit\n" + " %%run interpret program\n" + " %%set set variable\n" + " %%print [] print variable, or all variables\n" + " %%help show this message\n"); } static void repl_exec_command(hashmap_t context, const char *command) { -- cgit v1.2.3