WRITING A MINI LANGUAGE [THE LEXER HAT ]

WRITING A MINI LANGUAGE [THE LEXER  HAT ]:

In the previous blog we discussed what lexers do and what is their purpose . In short, A lexer performs a process which involves separating a stream of characters(or string) into different categories.

 

Lets take the example of the ‘sorting hat’ from harry potter to understand lexers even better .

The Sorting Hat is a sentient hat at Hogwarts that magically determines which of the four school Houses each new student belongs most to. These four Houses are GryffindorHufflepuffRavenclaw, and Slytherin.(source : http://harrypotter.wikia.com/wiki/Sorting_Hat)

 

The lexer hat goes something like this :

“Hmmmmmmm, what is this? . it starts with a…………..yes a letter , this must be a………variable!”

harry

“what do I see in here , hmmm a number at the start , this must be …….a variable?. No! A variable starts with an alphabet . this must be a ……….number!”

herm.png

‘Oh! We have something interesting in here , it starts with a quote . what starts with a quote? , this must be under the category of strings , yes a string!’

ron

The ‘magical  lexer hat’ does what a lexer does . The lexer examines each part of the string and categorizes them , for example: string, variables , numbers , assignments etc.

 

Here is the code for a simple lexer : https://notabug.org/Richita/ILOVECODE/src/master/lexerhat

 

What this lexer can do :

  1. It can recognize variables , numbers, strings
  2. It can recognize operators, for example : plus , minus, addition , subtraction
  3. It categorizes the assignment operator (“=”) as an assignment operator , because the lexer hat considers  it is special .

What the lexer can not do :

  1. It cant recognize string with spaces in between
  2. It cant recognize any other function other than the ‘print’ function .
  3. And a lot more

You can go ahead and modify the program to make it your own and make it do more complex stuff like -1.manipulating strings with spaces 2.adding more functions 3.make your own creative functions and commands to feed to the lexer so that it can manipulate it as per your command.

 

Next time we will discuss parsers , and learn how parsers use the data from lexers .

 

Happy Coding!

##########################################################################

License: CC0 1.0 Universal(CC0 1.0)

Credit : richita  ankita  mn

##########################################################################

 

 

 

Advertisements

WRITING A MINI LANGUAGE [ ABOUT LEXERS ]

WRITING A MINI LANGUAGE [ ABOUT LEXERS – PART 1 ] :

Hi, welcome to ILOVECODE .  I am not an expert and I am still learning about lexers and parsers . I started learning for fun and im still experimenting . If you want to make a language by using your own lexer and parser then first you have to know about what your going to work on . so lets start with lexers .

Whats an lexer ?

A lexer performs a process which involves separating a stream of characters into different categories.  By stream of characters I mean a string , for example : “hat is red”

Now this is how the lexer sees the string :

noun = hat

is = linking verb

noun = colour

 

lexer takes each part of the string and stores them under a category . The output of the lexer is a list of tokens . So the output of of lexer for the above string is :

lexer output = [(“noun”,hat) , (“linking verb”, “is”) , (“noun, “colour”)]

 

so now you have an idea what a lexer does . lets take a practical example this time .

For example : “name  = “Pam” ”

Lexer output:

[(“variable”,”name”),(“assignment”,”=”),(“String”,”Pam”)]

 

LEXER

Now that we’ve discussed about lexers , in the next part we’ll make a mini lexer and you can experiment with it . I am going to use python for it

Just to give you an idea of how lexer works, when it sees stuff between to quotes, it stores that stuff as string .  I’ll discuss how the lexer works in the next blog post in detail. After lexers we’ll move to parsers . parsers use the information from the lexer to give an desired output .

Until next blog, happy coding  . Have a nice day .

 

if you want to look at the recent lexer I am working on go here .

###############################################

# license: creative commons cc0 1.0 (public domain)
# http://creativecommons.org/publicdomain/zero/1.0/
# credit :ankitaroy,mn,richitaroy

################################################

Space Wars game in python

Space Wars game in python:

pic2
Playing the game

Hey guys , I made a game named Space Wars in python  .  Its been 4 months since I finished making the game and I have probably forgotten some of the code (actually most of the code)

But I’m sure I remembered the logic behind the game(the algorithms and stuff) and I’ve recently added a high score feature which was not there in the original code . The high score feature is somewhat problematic (ill explain it to you later…..)

Game rules :

  • Press right and left arrow keys to move the rocket left and right respectively
  • Press space bar to fire , if you shoot the enemy ship( the pink pig thingy ship, I’m not good at explaining stuff, pardon me ) successfully then you earn a score.
  • If you get crashed with an asteroid (the brown rock), the game is over, however, you can play the game again by pressing play again button. The asteroids fall vertically downward

Link for the code and pictures :

https://notabug.org/Richita/ILOVECODE/src/master/spacewars

 

The intro of the game
The intro of the game
Game is over if you crash with an asteroid
Game is over if you crash with an asteroid

 

Press " p" to pause
Press ” p” to pause
Game rules
Game rules

The problem with the high score feature is the variable “high_score” which  I used to score the high score in the game resets itself to zero every time I quit and run the program ( /game) again . Therefore you get to see the high score as long as you play the game without quitting the program (i.e  , if you get crashed with an asteroid, you play the game again by pressing play again button , then high score will  work fine).

 

Thanks for reading the blog and I hope you have a great day 🙂

Happy Coding!!

Openhands Game in Python

I took the idea for this game from  Liyan Chua’s Blog  .She inspires me a lot , and check her blog . I did the code in python. I thought of trying this code because it is interesting .

 

Playing the game
Playing the game

Game rules :

The Player chooses the combination of two hands he/she has. For example : CC(closed hand, closed hand) ,   OC (Open Hand , Close hand . Or vice versa) and OO(Open Hand, Open Hand)  .

The predictor not only chooses the hand combination but also a number of open hands(any number , but its maximum value should be 4 , not more than that otherwise you will never win . ) .  For example : CC1(closed hand, closed hand,1) ,OC3 (Open Hand , Close hand . Or vice versa) and OO2(Open Hand, Open Hand) etc .

How it works :

There are two players : You and computer . You will be the first predictor . you will then alternately be the player or predictor.

For Example :

Predictor choice : CC2

Player choice : OO

In the above case  you will win , because CC+OO(predictor hands, player hands ) has two open hands .

If you don’t win the game will continue .

 if you win the game , it will exit

the code : https://pastebin.com/ymQ6vdi3

Have a nice day and happy coding .

 

SJ – Command Line Program

 

Sj is shell program in python . By adding a batch file (for example sj.bat ) to the folder where your cmd.exe file is present, you can execute sj from anywhere in shell  by just typing the name of the batch file . Content of my batch file is here .

Other than adding a batch file ,you can just execute sj code in python .

trying out the find (with and without name ) command
trying out the find (with and without name ) command

Sj commands :

find [file name or extension] –> to find a file with a definite name or extension.

dr [directory]–>  to show the or subdirectory/folders/files in a directory . for example : dr Lib”

dr [directory]-[subdirectory]–> to show the files/folders in subdirectories with in the mentioned directory

n dr [directory]-[subdirectory]–>  to show the firsr n subdirectory/folders/files in a directory . for example : 6 dr Lib-email”

ln  [directory]-[subdirectory]–>  to show the firsr n subdirectory/folders/files in a directory from the last . for example : l6 dr Lib-email

find w/-[name]–>  to find files with the name in it

find w/o-[name]–>  to find files without the name in it

mkd [folder name]–> to create a folder

copy [file] to [folder] –> copies a file to a folder

echo [text] [text file] –> overrides a text to the text file

echo-append [text] [text file] –>   adds a text to the text file

help –> its typically for me, i forget sj commands , just joking (or am im ? ). it lists the use of all the sj commands .

 

Link for sj  : https://notabug.org/Richita/ILOVECODE/src/master/sj

Thanks for visiting my blog . have a nice day and happy coding .

 

 

Lakshya Foundation, the joy of giving

 

Children gathered at the Lakshya Foundation meet
Children gathered at the Lakshya Foundation meet

28th January 2018 our NGO Lakshya Foundation organised a meet to distribute books and notebooks to children in need .
We sisters (Ankita and Richita) are official volunteers of this Foundation founded by my Father Gobinda Roy and mother Tanusree Roy.
Lakshya Foundation  aims at helping children in need of proper guidance and study materials .

Our team with few children (me and my sister 4th and 5th from right)
Our team with few children (me and my sister 4th and 5th from right)

Members of this foundation helped a lot in organizing this meet today . Me and my sister were engaged in collecting all the report cards and pasting photos of the children, distributed cakes and supervised the children.  It was a busy day but we had fun today 🙂
The word “Lakshya” means goal ; and thus our aim is go help children reach their goals .
The joy of giving is beyond explanation and we sisters are glad to be a part of this non profit organisation.
Thanks for reading this blog and have a nice day.

CB : My First Programming Language

CB : My First Programming Language:

Its been almost a month since I have started developing a programming language in python. It was a great experience as it changed my entire concept towards compilers .

 

CB

I always wanted to design a logo for something 🙂 wohooo !!

What cb language does :

  1. for loop :

Code for “for loop” :

for (1,10,1):

“How are you?” print

end #end is needed to specify the end of the loop

  1. if/elif loop :

Code for “if loop” :

if (6>5):

“How are you?” print

end

else loop is the same but there are no parameters for it, same as python.

4.while loop :

while 1:

“Hello World” print

break  #while loop needs break to mark the end of the loop

you can remove the break, but it will print the statement forever and ever and ever………………10 minutes later……ever and ever and ever…………

  1. assignment operator :

hello = “hello world”

4.taking input from the user using a cbfunction:

user_input = cbinput()

5.converting input into integer:

user_input = cbinput() int

  1. functions :

Code for creating and calling a function:

def minus(a,b):

result = a-b

result return

end # def also needs end

 

a  = call minus(10,5) # I assigned the result in a variable “a” because cb cannot call and print a function at the same time .

a print

  1. Nested loops :

“Enter your number:” print

num = cbinput()int

if (num>30):

“Your number is greater than 30” print

If(num == 50):

“Your number equals 50”

end

end

 

Cb reads a txt file and converts cb code into python code.

Thanks @codeinfig for guiding me through the entire process and solving all the problems with cb (: and thanks for making me understand everything  .

Thanks for reading my blog (: . I hope you all have an awesome day . Cb source code below.

 

CB functions:

https://github.com/RichitaRoy/ILOVECODE/blob/master/cb0.1

CB Parser:

https://github.com/RichitaRoy/ILOVECODE/blob/master/cbParser0.2

###############################################

# license: creative commons cc0 1.0 (public domain)
# http://creativecommons.org/publicdomain/zero/1.0/
# credit :mn,richitaroy,ankitaroy

################################################