{"id":22253758,"url":"https://github.com/khaledkamr/compiler-project","last_synced_at":"2025-03-25T12:26:07.992Z","repository":{"id":239502320,"uuid":"799300363","full_name":"khaledkamr/Compiler-project","owner":"khaledkamr","description":"This is a compiler project developed during my college coursework. It includes lexical analysis, syntax analysis, semantic analysis, intermediate code generation, optimization, and code generation stages. The goal is to translate high-level language source code into target machine code.","archived":false,"fork":false,"pushed_at":"2024-06-11T21:04:13.000Z","size":3588,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T11:27:22.886Z","etag":null,"topics":["c","lexical-analysis","semantic-analysis","syntax-analysis"],"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/khaledkamr.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}},"created_at":"2024-05-11T18:03:03.000Z","updated_at":"2024-06-11T21:04:55.000Z","dependencies_parsed_at":"2024-06-12T01:07:59.386Z","dependency_job_id":null,"html_url":"https://github.com/khaledkamr/Compiler-project","commit_stats":null,"previous_names":["khaledkamr/compiler-project"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledkamr%2FCompiler-project","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledkamr%2FCompiler-project/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledkamr%2FCompiler-project/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledkamr%2FCompiler-project/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khaledkamr","download_url":"https://codeload.github.com/khaledkamr/Compiler-project/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245460550,"owners_count":20619092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","lexical-analysis","semantic-analysis","syntax-analysis"],"created_at":"2024-12-03T07:19:50.852Z","updated_at":"2025-03-25T12:26:07.957Z","avatar_url":"https://github.com/khaledkamr.png","language":"C","readme":"# Summary\nIn this project a language called C minus is derived from the Tiny language. Then the lexical analyzer prints out the tokens of this derived language.\n\n# Introduction to the Compiler\nA compiler is a program that translates source code written in a high-level programming language into a lower-level language, often machine code, which can be executed by a computer's processor.\n\n# Phases of the Compiler\n### Lexical Analysis (Scanning): \nThis initial phase involves parsing the source code into a series of tokens, which are the smallest meaningful units such as keywords, identifiers, and symbols. A lexer or scanner is responsible for this task. \n\n### Syntax Analysis (Parsing): \nFollowing lexical analysis, the syntax analysis phase checks the validity of the token sequence against the language's grammar rules. It constructs a parse tree or abstract syntax tree (AST) representing the program's structure. Parsing is conducted by the parser. \n\n### Semantic Analysis: \nOnce the syntax is verified, semantic analysis ensures the correctness of statements based on the language's semantics. This includes tasks like type checking, scope resolution, and creating symbol tables to manage identifiers and their properties.\n\n### Intermediate Code Generation: \nHere, the compiler produces an intermediate representation (IR) of the source code. This IR is platform-independent and serves as a common ground for subsequent optimizations and target platform translations. \n\n### Optimization: \nOptimization techniques are applied to enhance the efficiency of the intermediate code. These techniques range from simple transformations to more complex algorithms like loop optimization and data-flow analysis. \n\n### Code Generation: \nIn this phase, the compiler translates the optimized intermediate code into machine code or assembly instructions tailored to the target hardware. It maps high-level language constructs to low-level instructions, considering factors like register allocation and instruction scheduling.\n\n# Software Tools\nCompiler development often relies on a variety of software tools to assist in different stages of the compilation process. These tools aid in tasks such as parsing, optimization, code generation, debugging, and performance analysis. Here are some of the tools listed below.\n\nLex and Yacc (or Flex and Bison):\nLex (or Flex) and Yacc (or Bison) constitute classic tool combinations for lexical analysis (scanning) and syntax analysis (parsing), respectively. Lex generates lexical analyzers based on regular expressions, while Yacc generates parsers based on context-free grammars. These tools are commonly employed for generating the scanner and parser components of a compiler.\n\nANTLR (ANother Tool for Language Recognition):\nANTLR stands out as a potent parser generator that supports various programming languages and facilitates the creation of\nparsers for intricate grammars. It finds extensive use in compiler construction and language development endeavors.\n\nLLVM (Low-Level Virtual Machine):\nLLVM serves as a compiler infrastructure offering a collection of modular and reusable compiler and toolchain components. It encompasses a compiler front-end for parsing and optimizing source code, a middle-end featuring diverse optimization passes, and a back-end for generating machine code across multiple target architectures. LLVM finds wide application in both academic research and industrial compiler development.\n\nGCC (GNU Compiler Collection):\nGCC emerges as a popular open-source compiler suite supporting multiple programming languages, such as C, C++, and Fortran. It encompasses front-ends for parsing source code written in these languages, alongside a comprehensive array of optimization and code generation tools.\n\nClang:\nClang, built upon LLVM, represents a C/C++ compiler delivering swift and precise compilation with expressive diagnostics. It frequently serves as a front-end in various toolchains and integrated development environments (IDEs).\n\n\nValgrind:\nValgrind stands as a potent debugging and profiling tool aiding in the detection of memory leaks, buffer overflows, and other memory-related errors in compiled programs. It furnishes a suite of tools, including Memcheck for memory debugging and Callgrind for profiling.\n\nGDB (GNU Debugger):\nGDB stands as a widely utilized debugger for debugging compiled programs. It empowers developers to scrutinize and manipulate program execution, set breakpoints, inspect variables, and trace program flow.\n\nPerf:\nPerf constitutes a performance analysis tool tailored for Linux systems, furnishing insights into program behavior, CPU usage, cache misses, and other performance metrics. It facilitates code optimization and identification of performance bottlenecks in compiled programs.\n\n# code \n### Example 1:\n```C\nint fact(int x)\n{\n    if(x \u003e 1)\n    return x * fact(x - 1);\n    else\n    return 1;\n}\nvoid main(void)\n{\n    int x;\n    x = read();\n    if(x \u003e 0) write (fact(x));\n}\n```\n```\n1: ID, name= int                7: reserved word: void\n1: ID, name= fact               7: ID, name= main\n1: (                            7: (\n1: ID, name= int                7: reserved word: void\n1: ID, name= x                  7: )\n1: )                            8: {\n2: {                            8: ID, name= int\n2: reserved word: if            8: ID, name= x\n2: (                            8: ;\n2: ID, name= x                  9: ID, name= x\n2: \u003e                            9: =\n2: NUM, val= 1                  9: reserved word: read\n2: )                            9: (\n3: ID, name= return             9: )\n3: ID, name= x                  9: ;\n3: *                            10: reserved word: if\n3: ID, name= fact               10: ( \n3: (                            10: ID, name= x\n3: ID, name= x                  10: \u003e\n3: -                            10: NUM, val= 0\n3: NUM, val= 1                  10: )\n3: )                            10: reserved word: write\n3: ;                            10: (\n4: reserved word: else          10: ID, name= fact\n5: ID, name= return             10: (\n5: NUM, val= 1                  10: ID, name= x\n5: ;                            10: )\n6: }                            10: )\n                                10: ;\n                                11: }\n                                11: EOF\n```\n### Example 2:\n```C\nint fun(int u, int v)\n{\n    if(v == 0)\n    return u;\n    else\n    return fun(v, u - u / v * v);\n}\nvoid main(void)\n{\n    int x, int y;\n    x = input();\n    y = input();\n    output(gcd(x, y));\n}\n```\n```\n1: ID, name= int                      7: reserved word: void\n1: ID, name= fun                      7: ID, name= main\n1: (                                  7: (\n1: ID, name= int                      7: reserved word: void\n1: ID, name= u                        7: )\n1: ,                                  8: {\n1: ID, name= int                      8: ID, name= int\n1: ID, name= v                        8: ID, name= x\n1: )                                  8: ,\n2: {                                  8: ID, name= int\n2: reserved word: if                  8: ID, name= y\n2: (                                  8: ;\n2: ID, name= v                        9: ID, name= x\n2: =                                  9: =\n2: =                                  9: ID, name= input\n2: NUM, val = 0                       9: (\n2: )                                  9: )\n2: ID, name= return                   9: ;\n2: ID, name= u                        9: ID, name= y\n2: ;                                  9: =\n3: reserved word: else                9: ID, name= input\n3: ID, name= return                   9: (\n3: ID, name= fun                      9: )\n3: (                                  9: ;\n3: ID, name= v                        10: ID, name= output\n3: ,                                  10: (\n3: ID, name= u                        10: ID, name= gcd\n3: -                                  10: (\n3: ID, name= u                        10: ID, name= x\n3: /                                  10: ,\n3: ID, name= v                        10: ID, name= y\n3: *                                  10: )\n3: ID, name= v                        10: )\n3: )                                  10: ;\n3: ;                                  12: }\n5: }                                  12: EOF\n```\n\n# Conclusion\nAfter finishing this project we now have a better understanding of how the compiler works and how the tokens are printed during the Lexical Analysis.\n\n# References\nCompiler Construction: Principles and Practice\nKenneth C. Louden\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhaledkamr%2Fcompiler-project","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhaledkamr%2Fcompiler-project","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhaledkamr%2Fcompiler-project/lists"}