issue-#95 TextMapper Scanner & SETS

Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: issue-#95 TextMapper Scanner & SETS

Post by Ivan Denisov »

I have found the solution with well readable logic. So it does not have main disadvantage of state machines.

Code: Select all

	PROCEDURE Set (VAR s: Scanner);
		CONST nothing = 0; digit = 1; comma = 2;
		VAR n, m: INTEGER; ch: CHAR; waiting: BYTE;
	BEGIN
		s.type := set; Get(s, ch); s.Skip(ch); s.set := {};
		IF ch = "}" THEN Get(s, ch)
		ELSE
			waiting := digit;
			WHILE (s.type = set) & (waiting # nothing) DO
				IF waiting = digit THEN
					IF ("0" <= ch) & (ch <= "9") THEN
						Cardinal(s,n); s.Skip(ch);
						IF (MIN(SET) <= n) & (n <= MAX(SET)) THEN
							INCL(s.set, n)
						ELSE s.type := invalid END;
						waiting := comma
					ELSE s.type := invalid END
				ELSIF waiting = comma THEN
					IF ch = "," THEN Get(s, ch); s.Skip(ch); waiting := digit
					ELSIF ch = "." THEN
						Get(s, ch);
						IF ch = "." THEN
							Get(s, ch); s.Skip(ch); Cardinal(s, m); s.Skip(ch);
							IF (n <= m) & (m <= MAX(SET)) THEN
								WHILE m > n DO INCL(s.set, m); DEC(m) END
							ELSE s.type := invalid END
						ELSE s.type := invalid END
					ELSIF ch = "}" THEN Get(s, ch); waiting := nothing
					ELSE s.type := invalid END
				END
			END
		END
	END Set;
It is based on the Robert's version and passing his test.
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: issue-#95 TextMapper Scanner & SETS

Post by Robert »

Ivan Denisov wrote:I have found the solution with well readable logic...
Thanks Ivan.

I have also tested it, and with a few more test cases than I published, and it works for me.
Robert
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: issue-#95 TextMapper Scanner & SETS

Post by Ivan Denisov »

I put this in the topic branch so anyone can easily test the result:
http://blackboxframework.org/unstable/i ... a1.384.zip
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: issue-#95 TextMapper Scanner & SETS

Post by Josef Templ »

Ivan, a state machine is not removed by renaming the variable 'state' to something else.

There is only one clean solution for adding the missing syntax checks:
Add the missing checks! Do not change the complete procedure.
If you like, I can show you the solution but it would be a very good exercise
if you try to find it by yourself.

- Josef
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: issue-#95 TextMapper Scanner & SETS

Post by Ivan Denisov »

Josef Templ wrote:Ivan, a state machine is not removed by renaming the variable 'state' to something else.

There is only one clean solution for adding the missing syntax checks:
Add the missing checks! Do not change the complete procedure.
If you like, I can show you the solution but it would be a very good exercise
if you try to find it by yourself.

- Josef
State machine is not bad by it self. It is bad if it make program difficult to analyse. Here we have only two states which are clear readable. That is a kind of Dijkstra cycle, realised with WHILE and IFs. Dijkstra cycle is not bad programming style. However maybe I will try to find the solution you talking about for exercising a bit later if I will have some time in the weekend.
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: issue-#95 TextMapper Scanner & SETS

Post by Josef Templ »

I have added the missing syntax checks based on the BB1.6 version.
In addition, I have fixed two more bugs that did existed in the 1.6 version.
1. the 'start' position of a set was not set correctly
2. in case of detecting eot (end of text) inside a set, the result type
was sometimes 'invalid' and sometimes 'eot'. Now it is always 'invalid'.

The Skip before parsing "}" is redundant and has been removed.

For the changes from the 1.6 version see http://redmine.blackboxframework.org/pr ... 22ba0cbbbc.


The version can be derived easily from the 1.6 version by adding the missing
syntax checks according to the expected and legal follow-up symbols at
a particular position within the parser. Doing this with the 1.6 version
leads to an increase of an already existing code replication for parsing
the comma and the syntax checks. But with the added checks
this code replication becomes really obvious (and ugly). However,
it can be removed systematically by factoring out the comma handling leading to:
Cardinal [".." Cardinal] ParseCommaAndCheckSyntax.
The first step, i.e. the added checks in the original code, is included below.

StdCoder.Decode ..,, ...R....3QwdONl9RhOO9vRbf9b8R7fJHP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FNuI90Uu.UUg6.qU3p7,tI9nRqE,5TeK45uPR9RZvPN9P9fQbf9b8RCIu8LqG
omCrl0ksH3..RtETfPd16F9vQ59.C24485Et...ktu0.Y62Um1,.d.,.HE.6..M.3Qwd0ZORNP
NZvQRtIIepZBG20ksH3.bf9ZORNPNG20EtD.0E.ses,2UXW.2.52.CLLC3b8Rn9P99F9vQ0ks,
Uikwm46.ZD00U..60cKE.cU.UiAco,gA70,E0E...7,,M.,.,.,tcpW.6.tfj1kDz41.1.,6.I
kmL,6..EBU.U,.J,2.0E65.2..yf....
--- end of encoding ---

- Josef
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: issue-#95 TextMapper Scanner & SETS

Post by Ivan Denisov »

Josef version http://blackboxframework.org/unstable/i ... a1.394.zip
It is passing Robert's test. I am ready to put this solution for voting if there are no more comments.
If 'No' will win we have to fix documentation.
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: issue-#95 TextMapper Scanner & SETS

Post by Josef Templ »

Given the fact that it now also contains 2 more bug fixes I would also vote Yes for the changes.

- Josef
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: issue-#95 TextMapper Scanner & SETS

Post by Robert »

Josef Templ wrote:I have added the missing syntax checks based on the BB1.6 version.
Thanks for that. This version does what I would expect for my extended set of test cases.

I must admit I don't understand what start is doing here. This stuff is never easy!
Robert
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: issue-#95 TextMapper Scanner & SETS

Post by Josef Templ »

'start' gives you the text position of the start of the scanned symbol.
In the specific case of scanning a set, it is the position of the opening "{".

- Josef
Post Reply