Skip to content

l4z0-space/CUI-LANGUAGE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CUI-LANGUAGE

Simple programming language implemented (handmade :p) in C++

This language is open to extensions and adding more features. The goal is to make a dead-simple programming language comprehensible for those new to programming.

To run the language

  1. Go to the same directory as main.cpp
  2. g++ -std=c++11 main.cpp -o exe.out && ./exe.out 'example_programs/filename.cui'

Or

  1. Go to the same directory as main.cpp
  2. ./cui.sh 'example_programs/filename.cui'

Features

  1. Variable declarations
  2. Conditional statements (if-else)
  3. Loops (while)
  4. Data types: strings and floats
  5. Prompt input directly from user interactively
  6. Global State

Other

  1. Recursive Descent Parsing Algorithm
  2. Context Free Grammar translation scheme in Parser.py

Main Components

Scanner: Lexical Analysis

  • Reads the input file char by char and forms the tokens
  • Reads the input file char by char and forms the tokens

Parser: Syntax Analysis

  • Gets the tokens generated from the Scanner, and forms the Syntax Tree

Syntax tree: Semantic Analysis

  • Semantic checks are done after successful Parsing.
  • Excetuion of he statements and expressions is made while Syntax Tree traversal.

Language Grammar

STATEMENT -> EXPRESSION_STATEMENT | IF_STATEMENT | WHILE_STATEMENT | PRINT_STATEMENT | INITIALIZATION_STATEMENT

EXPRESSION_STATEMENT -> IDENTIFIER "=" TERM;

IF_STATEMENT -> "if" (TERM) "ifbody" STATEMENT_LIST "else" "do" TERM "endif" ; | "if" (TERM) "ifbody" STATEMENT_LIST "endif" ;

WHILE_STATEMENT -> "while" (TERM) "whilebody" STATEMENT_LIST "endwhile" ;

PRINT_STATEMENT -> "print(TERM)" ;

INITIALIZATION_STATEMENT -> "create" IDENTIFIER "=" TERM ;

TERM -> FACTOR ( ( "+" | "-" ) FACTOR )* ;

FACTOR -> UNARY ( ( "" | "/" | "%" ) UNARY ) ;

UNARY -> ( "not" | "-" ) LITERAL ;

LITERAL -> NUMBER | STRING ;

VALID_CHAR -> "a" ... "z" | "A" ... "Z" | "_" ;

DIGIT -> "0" ... "9" ;

NUMBER -> DIGIT + ( "." (DIGIT)+ )? ;

STRING -> """ """ // Any chars except '' surroundedby ";

IDENTIFIER -> VALID_CHAR | (VALID_CHAR | DIGIT) * ;