A BNF Parser in Forth (1992)
bradrodriguez.com
bradrodriguez.com
\ FORTH PAPER TAPE PUNCHER:
: PT# ( L --- L/2 )
DUP 1 AND IF
ASCII @
ELSE BL THEN
HOLD
2/
;
: PT. ( N --- )
<# PT# PT# PT#
ASCII . HOLD
PT# PT# PT# PT#
#> TYPE
;
: CUT
." -----------" CR
;
: PTAPE
CUT
BEGIN
KEY ?DUP WHILE
DUP ." |" PT. ." |" SPACE EMIT CR
REPEAT
CUT
;
PS: I rewrote PTAPE in PostScript for NeWS, so it makes a window with actual holes punched into it in the shape of a piece of paper tape with the text you pass in as a parameter:https://www.donhopkins.com/home/archive/news-tape/fun/ptape/...
Can anybody write that for X11 using the SHAPES extension to punch the holes in fewer lines of code? ;)
(The comments about an elliptical canvas and round shape must have been copied from some other old code, because the shape of the window (and icon) it makes is rectangular with lots of round holes punched into it.)
The zip file [1] has english documentation, the papers [2,3] are unfortunately only available in german.
This [4] is a mostly working grammar for the Verilog language written using Gray. It uses a lexer that sits on top of Gforth's regexp [5] package that directly compiles regexps into forth code (BTW quite a few bugs in the regexp package were triggered by the lexer and subsequently fixed upstream).
[1] http://www.complang.tuwien.ac.at/forth/gray4.zip
[2] http://www.complang.tuwien.ac.at/papers/ertl89.ps.gz
[3] http://www.complang.tuwien.ac.at/papers/ertl97.ps.gz
[4] https://sourceforge.net/p/fourhdl/code/HEAD/tree/trunk/parse...
[5] https://github.com/forthy42/gforth/blob/master/regexp.fs
That said, it’s pretty cool.
Perl programmers may savor and fetishize their weird arbitrary unreadable code like letter-sparse sigil-saturated line noise, but Perl can't hold a candle to the subtle beauty and enigmatically punctuated surrealistic symmetry of FORTH code:
8 : BNF: [COMPILE] : SMUDGE COMPILE <BNF ; IMMEDIATE
9 : ;BNF COMPILE BNF> SMUDGE [COMPILE] ; ; IMMEDIATE
>b) Line 9 shows a recursive production, <S>. During the definition of a production, its name is automatically unSMUDGEd.Of course it's still possible to write self documenting FORTH code:
: C(-; LICK SMILE NOSE WINK ;
comp.lang.forth: Why smudge?https://groups.google.com/forum/#!topic/comp.lang.forth/olO-...
>Does "smudge" have any other use than to stop the current word being found during a dictionary lookup? If not, then its only use would seem to be to allow a word to be defined in terms of another (older) word with the same name. Are there some useful things that can be done with this ability that make up for the (minor) extra complication?
My other favorite (and much more useful and powerful for metaprogramming) FORTH words are <BUILDS and DOES>, but <BUILDS has been replaced by CREATE now, so they don't look as cool when used together.
http://amforth.sourceforge.net/TG/recipes/Builds.html
<BUILDS is the older sibling of CREATE. It has been in use in pre-ANSI Forth’s and is still around as an supplemental tool. It is a parsing word that takes the next word from the input source and creates a dictionary entry in the current word list. Unlike CREATE it does not add an execution token. Thus the word list entry created is unfinished and calling it will crash the system.
The definition of the word list entry is finished with the DOES> section, that makes the word list entry a proper callable word.
The big advantage over CREATE is that CREATE requires a rewrite of the contents of the execution token. That is on microcontrollers with the flash based dictionary only possible if a single cell can be re-programmed. The Atmegas can do that with little efforts, the MSP430 lacks essential ressources to do so.
From the programming perspective there are no differences. <BUILDS can safely replace CREATE in defining words that contain a DOES> section as well. All other occurances of CREATE should remain unchanged.
: constant create , does> @i ;
\ or
: constant <builds , does> @i ;I think, the modern Forths (i.e. since 1994 ANS Forth standard) need neither HIDE nor REVEAL as these are made superflous by RECURSE.
Other edge cases could be handled using a mechanism like DEFER [1] (which can be implemented in terms of ANS Standard words).
Just looking through the manual, even the modern Gforth has something like RECURSIVE [2] which is just a differently named implementation of REVEAL.
[1] http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Defe... https://forth-standard.org/standard/core/RECURSE [2] http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Call...
DEFER is great! But the word that resolves it should have been named REFED.
Fun, but totally unusable for a mediocre programmer like myself.
Then I tried to write a function that prints a number in decimal format, and realized I could not debug it. Fail!