[PYTHON] Let's make a simple language with PLY 1

Introduction

background

What is PLY

Preparation

Install PLY

$ pip install ply

Decide what to implement this time

Implementation

Lexical analysis

lex.py


import ply.lex as lex

tokens = (
  # Literals
  'ID',

  # Function
  'PUT', 'EXIT',
)

# Identifiers at var
t_ID = r'(?!put|exit)[A-Za-z_][A-Za-z0-9_]*'

# Function
t_PUT = r'put'
t_EXIT = r'exit'
# space is ignore

# space and tab is ignore
t_ignore = ' \t'

# comment  
t_ignore_COMMENT = r'\#.*'

# error handling
def t_error(t):
    print("Illegal characters'%s'" % t.value[0])
    t.lexer.skip(1)

#
lex.lex(debug=0)

Parsing

The code created below is shown

yacc.py


import ply.yacc as yacc
from lex import tokens
import sys

names = {}

# var (id)
def p_expr_id(p):
    'expr : ID'
    try:
        p[0] = names[p[1]]
    except LookupError:
        print('Undefine var name %s' %p)
        p[0] = 0

# exit function
def p_exit(p):
    'expr : EXIT'   
    print('See You!')
    sys.exit()

# empty
def p_empty(p):
    'empty :'
    pass

# syntax error
def p_error(p):
    print ('Syntax error in input %s' %p)

parser = yacc.yacc()

# Debug
def parse(data, debug=0):  
    return yacc.parse(data, debug=debug)
    
if __name__ == '__main__':  
    while True:
        try:
            s = input('>>> ')
        except EOFError:
            break
        if not s:
            continue
        result = parser.parse(s)
        print (result)

Try to move

Actually, I defined a variable, but I can't do anything at the moment All you can do is try the exit function

Execution

$ python yacc.py
>>> exit
See You!
$

in conclusion

Somehow it became a screen like that and I got out with exit The explanation has become rough towards the end

Referenced

I made an original language "PPAPScript" that imaged PPAP (Penpainappoappopen) with Python, [ryo-ma](http: / /qiita.com/ryo-ma).

Recommended Posts

Let's make a simple language with PLY 1
Let's make a simple game with Python 3 and iPhone
Let's make a GUI with python.
Let's make a breakout with wxPython
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Make a simple pixel art generator with Flask
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Let's make a Discord Bot.
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
[Let's play with Python] Make a household account book
Let's make Othello with wxPython
Let's make dice with tkinter
Let's make dependency management with pip a little easier
Make a fortune with Python
Let's make a Mac app with Tkinter and py2app
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
Let's make a rock-paper-scissors game
What is God? Make a simple chatbot with python
[Super easy] Let's make a LINE BOT with Python.
Make a fire with kdeplot
Let's make a websocket client with Python. (Access token authentication)
[Practice] Make a Watson app with Python! # 3 [Natural language classification]
Make a LINE bot with GoogleAppEngine / py. Simple naked version
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Let's make a number guessing game in your own language!
Let's make a diagram that can be clicked with IPython
Let's make a remote rumba [Hardware]
Let's make a remote rumba [Software]
Make a sound with Jupyter notebook
Let's make a spot sale service 2
Let's make a spot sale service 1
Let's make Othello AI with Chainer-Part 1-
Creating a simple app with flask
Make a recommender system with python
Make a filter with a django template
Let's make Othello AI with Chainer-Part 2-
Make a model iterator with PySide
Make a nice graph with plotly
Let's make a spot sale service 3
Let's make a WEB application for phone book with flask Part 1
Rubyist tried to make a simple API with Python + bottle + MySQL
Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Let's make a WEB application for phone book with flask Part 2
Try to make a web service-like guy with 3D markup language
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Let's make a web chat using WebSocket with AWS serverless (Python)!
Creating a simple PowerPoint file with Python
A simple RSS reader made with Django
Make a video player with PySimpleGUI + OpenCV
Let's write a simple DC current solver
Let's create a free group with Python
Make a rare gacha simulator with Flask