Parsing LANG1
In this section we will investigate the parsing of LANG1, that is, we will
construct a function that will go from the concrete syntax of LANG1
to the abstract syntax of LANG1. Loosely speaking, this function,
call it doparse, will be an inverse
to the function given by the homomorphism hsyn
= aitch(alg_LANG1Syn, __ ) . However, since the context-free
grammar for LANG1 is ambiguous (i.e., hsyn
is
not injective), what we will get is hsyndoparse
= 1 but doparsehsyn
1. The ambiguity is not be a problem since we will get
hsem
doparsehsyn=
hsem , where hsem
= aitch(alg_LANG1Sem, __ )
It follows then that the function hsem
doparse is a compiler for LANG1, that is, it is a function
taking expressions written in the concrete syntax to their meanings.
The Programs
We offer the user three programs
-
doparse(prog) which takes a concrete program, prog,
as argument and returns the corresponding abstract program.
-
compLANG1(prog) which takes a concrete program, prog,
as argument and returns the corresponding function on states -- more
accurately speaking, it returns a JavaScript program that computes that
function. Thus compLANG1 can be viewed as a compiler from LANG1 to
JavaScript. However, the result of applying compLANG1 to a LANG1
program, prog, is a JavaScript program implementing
a function SttStt
rather than a direct translation of prog
into JavaScript.
-
runLANG1(prog, stt) which takes a concrete program, prog,
and a state, stt, as arguments and returns
the state resulting from applying the function corresponding to prog
to the state stt.
There is a built-in sample state, called samplestt,
with initial value [["a", 10], ["b",
3], ["c", 77]] that can be used to test the following examples.
Note that performing the examples will modify the value of samplestt.
Of course, you can also input states of your own choosing.
Here are some sample concrete LANG1 programs which you can parse,
compile or run:
-
"b:=1; While (1<=a) do b:=(a*b); a:=(a+(-1))
od" -- the familiar program for the factorial
-
"If (a<=b) then c:=1 else c:=2 fi"
-
"If (a<=b) then c:=(c+(-1)) else c:=(c+1)
fi"