Skip to content

Simplified compiler for the Pascal language built with Python3

License

Notifications You must be signed in to change notification settings

effeix/PascalSimplifiedCompiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

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/):

$ 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
  • Lexical Analysis
  • Syntactic Analysis
  • Addition / Subtraction
  • Multiplication / Division
  • Comments
  • Syntatic Errors
  • Parenthesis
  • Unary Operators
  • Abstract Syntax Tree
  • Program Flow
  • Keywords
  • Variables
  • Symbol Table
  • Print
  • Boolean Operators
  • Conditional Statements
  • Loops
  • Read
  • Variable Declaration
  • Types
  • Type checking
  • Semantic Errors
  • Functions
  • Function Arguments
  • Variable Scopes
  • Code Generation !

EBNF

An EBNF (Extended Backus-Naur Form) is a sequence of statements describing a 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).

Below is the EBNF for this compiler:

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

About

Simplified compiler for the Pascal language built with Python3

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages