Tag Archives: !!AB

!!AB : Creating Compile Language : Part 1

!!AB

Per the original spec for my Nonalphabetic Programming Language –dubbedd !!AB or NOTAB– I have finally gotten around to trying to turn it into an actual usable product. First time around, I haven’t gotten used to Flex/Bison yet, so I didn’t get anywhere. But this time, I think I have a slightly better grasp, and have something that seems to work. I’ve read some tutorials, but still have a long way to fully understand the syntax and logical procedure and grand vision for Flex/Bison.

It’s still at a very early stage. The source code can be found at this git repository for NOTAB.

Strings

First area I started with is text output, so that I can see the results of the sample source code written in !!AB language. It will only accept double-quotes and not single-quotes/apostrophe to enclose strings–in order to not use up the limited number of nonalphanumeric characters, nor to use symbols in combinations that are not intuitive. (Like why do languages have comments and comment blocks delimited by // and /*? This makes no sense.) Per the specs of this new language, the early draft of my Flex code went something like this:

%%
%x MODE_STRING
%%
\"                { BEGIN(MODE_STRING); }
<MODE_STRING>\"   { BEGIN(INITIAL); }
<MODE_STRING>[^\"]*  { yylval.sval = strdup(yytext); return STRING; }

The program always begins in the INITIAL mode.
%x MODE_STRING
tells Flex this is a mode that the program will enter and leave when it encounters specific text in the source. In this case, this mode will try to capture a string of text between two double-quotes “”. This is an exclusive mode –signified by %x– which means only rules beginning with this code —
<MODE_STRING>
— will be processed while the lexer is under this mode. (The other type is inclusive mode, signified by %s.)

When it sees a double-quote while not in MODE_STRING mode, it will turn on this mode. While in this mode, any character besides the double-quote is considered part of the string, including newlines. That means a string can span multiple lines.

This mode will end when it meets the next double-quote. Caveat is that the !!AB spec desires that double-quotes within strings be escaped as \”. However, this is not possible yet, since my skill with Flex/Bison is still very lacking. Although one idea is to create a temporary string variable when it first enters this mode. Then create a new rule to additionally capture \”. Finally when it leaves this mode (but not before), the entire string will be returned.


Commentation

Also completed the comment system. Much simpler than strings concept. Allows both single- and multi-line comments. Single line comment begins with single-quote ‘, and will ignore the rest of the line. Multi-line comment blocks are enclosed within ‘[ and ‘].

Flex goes like this:

%%
%x MODE_COMMENT
%%
"'["		       { BEGIN(MODE_COMMENT); }
<MODE_COMMENT>{
	[^'\n]*      // ignore anything that's not a '
	'+[^'\]\n]*  // ignore ' not followed by ]
	\n           { ++line_num; }
	'+"]"        { BEGIN(INITIAL); }
}

'[^\[][^\n]*		; // comment, ignore rest of line

I designate a second mode for block comments. It initiates when it detects ‘[. It ignores pretty much every character while in this mode. The new line is to keep the line number for debugging, so you know the line number of the input source when you get an error.

There are two different checks during this mode. The comment mode exits when it meets ‘]. So you can have series of ‘ with or without a trailing ]. Therefore check both conditions to get the desired behaviour.

Also to ensure single line comments don’t interfere with multi-line, it must check against the opening bracket [ that starts multi-line mode.


Test Source

Here is the test source file written to the specs of the new language. It seems to work as expected.

$< "Hello Master " + "Xayvir."

$<
$< "What about
newlines?"
$<
$< "Or \n escaped newlines?"

$<
$< "!!AB is such a \"great\" programming language" ' aka NOTAB

$<
$< 890 + \
" beers on the wall."

$<
$< "I have " + 11.01 + " dollars." 'US dollars

'[
comment block
']

$<
$< "Flavor text with" + '[ this should not appear '] " inside comment block"

$<
$< "What about unclosed string?"

While writing this post but before completing, I went ahead and read more materials on getting Flex to do more complicated strings and comments. https://www.cs.virginia.edu/~cr4bd/flex-manual/Start-Conditions.html. So the strings section in final program is actually more advanced than what is posted in this post. (You can read that site rather than me repeat them here.)

Nonalphabetic Programming Language

Nonalphabetic programming language
called !!AB

Programming language where common and simple operations and expressions (excluding variables) are typed with non-alphabetic symbols. Allows various programming paradigms, including procedural, object oriented, and functional.  Balanced ternary is the base for logic and digit operations. Decimal numbers are preferred for human readability. Array index starts at 1.

 Continue reading Nonalphabetic Programming Language