CS52 - Spring 2016 - Class 19
- Academic honesty
- 5 operators
- work bitwise
- 5 & 4 = 101 & 100 = 100
- 5 | 4 = 101 & 100 = 101
- !5 = !101 = 010
- Some more complicated ones
- 110 & 100 | 011 = ?
- order of operations!
- | and ^
- 110 & 101 | 100 = 100
- !110 & (101 | 100) = 001 & 101 = 001
Parsing binary expressions
- EBNF for binary
- 110 & 100 | 011
- draw tree
- 110 & 100 & 011
- two possible trees!
- &, | and ^ will all left associate
- -> will right associate
- !110 & (101 | 100)
Work left to right
- can be done in a single pass
- three main phases:
- tokenization (scan): characters to tokens
- parsing (parse): tokens to a syntax tree
- evaluation (eval): syntax tree to a value
- write at least one function for each production rule
- e.g. byte, literal, etc.
- should translate directly from the EBNF rules
- each of these functions should:
- take a list of tokens as input
- consume as many tokens as needed for that particular component
- return a tuple:
1) the syntax tree from the created component
2) a list of the remaining tokens that were not processed (i.e. were not part of this component)
Look at the disjunction function
Advice on how to proceed: work bottom up and debug as you go!