|
| 1 | +# **TINY Language Parser** |
| 2 | + |
| 3 | +This project implements a **Parser** for the **TINY Language**, built on top of the lexical analysis provided by the `Scanner`. The parser transforms the tokens produced by the scanner into a **Syntax Tree**, which represents the program's structure for further processing, such as semantic analysis or code generation. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## **Overview** |
| 8 | + |
| 9 | +The TINY Language Parser is designed to process source code written in the TINY language. It reads a sequence of tokens generated by the scanner and constructs a **Syntax Tree** that conforms to the grammar of the language. |
| 10 | + |
| 11 | +### **Features** |
| 12 | + |
| 13 | +- Modular architecture with clear separation between the **Scanner** and the **Parser**. |
| 14 | +- Constructs a **Syntax Tree** for further processing. |
| 15 | +- Error handling for syntax errors in the input program. |
| 16 | +- Easily extendable for additional grammar rules and constructs. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## **Project Structure** |
| 21 | + |
| 22 | +``` |
| 23 | +. |
| 24 | +├── Makefile # Build system for compiling the parser and scanner |
| 25 | +├── README.md # Documentation for the parser |
| 26 | +├── parser/ |
| 27 | +│ ├── include/ |
| 28 | +│ │ ├── parser.hpp # Header for the parser |
| 29 | +│ │ └── syntax_tree.hpp # Header for the syntax tree representation |
| 30 | +│ └── src/ |
| 31 | +│ ├── parser.cpp # Implementation of the parser |
| 32 | +│ └── syntax_tree.cpp # Implementation of the syntax tree |
| 33 | +└── scanner/ |
| 34 | + ├── include/ |
| 35 | + │ ├── scanner.hpp # Header for the scanner |
| 36 | + │ ├── token.hpp # Header for tokens |
| 37 | + │ └── token_stream_builder.hpp # Header for managing tokens |
| 38 | + └── src/ |
| 39 | + ├── scanner.cpp # Implementation of the scanner |
| 40 | + ├── token.cpp # Implementation of the token class |
| 41 | + └── token_stream_builder.cpp # Implementation of the token stream builder |
| 42 | +``` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## **How It Works** |
| 47 | + |
| 48 | +1. **Scanner Phase**: |
| 49 | + The scanner reads the input source code and converts it into a stream of tokens. These tokens represent individual lexical elements such as keywords, identifiers, operators, and literals. |
| 50 | + |
| 51 | +2. **Parser Phase**: |
| 52 | + The parser consumes the token stream and applies the grammar rules of the TINY language to construct a **Syntax Tree**. |
| 53 | + |
| 54 | +3. **Syntax Tree**: |
| 55 | + The syntax tree represents the hierarchical structure of the program. Each node corresponds to a grammar rule or a terminal token. |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## **Installation and Usage** |
| 60 | + |
| 61 | +### **Build Instructions** |
| 62 | + |
| 63 | +1. Clone the repository: |
| 64 | + |
| 65 | + ```bash |
| 66 | + git clone https://github.com/OrcaLinux/Design-of-Compilers.git |
| 67 | + cd Design-of-Compilers/parser |
| 68 | + ``` |
| 69 | + |
| 70 | +2. Compile the project: |
| 71 | + ```bash |
| 72 | + make |
| 73 | + ``` |
| 74 | + |
| 75 | +### **Run the Parser** |
| 76 | + |
| 77 | +To parse a TINY language program, run the following command: |
| 78 | + |
| 79 | +```bash |
| 80 | +./parser < input_file.tiny |
| 81 | +``` |
| 82 | + |
| 83 | +Replace `input_file.tiny` with the path to your TINY language source code. |
| 84 | + |
| 85 | +### **Example Input** |
| 86 | + |
| 87 | +Sample TINY language program: |
| 88 | + |
| 89 | +```tiny |
| 90 | +if x < 10 then |
| 91 | + write x; |
| 92 | +end |
| 93 | +``` |
| 94 | +
|
| 95 | +### **Output** |
| 96 | +
|
| 97 | +The parser will generate a syntax tree representation of the program: |
| 98 | +
|
| 99 | +``` |
| 100 | +IF |
| 101 | +├── CONDITION: x < 10 |
| 102 | +└── THEN: |
| 103 | + └── WRITE x |
| 104 | +``` |
| 105 | +
|
| 106 | +--- |
| 107 | +
|
| 108 | +## **Development Details** |
| 109 | +
|
| 110 | +### **Parser Implementation** |
| 111 | +
|
| 112 | +- The parser uses a recursive descent approach to process tokens. |
| 113 | +- Each grammar rule is implemented as a separate function in `parser.cpp`. |
| 114 | +
|
| 115 | +### **Syntax Tree Representation** |
| 116 | +
|
| 117 | +- The `SyntaxTree` class represents the hierarchical structure of the parsed program. |
| 118 | +- Each node in the tree corresponds to a grammar rule or a terminal token. |
| 119 | +
|
| 120 | +--- |
| 121 | +
|
| 122 | +## **Extending the Parser** |
| 123 | +
|
| 124 | +To add support for new grammar constructs: |
| 125 | +
|
| 126 | +1. Update the grammar rules in `parser.cpp`. |
| 127 | +2. Extend the `SyntaxTree` implementation in `syntax_tree.cpp`. |
| 128 | +
|
| 129 | +--- |
| 130 | +
|
| 131 | +## **Contributing** |
| 132 | +
|
| 133 | +Contributions are welcome! |
| 134 | +To contribute: |
| 135 | +
|
| 136 | +1. Fork the repository. |
| 137 | +2. Create a feature branch. |
| 138 | +3. Commit your changes and open a pull request. |
| 139 | +
|
| 140 | +--- |
| 141 | +
|
| 142 | +## **License** |
| 143 | +
|
| 144 | +This project is licensed under the MIT License. See the `LICENSE` file for details. |
0 commit comments