How konas122/sysy-compiler works
Project Type and Purpose
• This is a compiler for the SysY language.
• It implements the classic compilation pipeline: lexical analysis, parsing, semantic/type analysis, AST construction, and x86 code generation.
• It uses Flex (lexer), Bison (parser), and the GNU toolchain (assembler/linker).
File Structure
• Top‑level scripts & configs
• makefile, batch_compile.sh, compile.sh
• Source directory (src/)
• Lexical spec: sysy.l
• Grammar: sysy.y
• Driver: main.cpp
• AST & symbol tables: tree.cpp/h, type.cpp/h, common.h, global.h
• Build artifacts
• build/ (placeholder)
• out/ (generated assembly / binaries)
• Tests
• test/syntax/ (syntax‑level conformance)
• test/semantic/ (semantic checks)
• test/asm_test/ (end‑to‑end code‑generation validation)
Extract Architecture Insights from README
• The compile steps map to make targets:
• make syntaxX → generate AST for syntax tests
• make checkX → semantic checks
• make run / scripts → generate & assemble x86 code
• Outputs: AST in result.txt (debug), assembly in .asm, object files via as, executables via gcc -m32.
Main Components & Interactions
a. Frontend
• Lexer (Flex/runs sysy.l)
• Parser (Bison/runs sysy.y)
• AST Builder (in tree.cpp/h)
b. Semantic Analyzer
• Symbol Table & Type Checker (in type.cpp/h, global.h)
c. Code Generator
• AST → x86 Assembly emitter (main.cpp orchestration + emitter code)
d. Assembler & Linker
• External Tools: as, gcc -m32
e. Build & Test Harness
• Scripts: makefile, batch_compile.sh, compile.sh
• Test Suites: syntax, semantic, asm_test
Relationships & Data Flow
• Source Code (.c) → [Lexer] → token stream
• Token stream → [Parser] → parse tree/AST
• AST → [Semantic Analyzer] → annotated AST or error reports
• Annotated AST → [Code Generator] → x86 .asm file
• .asm → [Assembler → Object] → [Linker → Executable]
• Tests feed source into this pipeline, validate outputs.
Architectural Patterns & Principles
• Pipeline: clear separation of compilation stages
• Modularity: each stage implemented in its own source files
• Tool Integration: Flex/Bison and GCC toolchain as external “services”
• Test‑Driven: separate directories for syntax, semantic, and codegen tests
Final Checklist
• [ ] Groupings for “Compiler Core”, “Build & Test Harness”, “Output Artifacts”
• [ ] Optional: annotate where files live (src/, test/, out/)