67 lines
1.4 KiB
Text
67 lines
1.4 KiB
Text
#*
|
|
|
|
GRAMMAR OR PARSE TOKENS
|
|
int* - an unsigned integer
|
|
sint* - a +/- signed integer
|
|
.* - decimal point
|
|
|
|
HISTORY
|
|
12 sept 2021: written, working
|
|
|
|
*#
|
|
|
|
# remove leading space
|
|
begin { while [:space:]; clear; }
|
|
|
|
read;
|
|
[0-9] {
|
|
while [0-9]; put; clear; add "int*"; push; .reparse
|
|
}
|
|
[.+-] { put; add "*"; push; .reparse }
|
|
[:space:] { while [:space:]; clear; add "space*"; push; .reparse }
|
|
!"" {
|
|
put; clear;
|
|
add "Non-numeric char at "; lines; add ":"; chars; add "\n";
|
|
print; quit;
|
|
}
|
|
|
|
parse>
|
|
# To visualise token reduction uncomment this:
|
|
#lines; add ":"; chars; add " "; print; clear;
|
|
#add "\n"; unstack; print; clip; stack;
|
|
|
|
pop; pop;
|
|
|
|
# -------------
|
|
# 2 tokens
|
|
"+*int*","-*int*" { clear; add "sint*"; push; .reparse }
|
|
|
|
pop;
|
|
# -------------
|
|
# 3 tokens
|
|
"sint*.*int*","int*.*int*" {
|
|
clear; add "float*"; push; .reparse
|
|
}
|
|
|
|
(eof) {
|
|
# ignore trailing space
|
|
stack; pop; "space*" { clear; }
|
|
unstack;
|
|
"int*","sint*","float*" {
|
|
replace "sint*" "signed integer,";
|
|
replace "int*" "integer,";
|
|
replace "*" ",";
|
|
put; clear;
|
|
add "It looks like a number (of type "; get; add ")\n";
|
|
print; quit;
|
|
}
|
|
replace "sint*" "signed integer,";
|
|
replace "int*" "integer,";
|
|
replace "*" ",";
|
|
put; clear;
|
|
add "Doesnt look like one number \n";
|
|
add "It was parsed as '"; get; add "'\n";
|
|
print; quit;
|
|
}
|
|
|
|
push; push; push;
|