mozart / mozart2

Mozart Programming System v2
http://mozart.github.io/
BSD 2-Clause "Simplified" License
566 stars 96 forks source link

I am having trouble with an assignment that involves file i/o. It involves the problem of where to save my file in Oz.If it's not too much trouble, may I have a hint/Assistance to my problem? #333

Open Spainardzap opened 3 years ago

Spainardzap commented 3 years ago

The proposed imperative language which should be tokenized has the following list of tokens: keywords, operators, atoms, integers, floats, and separators.The keywords are: program, void, bool, int, float, true, false, if, then, else, local, in, end, assign, call. The operators are: ‘=’, ‘+’, ‘-‘, ‘*’, ‘/’, ‘==’, ‘!=’, ‘>’, ‘<’,‘<=’,‘>=’. An atom starts with a lowercase character, followed byany number of alphanumeric characters, but it cannot be a keyword. The integers and floats are those known like in any programming language. The separators are blanks (spaces), ‘;’, ‘,’, ‘(‘, ‘)’, ‘{‘, ‘}’. Write an Oz program which reads a sequenceof characters, and provides in the output the list of tokens according to the above rules.If a token cannot be accepted, then the Oz program should provide an error message accordingly.For example, the fileprogram foo; int meth1(int x) { assign x=x+1; call boo(3)}should provide at the output the list of tokens: [program foo ';' int meth1 '(' int x ')' '{' assign x '=' x '+' 1 ';' call boo '(' 3 ')' '}']

What i did so far: declare

%ExampleOfToken="foo;;foo;"

fun {SeparateToken Token Token1 Acc}

case Token

of nil then Acc

[] H|T then

  if H == '&' then

          % We identify the semicolon ;

          % The scanned characters identify the token Token1

           if Token1 \= nil then

              % if this token is not nil, then we append Token1 and ";"

              {SeparateToken T nil {Append Acc {Append [Token1] [";"]}}}

          else

              % if this token is nil, we do not append Token1

        {SeparateToken T nil {Append Acc [";"]}}

          end

  else

          % otherwise, we are in the middle of a token, so

          % Token1 will be {Append Token1 [H]}

          {SeparateToken T {Append Token1 [H]} Acc}

  end

end

end

%{Browse {SeparateToken ExampleOfToken nil nil}}

layus commented 3 years ago

Hi, I am glad to here that Oz is used as at teaching language outside UCLouvain :-). May I ask where you are studying, and in which context this assignment was given to you ?

That being said, I do not really understand what you need help for. You requested hints about File IO usage, but your code is a single function parsing strings. There is some documentation about file IO here: http://mozart2.org/mozart-v1/doc-1.4.0/system/node55.html

Spainardzap commented 3 years ago

It is being taught at Lamar university in Beaumont,Texas. Sorry about the jumbled mess of a question. I was meaning to ask how to save files to OZ and read them.

The module “File.oz” can be found at the book’s URL, namely http://www.info.ucl.ac.be/people/PVR/ds/mitbook.htmlThe module “File.oz” should be compiled using the command: ozc –c File.oz which will create the compiled functor, “File.ozf”. This should be loaded and linked interactively using the command:declare [File]={Module.link ['File.ozf']}Considering that our program to be tokenized is stored in the file “foo.txt”, then the operation File.readListreads the whole content of the file into a string:Content = {File.readList "foo.txt"}To check its value we may just simply type:{Browse Content}% which returns the string represented as a list of ASCII codesor {Browse {String.toAtom Content}}% which returns the stringTo do the tokenization, one can use a standard Oz function, called String.tokens. For example, L={String.tokens "a bb cc d" & }% after &there is a space “ “will bind Lto the list ["a" "bb" "cc" "d"].

layus commented 3 years ago

In the OPI (emacs, and vscode-oz) Open is already available. If you are in a functor you need to import Open.

Now you just have to follow http://mozart2.org/mozart-v1/doc-1.4.0/op/node6.html#label60