{"id":28438018,"url":"https://github.com/namangupta123/c-crafters","last_synced_at":"2026-04-25T22:33:07.536Z","repository":{"id":295656984,"uuid":"962142793","full_name":"Namangupta123/C-Crafters","owner":"Namangupta123","description":"Compiler Design Project (Mini C++ Compiler)","archived":false,"fork":false,"pushed_at":"2025-05-27T03:44:39.000Z","size":400,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-28T15:43:01.514Z","etag":null,"topics":["c","cli","compiler-design","cpp","low-level-programming","yacc-lex"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Namangupta123.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-07T18:01:51.000Z","updated_at":"2025-05-26T18:39:52.000Z","dependencies_parsed_at":"2025-05-26T18:25:50.645Z","dependency_job_id":"640a01e3-fb6b-472b-9d98-177c0e2274ac","html_url":"https://github.com/Namangupta123/C-Crafters","commit_stats":null,"previous_names":["namangupta123/c-crafters"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Namangupta123/C-Crafters","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Namangupta123%2FC-Crafters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Namangupta123%2FC-Crafters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Namangupta123%2FC-Crafters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Namangupta123%2FC-Crafters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Namangupta123","download_url":"https://codeload.github.com/Namangupta123/C-Crafters/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Namangupta123%2FC-Crafters/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32279655,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-25T18:29:39.964Z","status":"ssl_error","status_checked_at":"2026-04-25T18:29:32.149Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cli","compiler-design","cpp","low-level-programming","yacc-lex"],"created_at":"2025-06-06T00:38:41.435Z","updated_at":"2026-04-25T22:33:07.518Z","avatar_url":"https://github.com/Namangupta123.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C-Crafters: Mini C/C++ Compiler\n## Overview\nC-Crafters is a comprehensive mini compiler for a subset of C/C++ language, built using LEX (Lexical Analyzer) and YACC (Yet Another Compiler Compiler). This project demonstrates the fundamental phases of compilation including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and assembly code generation.\n\n## Features\n- Lexical Analysis : Tokenizes source code into meaningful lexemes\n- Syntax Analysis : Validates program structure using grammar rules\n- Semantic Analysis : Builds and manages symbol tables\n- Abstract Syntax Tree (AST) Generation : Creates hierarchical representation of program structure\n- Intermediate Code Generation (ICG) : Produces three-address code\n- Code Optimization : Implements constant folding and dead code elimination\n- Assembly Code Generation : Converts optimized code to machine-specific instructions\n\n### Supported C/C++ Features\n- Data types: int, float, char, void\n- Control structures: for loops, while loops, if-else conditionals\n- Arithmetic and logical operations\n- Variable declarations and assignments\n\n## Project Structure\n```\n├── AST/                        # Abstract Syntax Tree generation\n│   ├── ast.l                   # Lexical analyzer for AST\n│   ├── ast.y                   # YACC grammar for AST generation\n│   └── input.cpp               # Sample input file\n├── CodeOpt/                    # Code optimization techniques\n│   ├── main.py                 # Implements constant folding and dead code elimination\n│   ├── input.txt               # Intermediate code input\n│   └── output.txt              # Optimized code output\n├── ICG/                        # Intermediate Code Generation\n│   ├── Yacc.y                  # YACC grammar for ICG\n│   ├── lex.l                   # Lexical analyzer for ICG\n│   └── input.cpp               # Sample input file\n├── LexicalAnalyser/            # Lexical analysis implementation\n│   ├── lexicalanalyzer.cpp     # Tokenizes input source code\n│   └── lexicalinput.txt        # Sample input file\n├── SymbolTable/                # Symbol table implementation\n│   ├── sym.l                   # Lexical analyzer for symbol table\n│   ├── sym.y                   # YACC grammar for symbol table\n│   └── input.cpp               # Sample input file\n└── Assembly/                   # Assembly code generation and handling\n    ├── main.py                 # Python script to generate assembly code\n    ├── input.txt               # Intermediate code input\n    └── output.txt              # Generated assembly code output\n```\n\n## Compilation Phases\n### 1. Lexical Analysis\nThe lexical analyzer breaks down source code into tokens such as keywords, identifiers, operators, and literals. It ignores whitespace and comments.\n\nExample output:\n\n```\nKeywords: int, if, else, while, for\nIdentifiers: a, b, c, main\nMath Operators: +, -, *, /, =\nLogical Operators: \u003c, \u003e\nNumerical Values: 1, 2, 3.14\nOthers: (, ), {, }, ;\n```\n### 2. Syntax Analysis\nThe parser validates the structure of the program according to the grammar rules of the language and builds a parse tree.\n\n### 3. Semantic Analysis \u0026 Symbol Table\nThe compiler creates and maintains a symbol table to track variables, their types, scopes, and other attributes. This helps in type checking and scope resolution.\n\n### 4. Abstract Syntax Tree (AST)\nThe AST provides a hierarchical representation of the program structure, making it easier to analyze and transform the code.\n\n### 5. Intermediate Code Generation\nThe compiler generates three-address code as an intermediate representation, which is closer to machine code but still machine-independent.\n\nExample output:\n\n```\nT0 = i + 1\ni = T0\nT1 = b \u003c 7\nif T1 goto L0\n```\n### 6. Code Optimization\nThe optimization phase improves the intermediate code by applying techniques such as:\n\n- Constant Folding : Evaluates constant expressions at compile time\n- Dead Code Elimination : Removes code that doesn't affect the program output\n\n### 7. Code Generation\nThe final phase generates machine code from the optimized intermediate code.\n\n\n## Installation \u0026 Usage\n### Prerequisites\n- GCC compiler\n- Flex (Fast Lexical Analyzer)\n- Bison (GNU Parser Generator, replacement for YACC)\n- Python 3.x (for code optimization)\n\n### Building the Compiler\n1. Lexical Analyzer\n```\ncd LexicalAnalyser\ng++ lexicalanalyzer.cpp -o lexical\n./lexical\n```\n2. Symbol Table\n```\ncd SymbolTable\nflex sym.l\nbison -dy sym.y\ngcc lex.yy.c y.tab.c -o symbol_table\n./symbol_table input.cpp\n```\n3. Abstract Syntax Tree\n```\ncd AST\nflex ast.l\nbison -dy ast.y\ngcc lex.yy.c y.tab.c -o ast\n./ast input.cpp\n```\n4. Intermediate Code Generation\n```\ncd ICG\n./run.sh\n```\n5. Code Optimization\n```\ncd CodeOpt\npython3 main.py\n```\n6. Assembly Code Generation\n```\ncd Assembly\npython3 main.py\n```\n\n## Example Workflow\n1. Write a C/C++ program in input.cpp\n2. Run the lexical analyzer to tokenize the code\n3. Generate the symbol table to track variables and their properties\n4. Create an abstract syntax tree to represent the program structure\n5. Generate intermediate code\n6. Apply optimizations to the intermediate code\n7. Generate the final optimized code\n\n## Limitations\n- Supports only a subset of C/C++ language features\n- Limited error handling and recovery\n\n## Future Enhancements\n- Support for more C/C++ features (structs, classes, pointers)\n- Enhanced error reporting and recovery\n- More optimization techniques\n\n## Contributors\n- Aryan Shorya (Team Lead | AST Generator and Assembly Code Generator)\n- Diya Gupta (Lexical Analyzer)\n- Naman Gupta (Code Optimizer and Intermediate Code Generator)\n- Riya Vihan (Symbol Table Generator)\n\n## Acknowledgment\nThis project was developed as part of a compiler design course/project.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnamangupta123%2Fc-crafters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnamangupta123%2Fc-crafters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnamangupta123%2Fc-crafters/lists"}