Skip to main content

A teaching Python library for compiler-design topics: lexer, grammar transforms, FIRST/FOLLOW, LL(1), shift-reduce, LR(0), symbol table, 3AC, DAG, and expression conversion.

Project description

compilerdesign

A teaching Python library that covers the entire Compiler Design syllabus in one import. Every function returns a plain dict / str, and every function has a matching show_* pretty-printer so you can read the output at a glance.

pip install compilerdesign
import compilerdesign as cd

cd.show_lexical(cd.lexical_analyzer("int x = 10;"))

Full walkthrough with input formats, outputs and end-to-end examples lives in DOCUMENTATION.md. Version history: CHANGES.md.


What's inside

Topic Core function Pretty-printer
Lexical analysis lexical_analyzer show_lexical
Symbol table build_symbol_table show_symbol_table
Left-recursion elimination eliminate_left_recursion show_grammar
Left factoring left_factoring show_grammar
Ambiguity check (heuristic) check_ambiguity show_ambiguity
FIRST / FOLLOW compute_first_follow show_first_follow
LEADING / TRAILING compute_leading_trailing show_leading_trailing
LL(1) table + parse trace build_ll1_table / ll1_parse show_ll1_table / show_parse_trace
Shift-reduce parser shift_reduce_parse show_parse_trace
LR(0) canonical collection compute_lr0_items show_lr0
Infix ↔ Postfix ↔ Prefix convert_expression, etc. show_expression
Three-address code (quadruples) generate_three_address_code show_three_address_code
DAG of an expression build_dag show_dag

60-second tour

import compilerdesign as cd

# 1) Lexical + Symbol table
src = """
int add(int a, int b) {
    int c = a + b * 2;
    return c;
}
"""
cd.show_lexical(cd.lexical_analyzer(src))
cd.show_symbol_table(cd.build_symbol_table(src))

# 2) Expression → 3AC → DAG
expr = "a + b * c - d"
cd.show_three_address_code(cd.generate_three_address_code(expr))
cd.show_dag(cd.build_dag(expr))

# 3) Grammar pipeline
g = {'E': ['E + T', 'T'], 'T': ['T * F', 'F'], 'F': ['( E )', 'id']}
g = cd.eliminate_left_recursion(g)
cd.show_grammar(g, "NO LEFT RECURSION")
cd.show_first_follow(cd.compute_first_follow(g, start='E'))
cd.show_ll1_table(cd.build_ll1_table(g, start='E'))
cd.show_parse_trace(
    cd.ll1_parse(g, ['id', '+', 'id', '*', 'id'], start='E'),
    "LL(1) PARSE",
)

Grammar input format

grammar = {
    'E': ['E + T', 'T'],    # each production is a space-separated string
    'T': ['T * F', 'F'],
    'F': ['( E )', 'id'],
}
# epsilon: 'ε' or 'eps'

Shift-reduce / LR(0) take a list of tuples instead:

productions = [
    ('E', ['E', '+', 'T']),
    ('E', ['T']),
    ('T', ['id']),
]

Running tests

python test_all.py

License

MIT — see DOCUMENTATION.md for the full reference.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compilerdesign-1.0.2.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compilerdesign-1.0.2-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file compilerdesign-1.0.2.tar.gz.

File metadata

  • Download URL: compilerdesign-1.0.2.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for compilerdesign-1.0.2.tar.gz
Algorithm Hash digest
SHA256 57b56113a806f7a8728d4c0a3447422a6533669e7e9fbb6f1d9a0e7a138ec76e
MD5 619725dae613af30acc5b1a9e62634bd
BLAKE2b-256 0573528738334efdc13701b2b9118fc9065793c1454187701042bf1c4d768deb

See more details on using hashes here.

File details

Details for the file compilerdesign-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: compilerdesign-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for compilerdesign-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eb4cfa7b7ed7cee30f6e942294caa359db9001d49034f2a8bbd9f84efb4ee2b9
MD5 91dc1579f7f5307088da684fd36fb020
BLAKE2b-256 6f7ca3be8c199e74a2e5b67fdd67f265bbe3abdcd3ce630e0c60f0908bed7979

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page