Script Interpretation (how to approach the problem?) - Page 1
Forum section: electronics / general

author: Rerouter
author_rank: Super Contributor
timestamp: May 08, 2016, 08:08:11 am
content: Hello,For a while now i have been using a software package that uses text based scripts to execute user code,I would like to write my own program that could also execute these scripts in a similar manner, and possibly other programming language ones later on.Where i am stuck is, how to approach the problem, E.g. Input Sanitation, Order of operations, Error handling, etc, without making it into a full blown compiler?I am assuming a for loop running through an IF statement tree would not be the ideal approach.Here is an example of a script, with 3 distinct sections, FORM related to the layout of the window it opens, and what is where in the window, labels being text, digits being a value box, etc. Onshow / OnApply being the executed code, with functions limited to If/Else/While, and all variables not declared in "Form" must start with "Var", The "@" keyword means read from that position in a file to fill in everyone,Code:Form{Group("Value",5,5,150,80){Digit(Value," ",5,18,90);Label(lb1,"Enter Units",110,20);}Group("Information",5,88,270,45){Label(Message," ",0,18);}Group("Description",5,135,270,100){Label(lb4,"Converter.",0,20);Label(lb2,"To convert to the European system of measurement",0,55);Label(lb3,"press <Apply>.",0,70);}}OnShow{Message.Bold = 1;// --------------- Test If Dump is correctVar0 = 0;Var1 = (@0x65 << 24) | (@0x64 << 16) | (@0x63 << 8) | @0x62;Var2 = (@0x69 << 24) | (@0x68 << 16) | (@0x67 << 8) | @0x66;Var3 = (@0x6d << 24) | (@0x6c << 16) | (@0x6b << 8) | @0x6A;Var4 = @0xD7;Var5 = @0xD8;Var6 = @0xD9;Var7 = @0xAE;Var8 = @0xAF;Var9 = @0xB0;if(Var4 = 0xFD){ Var0 = Var0 + 1;}if(Var5 = 0xFD){ Var0 = Var0 + 1;}if(Var6 = 0xFD){ Var0 = Var0 + 1;}if(Var7 = 0xBF){ Var0 = Var0 + 1;}if(Var8 = 0xBF){ Var0 = Var0 + 1;}if(Var9 = 0xBF){ Var0 = Var0 + 1;}if(Var0 != 6){Message.Color = 0x0000FF;Message = "Wrong File !";Distance = 0;}else{Var1 = 0xFFFFFFFF ^ Var1;// translate in BCD formVar0 = ((Var1 >> 28) & 0xF) * 10000000;Var0 = Var0 + (((Var1 >> 24) & 0xF) * 1000000);Var0 = Var0 + (((Var1 >> 20) & 0xF) * 100000);Var0 = Var0 + (((Var1 >> 16) & 0xF) * 10000);Var0 = Var0 + (((Var1 >> 12) & 0xF) * 1000);Var0 = Var0 + (((Var1 >> 8) & 0xF) * 100);Var0 = Var0 + (((Var1 >> 4) & 0xF) * 10);Var0 = Var0 + (Var1 & 0xF);Message.Color = 0xFF0000;Message = "File OK, press <Apply>";Value = Var0;}}OnApply{Var1 = Value;Var0 = 0;Var4 = 0xD7;Var5 = 0xD8;Var6 = 0xD9;Var7 = 0xAE;Var8 = 0xAF;Var9 = 0xB0;if(@Var4 = 0xFD){ Var0 = Var0 + 1;}if(@Var5 = 0xFD){ Var0 = Var0 + 1;}if(@Var6 = 0xFD){ Var0 = Var0 + 1;}if(@Var7 = 0xBF){ Var0 = Var0 + 1;}if(@Var8 = 0xBF){ Var0 = Var0 + 1;}if(@Var9 = 0xBF){ Var0 = Var0 + 1;}if(Var0 != 6){Message.Color = 0x0000FF;Message = "File wrong stop recording";}else{@Var4 = 0xFB;@Var5 = 0xFB;@Var6 = 0xFB;@Var7 = 0x7F;@Var8 = 0x7F;@Var9 = 0x7F;Message.Color = 0xFF0000;Message = "Translation in Europe ended.";}}If i am on the right track with a large IF tree, then fine, but was curious if there are other ways to approach it,

author: helius
author_rank: Super Contributor
timestamp: May 08, 2016, 08:15:40 am
content: Writing "a large IF tree" is a complete waste of time and will be impossible to debug or extend. Learn yacc, love yacc.

author: Rerouter
author_rank: Super Contributor
timestamp: May 08, 2016, 09:49:01 am
content: Thank you, looks exactly like how i should approach it, bit of a learning curve, but suppose i knew that walking in.

author: ataradov
author_rank: Super Contributor
timestamp: May 08, 2016, 07:15:44 pm
content: Quote from: helius on May 08, 2016, 08:15:40 amActually don't. Yacc is one of those things from the 70's that seemed like a good idea, but it is not.Neither GCC or Clang use it for any of their front-ends. I'm actually having a hard time thinking of an actively maintained compiler that uses YACC.Manual parsing is perfectly maintainable, if done correctly.

author: legacy
author_rank: Super Contributor
timestamp: May 08, 2016, 07:43:13 pm
content: here I am developing a new language which is a subset of C language plus a contamination of VHDL and ADAthe first suggestion i'd like to give is: write a good lexer/tokener, mine is very comfortable and smarteven if it took a lot of time to be designed (2 years, 4 hours per weekend)since then, I has been making things easier and funnierCode:33. 6 Kbyte lib_tokener_v6_deprecated.c26. 7 Kbyte lib_tokener_v8b.c27. 5 Kbyte lib_tokener_v7.c

author: helius
author_rank: Super Contributor
timestamp: May 08, 2016, 08:48:49 pm
content: Quote from: ataradov on May 08, 2016, 07:15:44 pmYacc is just a parser generator. It isn't only for compilers, but any tool that needs to parse a language.MySQL uses a yacc-alike, Bison, to parse SQL queries. They should rather have asked you for your expert opinion and written a manual parser?Puh-leeze.

author: rstofer
author_rank: Super Contributor
timestamp: May 09, 2016, 12:48:41 am
content: The complexity of the task is entirely dependent on the language definition. If you can make the grammar simple, like Pascal, you can easily parse the language and produce the output (tokens or whatever)For an example of a tiny version of Pascal see:https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_ProgramsThis is a terrific book! If you are interested in Pascal and, in 1980, it was the hottest thing around for PCs - UCSD Pascal comes to mind.Look up 'Pascal syntax diagrams' and see the elegance of the grammar.

author: nuno
author_rank: Frequent Contributor
timestamp: May 09, 2016, 01:59:17 am
content: Quote from: Rerouter on May 08, 2016, 08:08:11 amVery strange sentenceIt's relatively easy to translate a language into code for a predictive recursive parser, if you have your language described in a grammar or equivalent form (you should have your language defined somehow, anyway). However... lexical and syntactic parsing are only part of the work; you still need to transform the source code into an "executable" data structure, and execute it.Anyway, if you split the work into modules, one being a "tokenizer", another the syntax checker and so on, it will help greatly.

author: Rerouter
author_rank: Super Contributor
timestamp: May 09, 2016, 03:11:29 am
content: To clarify the files are literally .txt files that hold the text in that code block. The software had the ability to run these scripts,Thanks for all the help, tokenizing looks pretty easy,