A simple parser for python made using lex yacc / GNU's flex bison.
The stable version currently supports only certain python expressions as stated below. The code for the same is in python-expression-parser
directory. A similarly made python for-loop parser can also be found in the python-loop-parser
directory.
- The following packages are required
- gnu's lex yacc
sudo apt-get install bison flex
- make utility
sudo apt-get install build-essential
- gnu's lex yacc
- go to source folder
cd python-expression-parser
make clean
to remove old stuff lying aroundmake
to build : this will generate the usual filesmake run
to execute or simply run./expression-parser
- go to source folder
cd python-expression-parser
make clean
to remove old stuff lying aroundmake debug
to build with debugging flags. This will generate a lot of extra files. Browse them to know more about them.- run
./debug-expression-parser
-
Program works when input in only a single line.
- It can be made into consecutive multiple line input using
BEGIN <stmt-list> END
. - But then, when a syntax error is encountered,
- i will have to handle the error,
- empty the current stack
- then recover from the error
- and then keep the program going until statements are over
- It can be made into consecutive multiple line input using
-
One program run parses only one statement.
- It can be made for infinite statements.
- That is, continous INPUT_OUTPUT stream until ctrl+c on terminal can be achieved.
- This can be done by removing
return 0;
from the success case. - In this case as well, the current stack needs to be emptied.
- each statement ends with semicolon
-
arithmetic expressions along with brackets
a=5; a=5+24; a=rod; a=5*(4+3);
-
string assignments
var="hello"; var='hello';
-
print statements
print 'hello'; # single quotes handled print "hello"; # double quotes handled
-
keywords cant be identifiers
-
Multiple Statements on a Single Line
var=5; var2=6; var7=23;
-
multi line statements -- a single statement written in multiple lines
var= 2+ \ 3+ \ 4
-
static array initialisation not included
var = [3,3,23]; days = ['Monday', 'Tuesday', 'Wednesday','Thursday', 'Friday']
-
single or multi line comments
-
triple quote print statements - multiline strings
print """hello"""; print '''hello''';
-
typecasting
print "hello" + str(5);
-
shorthand operators not handled
var+=3; var+=2;
- This assignment covers only simple cases.
- Only to be used as a reference point.
- This code was made in 2015. Use at your own discretion.
-
Lex Yacc Introduction
-
Lex yacc primer
-
Bison manual references
-
Python expressions