TILBegin-EndExtensionUsingTXL
Software Transformation Systems
A TXL solution to
TIL Chairmarks #1.3, the begin-end syntax extension for the
Tiny Imperative Language.
TXL is designed for implementing language extensions, so adding a new syntactic feature is simple in TXL using
Grammar Overrides. The "..." in the "redefine" is not a shorthand in this example, rather a language feature of TXL that denotes an extension to the existing nonterminal alternatives.
Since there is no semantics for begin-end in TIL, we don't add any transform for this yet.
--
JamesCordy - 10 Oct 2005
File "TILbeginparser.Txl"
% TXL parser for begin-end language extenssion to Tiny Imperative Language
% Jim Cordy, October 2005
% Begin with the standard TIL grammar
include "TIL.Grm"
% Add begin-end statements using grammar overrides
redefine statement
... % refers to all existing forms for [statement]
| [begin_statement] % add alternative for our new form
end redefine
define begin_statement
'begin [IN][NL]
[statement*] [EX]
'end [NL]
end define
% No need to do anything except recognize the input, since the grammar
% includes the output formatting cues
function main
match [program]
_ [program]
end function
Example run:
<linux> cat begintest.til
// Factor an input number
begin
var n;
write "Input n please"; read n;
write "The factors of n are";
begin
var f; f := 2;
while n != 1 do
while (n / f) * f = n do
// Got one - print it!
write f;
n := n / f;
end
f := f + 1;
end end end
<linux> txl begintest.til TILbeginparser.Txl
TXL v10.4a (15.6.05) (c)1988-2005 Queen's University at Kingston
Compiling TILbeginparser.Txl ...
Parsing begintest.til ...
Transforming ...
begin
var n;
write "Input n please";
read n;
write "The factors of n are";
begin
var f;
f := 2;
while n != 1 do
while (n / f) * f = n do
write f;
n := n / f;
end
f := f + 1;
end
end
end
<linux>