Compiler/Verifying syntax: Difference between revisions

(→‎{{header|Go}}: Further tweaks and examples.)
Line 511:
procedure cmp_expr()
if tok=={IDENT,"not"} then next_token() end if
while true dosum_expr()
if not find(tok,{{SYMBOL,"="},{SYMBOL,"<"}}) then exit end if
next_token()
sum_expr()
end whileif
if not find(tok,{{SYMBOL,"="},{SYMBOL,"<"}}) then exit end if
next_token()
end while
end procedure
 
Line 548:
 
constant tests = {
"3 + not 5",
"3 + (not 5)",
"(42 + 3",
" not 3 < 4 or (true or 3 / 4 + 8 * 5 - 5 * 2 < 56) and 4 * 3 < 12 or not true",
" and 3 < 2",
"not 7 < 2",
"2 < 3 < 4",
"2 < foobar - 3 < 4",
"2 < foobar and 3 < 4",
"4 * (32 - 16) + 9 = 73",
"235 76 + 1",
"true or false = not true",
"not true = false",
"a + b = not c and false",
"a + b = (not c) and false",
"a + b = (not c and false)",
"ab_c / bd2 or < e_f7",
"g not = h",
"étéété = false",
"i++",
"j & k",
"l or _m"}
 
printf(1,"Verify Syntax:\n")
for i=1 to length(tests) do
verify_syntax(tests[i])
Line 569 ⟶ 578:
Note that "= not c" fails, whereas "= (not c)" passes, see talk page.
<pre>
Verify Syntax:
(42 + 3 ==> fail [tok={"ERROR",") expected"}]
not 3 < 4 or (true or 3 / 4 + 8 * 5 - 5 * 2 < 56) and 4 * 3 < 12 or+ not true5 ==> passfalse [tok={"INTEGER",5}]
3 + and(not 3 < 25) ==> fail [tok={"INTEGER",3}]true
not 7 <(42 2+ 3 ==> passfalse [tok={"ERROR",") expected"}]
not 3 < 4 or (true or 3 / 4 + 8 * (32 5 - 165 * 2 < 56) +and 94 =* 3 < 12 or not 73true ==> passtrue
235 76and +3 1< 2 ==> failfalse [tok={"INTEGER",763}]
a + b = not c7 and< false2 ==> fail [tok={"IDENT","c"}]true
a + b = (not c) and false 2 < 3 < 4 ==> passfalse [tok={"SYMBOL","<"}]
ab_c / bd22 or< foobar - 3 < e_f74 ==> failfalse [tok={"ERROR",`invalid [{"SYMBOL","<"}]`}]
2 < foobar and 3 < g not = h4 ==> fail [tok={"IDENT","not"}]true
4 * (32 - 16) + 9 = 73 ==> true
â??®tâ??® = false ==> fail [tok={"ERROR",`invalid [{"ERROR","illegal char (â??/195)"}]`}]
235 76 + i++1 ==> failfalse [tok={"ERRORINTEGER",`invalid [{"SYMBOL","+"}]`76}]
true or false = not j & ktrue ==> failfalse [tok={"SYMBOLIDENT","&true"}]
not true = false ==> true
l or _m ==> fail [tok={"ERROR",`invalid [{"ERROR","identifiers may not start with _"}]`}]
a + b = not c and false ==> false [tok={"IDENT","c"}]
a + b = (not c) and false ==> true
a + b = (not c and false) ==> true
ab_c / bd2 or < e_f7 ==> false [tok={"ERROR",`invalid [{"SYMBOL","<"}]`}]
g not = h ==> false [tok={"IDENT","not"}]
â??®tâ??® été = false ==> failfalse [tok={"ERROR",`invalid [{"ERROR","illegal char (â??/195)"}]`}]
i++ ==> false [tok={"ERROR",`invalid [{"SYMBOL","+"}]`}]
(42 + 3j & k ==> failfalse [tok={"ERRORSYMBOL",") expected&"}]
l or _m ==> failfalse [tok={"ERROR",`invalid [{"ERROR","identifiers may not start with _"}]`}]
</pre>
7,806

edits