While this code is open source, you may not use it or even look at it if you are taking CS 160 or a similar compilers course. You have been warned.
Andres Riofrio ariofrio@cs.ucsb.edu
Add test harness: Feb 2-3
./simple --only-scanner
outputs a space-separated list of tokensmake test
compares generated tokens with expected tokensmake test
ensures parser accepts valid programs (there's no output)make test
ensures parser rejects invalid programs (there is output)- Added tests for each new feature introduced
Implement scanner: Feb 3
- The rule for octal numbers is above the rule for decimal numbers
- Tokens that are not single-character operators get a
%token
declaration in parser.ypp - Used a start condition to implement multi-line, non-greedy comments
Implement parser: Feb 3
- Used left-recursion for efficiency
- Use
%left
,%right
, and%nonassoc
to disambiguate the grammar according to this C/C++ precedence table
Implement AST generator: Feb 17
- Add harness files from class website
- Add actions to parser rules to construct the AST
- Separate
Assignment
parser rule intoAssignment
andArrayAssignment
; likewise withFunctionCall
- Add
Identifier
andNumber
parser rules that have an action that creates aPrimitive()
instead of adding the code to create aPrimitive()
every timeIDENTIFIER
andNUMBER
are used. - Set
yylval
in lexer rules to propagate integer values and symbol names - Check that each test passes (done manually: test harness not updated)
Implement type checking and semantic analysis: Mar 5-12
- Replace my code with harness files from class website
- Remove
--only-scanner
option and associated tests - Add semantic tests from class website
- Implement type checker, including checking that operators like magnitude get integers as their inner expression
- Implement constant folding
- BOTTOM propagates in order to catch errors more easily
- Division by zero gives zero
Implement code generation: Mar 17-20
- Test that correct programs compile and produce expected output
- Literals and identifiers push a value to the stack
- Operators pop one or two values from the stack and push the result
- Comparison operators implemented with
SETcc %eal
followed byANDL $255 %eax
- Function arguments are copied to occupy the same space as local variables, for ease of access
- Constants are folded (behave as literal integers/booleans)
if
statements with constant values only output the selected branchwhile
statements with constant values either eliminate the statement or simplify the infinite loop