Work on this exercise locally

This web app is a reference guide — you can read instructions, browse starter code, and view tests here. To actually complete the exercise, you need to work in your local development environment.

1Clone the repo: git clone https://github.com/weihaoqu/program-analysis-bootcamp-student
2Edit the starter file in your editor (VS Code, Vim, etc.) — replace failwith "TODO" with your implementation.
3Run the tests: dune runtest labs/lab2-ast-parser

Lab 2: AST Parser

Lab 2: AST Parser and Analyzer

Prerequisites: Modules 1-2 completed Duration: 3 hours | Total Points: 100

Objectives

  • Parse Python source files into ASTs using the ast module
  • Implement AST traversal to extract program information
  • Generate a structured analysis report

Parts

Part A: AST Generation (30 points)

  • Parse provided Python sample programs into ASTs
  • Generate visual representations of the ASTs
  • Handle at least 3 different statement types (assignment, if, for, function def, etc.)

Part B: Traversal Implementation (40 points)

  • Implement depth-first traversal using the Visitor pattern
  • Extract: all variable names, all function calls, all assignments
  • Count node types and report statistics

Part C: Analysis Report (30 points)

  • Document findings from AST analysis of all sample programs
  • Identify potential code issues (unused variables, unreachable code)
  • Propose improvement suggestions

Getting Started

cd labs/lab2-ast-parser/starter
pip install -r requirements.txt  # if present
python ast_parser.py sample_programs/fibonacci.py
pytest tests/test_parser.py -v

Deliverables

  1. Completed ast_parser.py with all TODO items implemented
  2. Completed analyzer.py with analysis passes
  3. Test suite passing: pytest tests/test_parser.py
  4. (Solution only) Completed analysis_report.md

Grading Rubric

CriteriaExcellent (90-100%)Good (80-89%)Satisfactory (70-79%)Needs Work (<70%)
Technical ImplementationHandles complex code, robustMost cases workBasic functionalitySignificant issues
Code QualityClean, documentedGood structureFunctionalPoor structure
Analysis DepthInsightful findingsClear findingsBasic analysisShallow analysis
Problem SolvingCreative, handles edge casesSolid approachMeets requirementsIncomplete