REAL


  • Home

  • About

  • Categories

  • Project

  • Archives

  • Tags

  • Search

Set drawing language

Motivation

Drawing a venn diagram isn't that hard.
However, drawing a venn diagram from a formula is complex.
Therefore, this project supports a language that can draw how venn diagram looks like.
This project was made for 3 set but it can be extended to more sets.

Language specification

This langauge consists of the following properties.
  • Basic sets
  • Basic sets represents basic sets for drawing sets.
    In the example below, S0,S1,S2,U are basic sets.
  • Union operation
  • A union B means elements are inside of A or B.
    In this language, union represented as +.
  • Intersection operation
  • A intersect B means elements are inside of A and B.
    In this language, intersection represented as *.
  • Difference operation
  • A difference B means elements are inside of A but not B.
    In this language, difference represented as -.
  • Parenthesis
  • Parenthesis to make a priority between operations.
  • Variable declaration
  • This language support variable declaration.
    There is no re-delcaration for any variables.
  • Print statement
  • Print statement is a only statement that printout the result from the program.
  • Comment
  • Comments will be ignored.

    Implementation

    This language consists of three compilation phases.
    1. Scaner, 2.Parser 3.Evaluation. It doesn't convert result to the binary or bytecode because it doesn't have any interaction after it produced.
    To parse language, it uses a top-down parser.
    Language consist of following rules.
    Notice that as it shows, language has following priority.
    1. Intersection applied first.
    2. Union applied second.
    3. Difference apllied last.
    In other words, A - B * C + D will be A - ((B * C) + D).

    $ \begin{array}{lcl} Program & ::= & Statement[\n]Program \\ & ::= & Statement \\ Statement & ::= & Declaration \\ & ::= & Print \\ & ::= & Comment \\ & ::= & λ \\ Declaration & ::= & Var [=] Exp\_Diff \\ Print & ::= & [\text{print1}] Exp\_Diff \\ & ::= & [\text{print2}] Exp\_Diff \\ Exp\_Diff & ::= & Exp\_Diff [-] Exp\_Uni \\ & ::= & Exp\_Uni \\ Exp\_Uni & ::= & Exp\_Uni [+] Exp\_Inter \\ & ::= & Exp\_Inter \\ Exp\_Inter & ::= & Exp\_PT [*] Exp\_Inter \\ & ::= & Exp\_PT \\ Exp\_PT & ::= & [(] Exp\_Diff [)] \\ & ::= & Var \\ Var & ::= & [a-zA-Z][a-zA-Z0-9]* \\ Comment & ::= & [/][/][\text{Any character except }\n]* \\ \end{array} $

    Scanner

    Scanner will produce a list of tokens from a code.
    Possible tokens types are as follows.
    Variable, Operation, Print, Parenthesis, EndOfLine.
    In the scanning process, it will ignore comments and empty lines.

    Parser and evaluation

    Parser will produce an AST from a list of tokens.
    AST's node will be one of followings.
    1. Operation, it will be a regular operation node to compute.
    They are one of followings, +(union), -(difference), *(intersection), and =(assignment).
    In the parsing, it will read out the parenthesis and make a correct orders.
    Notice that union and difference are left-associative and all others are right-associative.
    2. Print, it's only system call that this langauge has. It will print out result to the canvas.
    3. Varaible, this node means an object for a variable.
    If it is left of the assignment operation, it will mean a name of variable.
    If it is right of the assignment operation or some where other, it will mean a value of variable.
    4. State, this node doesn't do anything but it chains statements.
    In the evaluation stage, it follows ast and evaulates.

    Sample program

    • Post author: Programelot
    • Post link: https://programelot.github.io/projects/SetDL/
    • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
    © 2024 Programelot
    Powered by Jekyll
    Theme - NexT.Muse