{"id":26822525,"url":"https://github.com/techkumarnitish/neurocode-compiler","last_synced_at":"2025-03-30T08:18:50.792Z","repository":{"id":283603429,"uuid":"952303866","full_name":"TechKumarNitish/NeuroCode-compiler","owner":"TechKumarNitish","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-21T04:36:38.000Z","size":179,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T05:26:41.912Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/TechKumarNitish.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":"2025-03-21T04:09:49.000Z","updated_at":"2025-03-21T04:36:41.000Z","dependencies_parsed_at":"2025-03-21T05:36:44.726Z","dependency_job_id":null,"html_url":"https://github.com/TechKumarNitish/NeuroCode-compiler","commit_stats":null,"previous_names":["techkumarnitish/neurocode-compiler"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TechKumarNitish%2FNeuroCode-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TechKumarNitish%2FNeuroCode-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TechKumarNitish%2FNeuroCode-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TechKumarNitish%2FNeuroCode-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TechKumarNitish","download_url":"https://codeload.github.com/TechKumarNitish/NeuroCode-compiler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246290883,"owners_count":20753775,"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":[],"created_at":"2025-03-30T08:18:50.377Z","updated_at":"2025-03-30T08:18:50.784Z","avatar_url":"https://github.com/TechKumarNitish.png","language":"C","readme":"# Mini-compiler\n\n### Project Intro\n\n*The main output of this project is designing a simple programming language and implementing its compiler, to achieve this goal the following points were to be done:*\n\n- Designing and implementing the language and its rules\n  - Designing the lexical analyzer\n  - Designing the parser rules\n- Designing a symbol table\n- Implementing actions to execute upon successful reduction of parser rules\n- Detecting syntax and sematic errors\n- Translating input written in our language to assembly-like quadruples\n\n*The used tools and technologies for this purpose were*\n\n- Yacc(Bison) and Flex for the lexical analyzer and the parser rules\n\n  \n\n  \u003cimg src=\"https://upload.wikimedia.org/wikipedia/en/thumb/2/22/Heckert_GNU_white.svg/1024px-Heckert_GNU_white.svg.png\" style=\"zoom:20%;\" /\u003e\n\n  \n\n- C language for designing the actions executed upon parser rules reduction\n\n  \u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/The_C_Programming_Language_logo.svg/800px-The_C_Programming_Language_logo.svg.png\" style=\"zoom:25%;\" /\u003e\n\n- Python for the GUI\n\n  \n\n  \u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/1920px-Python_logo_and_wordmark.svg.png\" style=\"zoom:15%;\" /\u003e\n\n*So please make sure you have all these dependencies installed on your machine to be able to run our code*\n\n*Then to run our code, execute the following command from your terminal*\n\n```powershell\npython .\\compiler_gui.py\n```\n\n***\n\n### Language Design\n\n*In this phase we designed a simple language and implemented its lexical analyzer and parser rules*\n\n##### The Language\n\n- Declaring and initializing a variable\n\n  - Notes:\n\n    - A variable name should start with a  letter, then it can include other alphanumeric characters but not special characters\n    - The variable datatype is determined according to the first value assigned to it\n    - Datatypes can be either integer, float, string or boolean. This datatype cannot be changed for the variable after declaration\n    - Variables can be declared as constant or normal variables\n\n  - Example:\n\n    ```python\n    w = 10;\n    x = 1.5;\n    y = \"test\";\n    z = true;\n    ```\n\n- Declaring and calling methods\n\n  - Notes:\n\n    - Defining a method is done by preceding the method name by the word `def`\n    - Method names start with a 'dollar sign' `$`, then a letter, then it can include other alphanumeric characters but not special characters\n    - Method arguments are named exactly like variables but with an 'at sign' `@` at the beginning\n    - If more than one method is to be declared in the same program, the arguments of each method should have unique names. No two methods are allowed to have the same argument names. This is our design to the language\n    - To pass a value calculated inside a method, this is done by passing a variable to the function call and this variable will be treated as a variable that is passed by reference\n\n  - Example:\n\n    ```python\n    def $sum(@x, @y, @z)\n    {\n      @z = @x + @y;\n    }\n    \n    a = 1;\n    b = 2;\n    totalSum = 0;\n    $sum(a, b, totalSum);\n    ```\n\n- Mathematical expressions\n\n  - Notes:\n\n    - The available mathematical operations are the simple `+` , `-` , `*` , `/`\n    - The left and right hand sides of each operator should be of the same datatype, so for example if a programmer wants to use a number that might be a fraction, then floats should be used on both sides of an operator\n\n  - Example:\n\n    ```python\n    x = 0;\n    y = 10;\n    z = x / y - x;\n    ```\n\n- Logic expressions\n\n  - Notes:\n\n    - The available logic expressions are `\u003c` , `\u003e` , `==` , `\u003c=` , `\u003e=` , `!=` , `\u0026\u0026` , `||` \n    - Just like the mathematical expressions types of the left and right hand sides of each operator should match\n    - Can be used directly inside loops and if conditions.\n    - Their output cannot be assigned into a variable, but an example below will show how we designed the language to achieve the same result\n\n  - Example:\n\n    ```python\n    a = true;\n    b = false;\n    if(a \u0026\u0026 b)\n    {\n        c = true;\n    }\n    else\n    {\n        c = false;\n    }\n    ```\n\n    *This is equivalent to a=true; b=false; c=a\u0026\u0026b; but it is a rare case so this how we designed the language to handle it*\n\n- If-else statements\n\n  - Notes:\n\n    - This is written like any other normal if else statement\n    - Else if can be implemented by nesting an if statement inside the else of another if\n\n  - Example:\n\n    ```python\n    a = 1;\n    b = 2;\n    c = 0;\n    if(a \u003e= b)\n    {\n        c = a;\n    }\n    else\n    {\n        if(a \u003c b)\n        {\n            c = b;\n        }\n    }\n    ```\n\n- For loops\n\n  - Notes:\n\n    - For loops are written as follows `for(variable assignment; logic expression; variable assignment){statements}`\n    - There's no `i++` like statements, so this can be done simply as `i=i+1`\n    - `break;` can be used inside a for loop (only allowed inside loops and switch-cases)\n    - `continue;` can be used inside a for loop (only allowed inside loops)\n\n  - Example:\n\n    ```python\n    for (i = 0; i \u003c 10; i=i+1)\n      {\n        b = 10;\n      }\n    ```\n\n- While loops\n\n  - Notes:\n\n    - `break;` can be used inside a for loop (only allowed inside loops and switch-cases)\n    - `continue;` can be used inside a for loop (only allowed inside loops)\n\n  - Example:\n\n    ```python\n    while (x \u003c 20)\n      {\n        x = x + 1;\n      }\n    ```\n\n- Repeat-Until loops\n\n  - Notes:\n\n    - The code in the `repeat` block will be executed till the condition inside the ` until` is true\n\n  - Example:\n\n    ```python\n    x=1;\n    repeat\n      {\n        x = x + 1;\n      } until (x \u003c 20);\n    ```\n\n- Switch-Case\n\n  - Notes:\n\n    - Switch takes a variable and case checks the equality of this variable to a certain value\n    - `break;` should be used after each case\n    - No default cases\n\n  - Example:\n\n    ```python\n    x=2;\n    switch (x)\n      {\n      case 1:\n        x = 10; break;\n      case 2:\n      \tx = 12; break;\n      }\n    ```\n\n    ***\n\n##### Lexical Analysis\n\n*For this purpose we used the flex tool*\n\n- Importance of this stage\n\n  1. Defining the acceptable tokens for our language\n\n  2. Matching these tokens and returning them for the syntax analyzer to perform parsing\n\n  3. Return matched values such as variable names, method names, method argument names, etc. to the parser to pass them to actions executed when parser rules are reduced\n\n     ***\n\n##### Syntax Analysis (A.k.a. Parser)\n\n*For this purpose we used the bison tool*\n\n- Importance of this stage\n  1. Defining the parser rules for our language\n  2. Parsing input and detecting syntax errors\n  3. Executing actions when parser rules are successfully reduced\n\n***\n\n### Symbol Table\n\n*The main purpose for a symbol table is to hold information about declared variables to be able to check upon the correctness of the usage of these variables after declaration. Also to have a list of all the declared variables to check if an undeclared variable is being used*\n\n##### Variables symbol table\n\n- Data structure\n  - It is a simple array of struct pointers\n  - These pointers are to point to a structs containing information about declared variables such as variable name, datatype and a flag to check if it is a constant\n  - We keep track of the variables count with every declaration\n\n##### Methods symbol table\n\n- Data structure\n  - It is a simple array of struct pointers\n  - These pointers are to point to structs containing information about defined functions such as function name, and number of arguments\n\n***\n\n### Actions\n\n*Actions are implemented using c language, and the following are the signatures of each function and its purpose*\n\n- Functions that create structs for constant values\n\n  ```c\n  nodeType *conInt(int value) {/*returns a pointer to created struct for int value*/}\n  nodeType *conFloat(float value) {/*returns a pointer to created struct for float value*/}\n  nodeType *conStr(char *value) {/*returns a pointer to created struct for string (ie. char[]) value*/}\n  nodeType *conBool(bool value) {/*returns a pointer to created struct for boolean value*/}\n  ```\n\n- Function called when a variable is declared or set\n\n  ```c\n  nodeType *setAndDeclare(char *variableName, nodeType* rhs, bool isConst) {/*declares a new variable, gives it a type and adds it to the symbol table, or checks the correctness of a variable assignment if it was already declared*/}\n  ```\n\n- Function called to check if a variable is already declared (utility function)\n\n  ```c\n  int isVariableDeclared(char *variableName) {/*Returns variable index if the variable is found in the symbol table and returns -1 otherwise*/}\n  ```\n\n- Function called when a variable is used\n\n  ```c language\n  nodeType *id(char *s) {/*calls isVariable declared before returning a pointer for this variable*/}\n  ```\n\n- Function called when an operation is used\n\n  ```c\n  nodeType *opr(int oper, int nops, ...) {/*returns a pointer to operation struct containing the operator number of operands and these operands. This struct is then used for code generation as it contains all the required info to be able to generate code*/}\n  ```\n\n- Function called when a method is defined\n\n  ```c\n  nodeType *defineMethod(char *methodName, nodeType* statements) {/*defines a new function, counts its arguements and add it to the methods symbol table, then calls opr() to generate the struct that will be used for code generation, of course all of this logic is done after checking if it wasn't already defined, so it calls isMethodDeclared()*/}\n  ```\n\n- Function called for defining method arguments\n\n  ```c\n  void addOperand(char *variableName) {/*Adds method arguments to the symbol table*/}\n  ```\n\n- Function called to check if a method is already declared (utility function)\n\n  ```c\n  int isMethodDeclared(char *methodleName) {/*Returns variable index if the variable is found in the symbol table and returns -1 otherwise*/}\n  ```\n\n- Function called to generate quadruples\n\n  ```c\n  int ex(nodeType *p) {/*This function convers the struct generated at the opr function and pushed to the stack into an assembly-like code called quadruples*/}\n  ```\n\n***\n\n### Detecting Errors\n\n*When errors are detected, error messages are displayed to the programmer*\n\n- Example of syntax error\n\n  ```python\n  2s = 10;\n  ```\n\n  *This gives a syntax error since variables are not allowed to begin with numbers*\n\n- Semantic errors such as using undeclared variable, type mismatch, etc. are detected and errors are displayed for the programmer. For example:\n\n  ```python\n  x = 1;\n  a = \"test\";\n  x = a;\n  ```\n\n  *This gives a semantic error since x is of type integer and a is of type string and you are trying to assign the value of a to x*\n\n***\n\n### Quadruples\n\n*As mentioned above the function`ex(nodeType* p)` is used to convert code written in this language to quadruples that are assembly-like language, the following table shows the used quadruples and their meaning:*\n\n| Quadruple |                           Meaning                            |\n| :-------: | :----------------------------------------------------------: |\n|   Push    |                  Push the value into stack                   |\n|    Pop    |                  Pop value out of the Stack                  |\n|    ret    |        It is called to return from method declaration        |\n|   call    |          It is called to call a predefined function          |\n|   Lxxx    | It is label added in the code to indicate where to jump to in loops \u003cbr /\u003eand conditional statements \u003cbr /\u003e(x: represents a placeholder for a number) |\n|    jnz    |          To jump if last operation is not equals 0           |\n|    jz     | To jump if last operation equals 0\u003cbr /\u003e(for example, if comparison result is true) |\n|    jmp    |                      Unconditional jump                      |\n|   print   |                        Print variable                        |\n|    neg    |             To invert, (multiply by negative 1)              |\n|    add    |                        Add 2 operands                        |\n|    sub    |                     Subtract 2 operands                      |\n|    mul    |                     Multiply 2 operands                      |\n|    div    |                      Divide 2 operands                       |\n|  compEQ   |         Compare two operands and check if equal (==)         |\n|  compLT   | Compare two operands and check if first operand less \u003cbr /\u003ethan l the  second operand (\u003c) |\n|  compGT   | Compare two operands and check if first operand\u003cbr /\u003e greater than the second operand (\u003e) |\n|  compAND  |  Compare two boolean operands and check they are both true   |\n|  compOR   |          Check if one of 2 boolean operands is true          |\n|  compGE   | Compare two operands and check if first operand greater than \u003cbr /\u003eor equal  the second operand (\u003e=) |\n|  compLE   | Compare two operands and check if first operand less than \u003cbr /\u003eor equal  the second operand (\u003c=) |\n|  compNE   | Compare two operands and check if first operand not equal \u003cbr /\u003ethe second  operand (!=) |\n\n***\n\n### GUI\n\n![](./TestCases-Screenshots/if.PNG)\n\n*The GUI consists of the following:*\n\n1. Text editor\n2. Symbol table viewer\n3. Quadruples viewer\n4. Errors Viewer (the rectangle at the bottom, this example has no errors so nothing is displayed)\n5. A button `run` to compile and convert your code to quadruples\n6. A button `import` to import a file of code from your file system\n7. An exit button\n\n***\n\n### The project team\n\n- [Rohan More (122cs0080)](https://github.com/morerohan0037)\n- [Nitish Kumar (122cs0070)](https://github.com/TechKumarNitish)\n\n***\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechkumarnitish%2Fneurocode-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftechkumarnitish%2Fneurocode-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechkumarnitish%2Fneurocode-compiler/lists"}