README.txt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ============================================================
  2. Introduction to Bison.
  3. ============================================================
  4. 1. Get started!
  5. a) Check the available files:
  6. - Makefile : makefile to build the parser.
  7. - scanner.flex : the lexical analyser (scanner) in flex
  8. - parser.bison: the parser in bison
  9. Note: the given scanner & grammar only deal
  10. with numbers and the "+" operator.
  11. - common.c, common.h : common definitions / routines
  12. - ex1.txt ex2.txt ex3.txt : samples
  13. b) Compile the parser by typing "make" in the command line.
  14. The generated executable file is called "parser".
  15. c) Execute the parser for exemplo1.txt exemplo2.txt exemplo3.txt
  16. e.g. "./parser exemplo1.txt".
  17. You should get errors for all of them.
  18. Modify these files until the parser reports no errors.
  19. ============================================================
  20. 2. You may have noticed during compilation (step 1b)
  21. that bison reports "1 shift/reduce conflict"
  22. (if you missed it, recompile the parser using "make clean all").
  23. The conflict comes from the ambiguity of "expr ADD_TOKEN expr"
  24. (why is it ambiguous?).
  25. To overcome this conflict add
  26. %left ADD_TOKEN
  27. to the parser specification (on top) and re-compile.
  28. This change specifies left associativity for sum expressions
  29. and gets rid of the ambiguity.
  30. ============================================================
  31. 3. Complete the grammar to also handle the binary operators
  32. -, *, and /.
  33. Use %left as in step 2 to ensure there are no grammar conflicts.
  34. ============================================================
  35. 4. Now we will use semantic actions to associate a "double"
  36. value to each expression, and print these values.
  37. Proceed in the following steps:
  38. a) In parser.bison add at the top
  39. %define api.value.type { double }
  40. This will set "double" as the type of expressions.
  41. b) Modify scanner.flex to assign the value of a number to
  42. the special variable 'yylval' as part of the actions in the
  43. NUMBER_TOKEN rule. You may use the C library function 'atof'
  44. to convert the 'yytext' string to a number.
  45. c) In parser.bison encode semantic actions as follows:
  46. NUMBER_TOKEN { $$ = $1; }
  47. expr SUM_TOKEN expr { $$ = $1 + $3;}
  48. ... etc ...
  49. d) Use a semantic action for 'prog' to print the value of each
  50. expression read from the input
  51. e) If the values printed are wrong look at the order of the
  52. "%left" directives for the operators.
  53. This order dictates the precedence (lowest to highest) for
  54. the operators.
  55. 5. Extensions (outside class):
  56. a) Allow single-line comments begginning with "#".
  57. b) Handle expressions between "(" and ")".
  58. c) Allow variables identified by "a" to "z" in expressions, and allow
  59. attributions of the form "var = expr".
  60. You may assume all variables are initially 0.
  61. Suggestion: use an array to hold variable values.
  62. ============================================================