6 * Emulate the basic parser of the awk program. Breaks lines up into
7 * words and prints the words.
14 static char lineBuf[LINEBUF];
15 static char blineBuf[LINEBUF];
32 # Starts a line. Will initialize all the data necessary for capturing the line.
39 # Will be executed on every character seen in a word. Captures the word
40 # to the broken up line buffer.
42 blineBuf[blineLen++] = fc;
45 # Terminate a word. Adds the null after the word and increments the word count
48 blineBuf[blineLen++] = 0;
52 # Will be executed on every character seen in a line (not including
55 lineBuf[lineLen++] = fc;
58 # This section of the machine deals with breaking up lines into fields.
59 # Lines are separed by the whitespace and put in an array of words.
62 word = (extend - [ \t\n])+;
64 # The whitespace separating words in a line.
67 # The components in a line to break up. Either a word or a single char of
68 # whitespace. On the word capture characters.
69 blineElements = word $wordchar %termword | whitespace;
71 # Star the break line elements. Just be careful to decrement the leaving
72 # priority as we don't want multiple character identifiers to be treated as
73 # multiple single char identifiers.
74 breakLine = ( blineElements $1 %0 )* . '\n';
76 # This machine lets us capture entire lines. We do it separate from the words
78 bufLine = (extend - '\n')* $linechar %{ finishLine(); } . '\n';
80 # A line can then consist of the machine that will break up the line into
81 # words and a machine that will buffer the entire line.
82 line = ( breakLine | bufLine ) > startline;
84 # Any number of lines.
91 char *pword = blineBuf;
93 printf("endline(%i): %s\n", words, lineBuf );
94 for ( i = 0; i < words; i++ ) {
95 printf(" word: %s\n", pword );
96 pword += strlen(pword) + 1;
102 void awkemu_init( struct awkemu *fsm )
107 void awkemu_execute( struct awkemu *fsm, const char *_data, int _len )
109 const char *p = _data;
110 const char *pe = _data+_len;
114 int awkemu_finish( struct awkemu *fsm )
116 if ( fsm->cs == awkemu_error )
118 if ( fsm->cs >= awkemu_first_final )
129 void test( char *buf )
131 int len = strlen( buf );
133 awkemu_execute( &fsm, buf, len );
134 if ( awkemu_finish( &fsm ) > 0 )
143 test( "one line with no newline" );
144 test( "one line\n" );
148 #ifdef _____OUTPUT_____