Skip to main content

A Python library for demonstrating compiler design concepts

Project description

compilerdesign

A Python library for demonstrating core Compiler Design concepts. Import it as cd and use clean, simple functions covering everything from lexical analysis to intermediate code generation.

pip install compilerdesign

Quick Start

import compilerdesign as cd

Features & API Reference

1. Lexical Analyzer

code = """
int main() {
    int x = 10;
    float y = 3.14;
    return x + y;
}
"""
result = cd.lexical_analyzer(code)
print(result['summary'])
# {'total_tokens': 24, 'keywords': 5, 'identifiers': 4, 'integer_constants': 1,
#  'float_constants': 1, 'operators': 3, ...}
print(result['tokens'])       # list of (token_type, value)
print(result['unique_identifiers'])  # ['main', 'x', 'y']

2. Grammar Transformations

Grammar format: dict mapping non-terminal → list of production strings.

grammar = {
    'E': ['E + T', 'T'],
    'T': ['T * F', 'F'],
    'F': ['( E )', 'id']
}

# Eliminate Left Recursion
new_g = cd.eliminate_left_recursion(grammar)
# {'E': ["T E'"], "E'": ["+ T E'", 'ε'], 'T': ["F T'"], "T'": ["* F T'", 'ε'], ...}

# Left Factoring
grammar2 = {'A': ['a b', 'a c', 'd']}
factored = cd.left_factoring(grammar2)
# {'A': ["a A'", 'd'], "A'": ['b', 'c']}

# Check Ambiguity
result = cd.check_ambiguity(grammar)
# {'ambiguous': False, 'issues': []}

3. FIRST and FOLLOW Sets

grammar = {
    'E':  ['T R'],
    'R':  ['+ T R', 'ε'],
    'T':  ['F Y'],
    'Y':  ['* F Y', 'ε'],
    'F':  ['( E )', 'i']
}

result = cd.compute_first_follow(grammar, start='E')
print(result['FIRST'])   # {'E': ['(', 'i'], 'R': ['+', 'ε'], ...}
print(result['FOLLOW'])  # {'E': ['$', ')'], 'R': ['$', ')'], ...}

# Or individually:
first  = cd.compute_first(grammar)
follow = cd.compute_follow(grammar, start='E')

4. LL(1) Predictive Parsing Table

table_result = cd.build_ll1_table(grammar, start='E')
print(table_result['is_ll1'])     # True/False
print(table_result['table'])      # {(NonTerminal, Terminal): production}
print(table_result['conflicts'])  # list of conflict strings

# Parse a token string
parse = cd.ll1_parse(grammar, tokens=['i', '+', 'i', '*', 'i'], start='E')
print(parse['accepted'])  # True
for step in parse['steps']:
    print(step)

5. Shift-Reduce Parsing

productions = [
    ('E', ['E', '+', 'T']),
    ('E', ['T']),
    ('T', ['T', '*', 'F']),
    ('T', ['F']),
    ('F', ['id'])
]
result = cd.shift_reduce_parse(productions, tokens=['id', '+', 'id'])
print(result['accepted'])  # True
for step in result['steps']:
    print(step)  # {'stack': [...], 'input': [...], 'action': '...'}

6. LEADING and TRAILING Sets

result = cd.compute_leading_trailing(grammar)
print(result['LEADING'])   # {'E': ['(', '+', '*', 'i'], ...}
print(result['TRAILING'])  # {'E': [')', '+', '*', 'i'], ...}

7. LR(0) Items

productions = [
    ('E', ['E', '+', 'T']),
    ('E', ['T']),
    ('T', ['id'])
]
result = cd.compute_lr0_items(productions, start='E')
print(f"Total states: {result['num_states']}")
for state in result['states']:
    print(f"\nState {state['id']}:")
    for item in state['items']:
        print(f"  {item}")
for (frm, sym, to) in result['transitions']:
    print(f"  State {frm} --{sym}--> State {to}")

8. Intermediate Code Generation

# Infix → Postfix (RPN)
cd.infix_to_postfix("a + b * c")      # "a b c * +"
cd.infix_to_postfix("(a + b) * c")    # "a b + c *"

# Infix → Prefix (Polish Notation)
cd.infix_to_prefix("a + b * c")       # "+ a * b c"

# Postfix → Infix
cd.postfix_to_infix("a b c * +")      # "(a + (b * c))"

# Prefix → Infix
cd.prefix_to_infix("+ a * b c")       # "(a + (b * c))"

# Universal converter
cd.convert_expression("a + b * c", from_notation="infix", to_notation="postfix")
cd.convert_expression("a b c * +",  from_notation="postfix", to_notation="prefix")

Publishing to PyPI

See PUBLISHING.md for step-by-step instructions.


License

MIT

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.1.tar.gz (12.2 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.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: compilerdesign-1.0.1.tar.gz
  • Upload date:
  • Size: 12.2 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.1.tar.gz
Algorithm Hash digest
SHA256 9716a6062218bd8d522584b0300008a0fc14f85064aa7d08fc649a0c4d1dd18e
MD5 52aed1bef25dcca6a70f2a5f000ab954
BLAKE2b-256 fef2136e856057084aeec2db1e4efc318d71e9facb07e25a893cfd8acac92ec3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compilerdesign-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8d9c2e298a312ebe51e209f40d1e50ed9f95dd4792714d84075d70b3268d856
MD5 ed7c0889ce4359436065095d25635fd9
BLAKE2b-256 cd83cd0cbc7486d5aea75e3dee5fdf35741c0a2fd84f5bfc201be00d95061903

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