How to resolve the algorithm Function frequency step by step in the BBC BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Function frequency step by step in the BBC BASIC programming language
Table of Contents
Problem Statement
Display - for a program or runtime environment (whatever suits the style of your language) - the top ten most frequently occurring functions (or also identifiers or tokens, if preferred). This is a static analysis: The question is not how often each function is actually executed at runtime, but how often it is used by the programmer. Besides its practical usefulness, the intent of this task is to show how to do self-inspection within the language.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Function frequency step by step in the BBC BASIC programming language
Source code in the bbc programming language
INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(1,0) : REM Descending
Valid$ = "0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"
DIM func$(1000), cnt%(1000)
nFunc% = 0
file% = OPENIN("*.bbc")
WHILE NOT EOF#file%
ll% = BGET#file%
no% = BGET#file% + 256*BGET#file%
INPUT #file%, l$
i% = 1
REPEAT
j% = INSTR(l$, CHR$&A4, i%) : REM Token for 'FN'
k% = INSTR(l$, CHR$&F2, i%) : REM Token for 'PROC'
IF k% IF j%=0 OR j%>k% THEN
i% = k%
f$ = "PROC"
ELSE
i% = j%
f$ = "FN"
ENDIF
IF i% THEN
REPEAT
i% += 1
f$ += MID$(l$, i%, 1)
UNTIL INSTR(Valid$, MID$(l$, i%+1, 1)) = 0
FOR j% = 0 TO nFunc%-1
IF f$ = func$(j%) EXIT FOR
NEXT
IF j% >= nFunc% nFunc% += 1
func$(j%) = f$
cnt%(j%) += 1
ENDIF
UNTIL i%=0
ENDWHILE
CLOSE #file%
C% = nFunc%
CALL Sort%, cnt%(0), func$(0)
IF C% > 10 C% = 10
FOR i% = 0 TO C%-1
PRINT func$(i%) " (" ; cnt%(i%) ")"
NEXT
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Safe primes and unsafe primes step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the Forth programming language