Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/effeix/pascalsimplifiedcompiler
Simplified compiler for the Pascal language built with Python3
https://github.com/effeix/pascalsimplifiedcompiler
compiler languages pascal python3
Last synced: 9 days ago
JSON representation
Simplified compiler for the Pascal language built with Python3
- Host: GitHub
- URL: https://github.com/effeix/pascalsimplifiedcompiler
- Owner: effeix
- License: gpl-3.0
- Created: 2018-02-19T19:18:15.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-30T00:08:49.000Z (over 6 years ago)
- Last Synced: 2024-11-07T09:53:26.343Z (about 2 months ago)
- Topics: compiler, languages, pascal, python3
- Language: Python
- Homepage:
- Size: 6.78 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pascal Simplified Compiler
### Usage
You are free to use PSC however you like. Simply clone the repository and run the file ```main.py``` (located inside directory ```src/```):```sh
$ python3 src/main.py PASCAL_FILE i|c
```where ```i``` stands for the interpreted version and ```c``` for the compiled version.
The compiled executable will be generated inside diretory ```bin/```, located in repository root (same level as ```src/```).
##### Dependencies
- Python 3.6.x
- NASM command line tool
- GNU Linker### Features
###### Features marked with "!" are currently being implemented
- [x] Lexical Analysis
- [x] Syntactic Analysis
- [x] Addition / Subtraction
- [x] Multiplication / Division
- [x] Comments
- [x] Syntatic Errors
- [x] Parenthesis
- [x] Unary Operators
- [x] Abstract Syntax Tree
- [x] Program Flow
- [x] Keywords
- [x] Variables
- [x] Symbol Table
- [x] Print
- [x] Boolean Operators
- [x] Conditional Statements
- [x] Loops
- [x] Read
- [x] Variable Declaration
- [x] Types
- [x] Type checking
- [x] Semantic Errors
- [x] Functions
- [x] Function Arguments
- [x] Variable Scopes
- [ ] Code Generation !### EBNF
An [EBNF (Extended Backus-Naur Form)](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) is a sequence of statements describing a [Context-Free Grammar](https://en.wikipedia.org/wiki/Context-free_grammar). It is used to represent a formal language or programming language and, as the name sugests, is an extension to the original [BNF (Backus-Naur Form)](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form).Below is the EBNF for this compiler:
```ebnf
program = "program", identifier, ";", block, ".";
block = ["var", varblock], [funcblock], statements;
var_declaration = identifier, {",", identifier}, ":", type;
varblock = var_declaration, {";", var_declaration};
funcblock = "function", identifier, "(", {var_declaration}, ")", ":", type, ";", block;
statements = "begin", statement, {";", statement}, [";"], "end";
statement = attribution | statements | print | if | while;
if = "if", expression, "then", statement, ["else", statement];
while = "while", expression, "do", statement;
attribution = identifier, ":=", expression | read;
read = "read", "(", ")";
print = "print", "(", expression, ")";
expression = simple_expression, {("<" | ">" | "="), simple_expression};
simple_expression = term, {("+" | "-" | "or"), term};
term = factor, {("*", "/", "and"), factor};
factor = ({"+" | "-" | "not"}, factor) | number | ("(", expression, ")") | identifier | funccall;
funccall = identifier, "(", [expression, {";", expression}], ")";
identifier = letter, {letter | digit | "_" };
number = digit, {digit};
letter = a .. z | A .. Z;
digit = 0 .. 9;
type = "bool" | "integer";
```### Syntactic Diagram
The syntactic diagram is a visual representation of the EBNF, describing the algorithm used by the compiler. If you pay close attention to the code, the similarities between the diagram and the algorithm are clear. Below is the syntactic diagram for this compiler:![Syntactic Diagram](https://i.imgur.com/zgaumZ6.png)