Dorapower / Py-PL0-Interpreter

A PL/0 Interpreter implemented with Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Py-PL0-Interpreter

A PL/0 Interpreter implemented in Python

Grammar

The grammar is based on wikipedia's PL/0 page.

program = block "." ;

block = [ "const" ident "=" number {"," ident "=" number} ";"]
        [ "var" ident {"," ident} ";"]
        { "procedure" ident ";" block ";" } statement ;

statement = [ ident ":=" expression | "call" ident 
              | "?" ident | "!" expression 
              | "begin" statement {";" statement } "end" 
              | "if" condition "then" statement 
              | "while" condition "do" statement ];

condition = "odd" expression |
            expression ("="|"#"|"<"|"<="|">"|">=") expression ;

expression = [ "+"|"-"] term { ("+"|"-") term};

term = factor {("*"|"/") factor};

factor = ident | number | "(" expression ")";

Keywords

Note: Keywords are case-insensitive in this implementation.

  • const: declares constants
  • var: declares variables
  • procedure: declares a procedure
  • call: calls a procedure
  • begin: composite statements
  • end: composite statements ends
  • if: conditional statement condition
  • then: conditional statement body
  • while: loop statement condition
  • do: loop statement body
  • odd: condition operator, returns true if the expression is odd

Operators

  • +, -: unary operators
  • +, -, *, /: arithmetic operators
  • =, #, <, <=, >, >=: comparison operators
  • :=: assignment operator
  • ?: input operator
  • !: output operator
  • ;: statement separator
  • ,: variable separator
  • (, ): parenthesis
  • .: program end

Features

  • Lexer: converts source code into tokens
  • Parser: converts tokens into an AST
  • ASTInterpreter: interprets the AST
  • IR Generator: generates an IR from the AST
  • IR Interpreter: interprets the IR

Use the Interpreter

TODO

Test

To test a certain program, either use pytest to run the test file (not covering a lot) or run the following command:

python3.11 <path-to-program> <path-to-input>

and the output will be printed to the console.

About

A PL/0 Interpreter implemented with Python

License:Apache License 2.0


Languages

Language:Python 100.0%