semantic-analyzer-rs
所属分类:自动编程
开发工具:Rust
文件大小:0KB
下载次数:0
上传日期:2023-08-02 13:27:37
上 传 者:
sh-1993
说明: 用于以Rust编写的编译器的语义分析器库,用于编程语言的语义分析,并为任何后端生成代码,
(Semantic analyzer library for compilers written in Rust for semantic analysis of programming languages, with code generation for any backend,)
文件列表:
Cargo.toml (828, 2023-12-16)
LICENSE (1071, 2023-12-16)
src/ (0, 2023-12-16)
src/ast.rs (30998, 2023-12-16)
src/lib.rs (1097, 2023-12-16)
src/semantic.rs (61868, 2023-12-16)
src/types/ (0, 2023-12-16)
src/types/block_state.rs (17257, 2023-12-16)
src/types/condition.rs (8862, 2023-12-16)
src/types/error.rs (2249, 2023-12-16)
src/types/expression.rs (6009, 2023-12-16)
src/types/mod.rs (14202, 2023-12-16)
src/types/semantic.rs (16451, 2023-12-16)
src/types/types.rs (8187, 2023-12-16)
tests/ (0, 2023-12-16)
tests/binding_tests.rs (7101, 2023-12-16)
tests/codec_tests.rs (13697, 2023-12-16)
tests/const_tests.rs (7375, 2023-12-16)
tests/errors_tests.rs (869, 2023-12-16)
tests/expressions_tests.rs (43649, 2023-12-16)
tests/function_call_tests.rs (4591, 2023-12-16)
tests/function_declaration_tests.rs (3503, 2023-12-16)
tests/if_tests.rs (61208, 2023-12-16)
tests/import_tests.rs (475, 2023-12-16)
tests/let_binding_tests.rs (6306, 2023-12-16)
tests/loop_tests.rs (17470, 2023-12-16)
tests/main_tests.rs (24227, 2023-12-16)
tests/state_tests.rs (10485, 2023-12-16)
tests/types_tests.rs (13709, 2023-12-16)
tests/utils/ (0, 2023-12-16)
tests/utils/mod.rs (1024, 2023-12-16)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Lints](https://github.com/mrLSD/z-rose/actions/workflows/lints.yml/badge.svg)](https://github.com/mrLSD/z-rose/actions/workflows/lints.yml)
[![Tests](https://github.com/mrLSD/z-rose/actions/workflows/tests.yml/badge.svg)](https://github.com/mrLSD/z-rose/actions/workflows/tests.yml)
[![Crates.io version](https://img.shields.io/crates/v/semantic-analyzer.svg?style=flat-square)](https://crates.io/crates/semantic-analyzer)
[![codecov](https://codecov.io/gh/mrLSD/semantic-analyzer-rs/graph/badge.svg?token=ZQ8FCYSSZX)](https://codecov.io/gh/mrLSD/semantic-analyzer-rs)
mrLSD/semantic-analyzer-rs
Semantic analyzer is an open source semantic analyzer for programming languages
that makes it easy to build your own efficient compilers.
## What is the library for and what tasks does it solve
Creating a compilers for a programming language is process that involves several key
stages. Most commonly it is:
**Lexical Analysis (Lexer)**: This stage involves breaking down the input stream
of characters into a series of tokens. Tokens are the atomic elements of the programming language, such as identifiers, keywords, operators, etc.
**Syntax Analysis (Parsing)**: At this stage, the tokens obtained in the previous
stage are grouped according to the grammar rules of the programming language. The result
of this process is an **Abstract Syntax Tree (AST)**, which represents a hierarchical structure of the code.
**Semantic Analysis**: This stage involves checking the semantic correctness of the code. This can include
type checking, scope verification of variables, etc.
**Intermediate Code Optimization**: At this stage, the compiler tries to improve the intermediate representation of the code to make it more efficient.
This can include dead code elimination, expression simplification, etc.
**Code Generation**: This is the final stage where the compiler transforms the optimized intermediate representation (IR) into
machine code specific to the target architecture.
This library represent **Semantic Analysis** stage.
### Features
**Name Binding and Scope Checking**: The analyzer verifies that all variables, constants, functions are declared before they're used,
and that they're used within their scope. It also checks for name collisions, where variables, constants, functions, types in the same scope have the same name.
**Checking Function Calls**: The analyzer verifies that functions are called with the number of parameters and that the type of
arguments matches the type expected by the function.
**Scope Rules**: Checks that variables, functions, constants, types are used within their scope, and available in the visibility scope.
**Type Checking**: The analyzer checks that operations are performed on compatible types for expressions, functions, constant, bindings.
For operations in expressions. It is the process of verifying that the types of expressions are consistent with their usage in the context.
**Flow Control Checking**: The analyzer checks that the control flow statements (if-else, loop, return, break, continue) are used correctly.
Supported condition expressions and condition expression correctness check.
**Building the Symbol Table**: For analyzing used the symbol table as data structure used by the semantic analyzer to keep track of
symbols (variables, functions, constants) in the source code. Each entry in the symbol table contains the symbol's name, type, and scope related for block state, and other relevant information.
### Semantic State Tree
The result of executing and passing stages of the semantic analyzer is: **Semantic State Tree**.
This can be used for Intermediate Code Generation, for further passes
semantic tree optimizations, linting, backend codegen (like LLVM) to target machine.
#### Structure of Semantic State Tree
- **blocks state** and related block state child branches. It's a basic
entity for scopes: variables, blocks (function, if, loop).
Especially it makes sense for expressions. This allows you to granularly separate the visibility scope
and its visibility limits. In particular - all child elements can access parent elements.
However, parent elements cannot access child elements, which effectively limits the visibility scope and entity usage.
- **variables state**: block state entity, contains properties of variable in current
state like: name, type, mutability, allocation, mallocation.
- **inner variables state**: block state entity, contains inner variables names.
It's useful for Intermediate Representation for codegen backends like LLVM.
Where shadowed name variables should have different inner names. It means inner variables
always unique.
- labels state: block state entity, that contains all information about control flow labels.
- **Global state**: contains global state of constants, declared functions and types.
- **State entity**: contains:
- Global State
- Errors results
- Semantic tree results
All of that source data, that can be used for Intermediate Representation for next optimizations and compilers codegen.
### Subset of programming languages
The input parameter for the analyzer is a predefined
AST (abstract syntax tree). As a library for building AST and the only dependency
used [nom_locate](https://github.com/fflorent/nom_locate) - which allows getting
all the necessary information about the source code, for further semantic analysis
and generating relevant and informative error messages. Currently
decided that the AST is a fixed structure because it is a fundamental
element that defines the lexical representation of a programming language.
On the other hand, it allows you to implement any subset of the programming language that matches
syntax tree. It also implies a subset of lexical representations from which an AST can be generated
that meets the initial requirements of the semantic analyzer. As a library for lexical
analysis and source code parsing, it is recommended to use: [nom is a parser combinators library](https://github.com/rust-bakery/nom).
AST displays the **Turing complete** programming language and contains all the necessary elements for this.
## Examples
- There is the example implementation separate project [ Toy Codegen](https://github.com/mrLSD/toy-codegen).
The project uses the `SemanticStack` results and converts them into **Code Generation** logic. Which clearly shows the
possibilities of using the results of the `semantic-analyzer-rs` `SemanticStackContext` results. LLVM is used as a
backend, [inkwell](https://github.com/TheDan64/inkwell) as a library for LLVM codegen, and compiled into an executable
program. The source of data is the AST structure itself.
## MIT [LICENSE](LICENSE)
近期下载者:
相关文件:
收藏者: