Recursive descent parser generator: Difference between revisions
Content added Content deleted
m (→{{header|C++}}) |
(J) |
||
Line 355: | Line 355: | ||
_0004 = _0002 + _0003 |
_0004 = _0002 + _0003 |
||
</pre> |
</pre> |
||
=={{header|J}}== |
|||
J's recursive descent parser is adequate for this task, if we map names appropriately. |
|||
Implementation: |
|||
<lang J>cocurrent 'base' |
|||
inlocale=: 4 :0 L:0 |
|||
x,'_',y,'_' |
|||
) |
|||
parse=: 3 :0 |
|||
sentence=. ;:y |
|||
opinds=. (;:'+*-')i.sentence |
|||
opfuns=. (;:'plus times minus') inlocale 'base' |
|||
scratch=. cocreate'' |
|||
coinsert__scratch 'base' |
|||
names=. ~.sentence#~_1<:nc sentence |
|||
(names inlocale scratch)=: names |
|||
do__scratch ;:inv opinds}((#sentence)#"0 opfuns),sentence |
|||
) |
|||
op=: 1 :0 |
|||
: |
|||
r=. genname'' |
|||
emit r,'=:',x,m,y |
|||
r |
|||
) |
|||
plus=: '+' op |
|||
times=: '*' op |
|||
minus=: '-' op |
|||
N=: 10000 |
|||
genname=: 3 :0 |
|||
'z',}.":N=: N+1 |
|||
) |
|||
emit=: smoutput</lang> |
|||
Task example: |
|||
<lang J> parse'(one + two) * three - four * five' |
|||
z0001=:four*five |
|||
z0002=:three-z0001 |
|||
z0003=:one+two |
|||
z0004=:z0003*z0002 |
|||
z0004</lang> |