# Polish Notation - writing p.k to parse and evaluate

**URL:** <https://forum.kx.com/t/polish-notation-writing-p-k-to-parse-and-evaluate/9005>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [August 3, 2014, 10:17am UTC](https://forum.kx.com/t/polish-notation-writing-p-k-to-parse-and-evaluate/9005 "2014-08-03T10:17:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [August 3, 2014, 10:17am UTC](https://forum.kx.com/t/polish-notation-writing-p-k-to-parse-and-evaluate/9005/1 "2014-08-03T10:17:00Z")

</div>

couldn’t find anything on the wiki,…

i’d like to implement a language using kdb+. &nbsp;as an example, how about polish notation?

from http://en.wikipedia.org/wiki/Polish\_notation

The q expression 7\*5-6 in Polish Notation is written \*-5 6 7

so, given:

$cat eg.p  
\*-5 6 7

$q eg.p  
jack@uv:~/o$ echo \*-5 6 7 \>eg.p  
jack@uv:~/o$ q eg.p  
KDB+ 3.1 2014.07.01 Copyright (C) 1993-2014 Kx Systems  
l32/ 2()core 1177MB jack uv 127.0.1.1 NONEXPIRE&nbsp;

Welcome to kdb+ 32bit edition  
For support please see http://groups.google.com/d/forum/personal-kdbplus  
Tutorials can be found at http://code.kx.com/wiki/Tutorials  
To exit, type \  
To remove this startup msg, edit q.q  
k){0N!x y}  
'p.k: No such file or directory  
@  
“p”  
“\*-5 6 7”  
q))

what do i have to expose in p.k?&nbsp; parse? eval?

ta, jack.

---

<div class="post-metadata">

**Author:** ![felix1](https://avatars.discourse-cdn.com/v4/letter/f/3e96dc/32.png) [@felix1](https://forum.kx.com/u/felix1)\
**Post date:** [August 3, 2014, 12:59pm UTC](https://forum.kx.com/t/polish-notation-writing-p-k-to-parse-and-evaluate/9005/2 "2014-08-03T12:59:00Z")

</div>

Write p.k and save it where q.k is located:

\d .p  
e:{1" evaluating ",x}

Now in you can call this evaluator from command line or from q:  
q myscript.p  
Or  
p)+ 1 2

Of course, yo need to write a proper evaluator.  
This mechanism works only line by line.

Pe 03.08.2014 12:17, “Jack Andrews” \<effbiae@gmail.com\> a scris:

> couldn’t find anything on the wiki,…
> 
> i’d like to implement a language using kdb+. &nbsp;as an example, how about polish notation?
> 
> from http://en.wikipedia.org/wiki/Polish\_notation
> 
> The q expression 7\*5-6 in Polish Notation is written \*-5 6 7
> 
> so, given:
> 
> $cat eg.p  
> \*-5 6 7
> 
> $q eg.p  
> jack@uv:~/o$ echo \*-5 6 7 \>eg.p  
> jack@uv:~/o$ q eg.p  
> KDB+ 3.1 2014.07.01 Copyright (C) 1993-2014 Kx Systems  
> l32/ 2()core 1177MB jack uv 127.0.1.1 NONEXPIRE&nbsp;
> 
> Welcome to kdb+ 32bit edition  
> For support please see http://groups.google.com/d/forum/personal-kdbplus  
> Tutorials can be found at http://code.kx.com/wiki/Tutorials  
> To exit, type \  
> To remove this startup msg, edit q.q  
> k){0N!x y}  
> 'p.k: No such file or directory  
> @  
> “p”  
> “\*-5 6 7”  
> q))
> 
> what do i have to expose in p.k?&nbsp; parse? eval?
> 
> ta, jack.
> 
> –  
> Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [August 4, 2014, 6:39am UTC](https://forum.kx.com/t/polish-notation-writing-p-k-to-parse-and-evaluate/9005/3 "2014-08-04T06:39:00Z")

</div>

Thanks Felix,

I have implemented my first non-trivial\* language in k :)

i’ve hacked an eval for REVERSE polish notation  
&nbsp; http://en.wikipedia.org/wiki/Reverse\_Polish\_notation

jack@uv:~/o$ q  
KDB+ 3.1 2014.07.01 Copyright (C) 1993-2014 Kx Systems  
l32/ 2()core 1177MB jack uv 127.0.1.1 NONEXPIRE&nbsp;

q)k).p.e:{{$[102=@:y;(y .|:2#x),2\_x;0N!y,x]}/x@&:~101=t:@:‘x:-5!’-4!x}  
q)p)5 1 2+4\*+3-  
1 5  
2 1 5  
4 3 5  
3 17  
,14

ta, jack

\*for me.&nbsp; a god might do this in fewer characters.
