README.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ============================================================
  2. Abstract syntax trees (ASTs)
  3. ============================================================
  4. Summary: we will work on building ASTs for a language of
  5. arithmetic expressions involving integers.
  6. We will start with "programs" defined by a single expression
  7. made of numbers and sum (+) operations.
  8. After parsing, the AST for a program is visited/interpreted
  9. to print values for expressions.
  10. ============================================================
  11. 1. Get started:
  12. a) Check the available files:
  13. - Makefile : makefile to build the interpreter.
  14. - scanner.flex : the lexical analyser (scanner) in flex
  15. - parser.bison: the parser in bison
  16. - ast.h, ast.c: AST declarations & constructor implementations
  17. - interpreter.c: the interpreter routines including main
  18. - example[1234].txt : example files
  19. b) Compile the interpreter by typing "make" in the command line.
  20. The generated executable file is called "interpreter".
  21. c) The interpreter recognises a single expression composed
  22. of single integers or expressions making use
  23. of the '+' operator.
  24. Execute it for example1.txt and example2.txt, for instance
  25. type "./interpreter example1.txt".
  26. ============================================================
  27. 2. Handle binary operators '-', '*', '/', '%'.
  28. You'll need to:
  29. a) Modify the parser / scanner by defining / recognising
  30. a new tokens for each operator, and also operator
  31. associativity and precedence.
  32. b) Add new rules to the grammar for 'expr'.
  33. c) Modify the 'eval' function in 'interpreter.c'.
  34. c) Test with examples that make use of the new
  35. operators, for instance the available 'example3.txt'.
  36. ============================================================
  37. 3. Modify the language such that the interpreter accepts a
  38. list of expressions, rather than a single expression.
  39. The following steps are suggested:
  40. a) In ast.h: define the 'ExprList' datatype
  41. as a single-linked list of expressions and
  42. declare a constructor function for expression lists:
  43. ExprList* ast_exprlist(Expr* expr, ExprList* next);
  44. b) In ast.c: implement the new constructor function.
  45. c) In parser.bison change the grammar by:
  46. -- modifying the rule for 'program' to
  47. program: expr_list
  48. and define the rules for exprList
  49. -- associating the 'ExprList' type to 'expr_list'
  50. -- changing the type of the 'root' variable to 'ExprList'
  51. d) In interpreter.c: modify the main function such that it
  52. prints the evaluation of each expression in the 'root' list.
  53. You may test your code with "example4.txt".
  54. ============================================================
  55. 4. Relational operators (to work outside class)
  56. Handle expressions of the type 'bexpr ? e1 : e2'
  57. where 'bexpr' is a boolean expression, and 'e1' and 'e2' are
  58. arithmetic expressions.
  59. Boolean expressions can either be 'true', 'false',
  60. or 'expr <relop> expr' where <relop> is one of the
  61. relational operators '==', '!=', '<', '>', <=' and '>='.