Pep & Nom

home | documentation | examples | translators | download | journal | blog | all blog posts

the nom "clear" command

clear or delete the text in the workspace buffer.

A very common procedure in a nom script when parsing and translating a formal language (examples of languages are the json text data format, the “java” programming language, or the set of palindromes is to 1st match a set of parse tokens, then build the text attribute for the parse token which will replace the matched tokens, and then put the new attribute into the current tape cell. Then clear the workspace buffer and create the new parse token. The example below (from a fictitious grammar for parsing arithmetic expressions) illustrates these steps:

"reduce" parse tokens and build the new attribute


   pop; pop; pop;
   # match the token sequence 'leftbrace expression rightbrace'
   "leftbrace*expression*rightbrace*" {
     clear; 
     # build the new 'attribute' from each parse token attribute
     get; ++; get; ++; get;
     # realign the tape pointer and save the new attribute
     --; --; put; 
     # clear the workspace, build the new parse token, and push on stack
     clear; add "expression*"; push;
     # jump back to the 'parse>' label: this allows any other 
     # grammar reductions/productions to take place
     .reparse
   }
 

The script above represent a single grammar rule or production namely

 expression = leftbrace expression rightbrace ;

examples

remove tabs and line feeds from the input


    read; [\t\r] { clear; } print; clear;
  

classify words into grammar parse tokens


    while [:space:]; clear; (eof) { .reparse }
    whilenot [:space:]; put; clear; add "word*"; push;
  parse>
    pop; "word*" {
      clear; get; 
      "green","blue","yellow" { clear; add "colour*"; push; .reparse }
      [:digit:] { clear; add "number*"; push; .reparse }
    }
    push;
    # script continues.
  

machine diagrams

By examining the diagrams below, it is possible to see exactly how the clear command affects machine state. Actually, only the workspace buffer is affected. Within the pep interpreter the instruction pointer is also affected, but when a script runs through a translator there is no equivalent program state.

program and machine state before 'clear' command
Machine State
stack workchar*char* peepv
acc0 flagTRUE esc\\
delim* chars2 lines1
Tape
celltextmark
(size: 500)
0>r
1e
2
Partial Program Listing
57:add [text:pal*pal*]
58:testis [text:char*char*]
59:jumpfalse [int:19]
60>clear
61:get
62:++

machine state after the 'clear' command
Machine State
stack work peepv
acc0 flagTRUE esc\\
delim* chars2 lines1
Tape
celltextmark
(size: 500)
0>r
1e
2
Partial Program Listing
58:testis [text:char*char*]
59:jumpfalse [int:19]
60:clear
61>get
62:++
63:testtape

the current parse token