Initial check in docu

This commit is contained in:
2026-01-03 18:31:15 +01:00
parent e2c3cbc520
commit ee130973e2
98 changed files with 9430 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
program calc;
{$APPTYPE CONSOLE}
uses
Classes,
SysUtils,
calcLexer in 'calcLexer.pas',
calcParser in 'calcParser.pas';
var
stm: TFileStream;
lex: TcalcLexer;
par: TcalcParser;
begin
if ParamCount <> 1 then
begin
writeln('usage: calc <filename>');
exit;
end
else
begin
try
stm := TFileStream.Create( ParamStr(1), fmOpenRead);
lex := TcalcLexer.Create(stm);
par := TcalcParser.Create(lex);
par.calc;
except
end;
end;
stm.Free;
par.Free;
end.
+5
View File
@@ -0,0 +1,5 @@
1+2+3+4+5+6+7+8+9;
(((((2+3)))));
(-1*(-2*(-3*(-4+ -5))));
(-1*(-2*(-3*(-4+ 5))));
7 * -(-9);
+46
View File
@@ -0,0 +1,46 @@
// ============================================================================
// Demo lexer for a four operator calculator
// ============================================================================
unit calcLexer;
lexer TcalcLexer;
options
{
exportVocab = calcLexer;
k = 2;
}
// ============================================================================
// Simple tokens
// ============================================================================
LPAREN : '(';
RPAREN : ')';
PLUS : '+';
MINUS : '-';
STAR : '*';
SLASH : '/';
SEMI : ';';
// ============================================================================
// INT
// ============================================================================
INT : ('0'..'9')+;
// ============================================================================
// White space
// ============================================================================
WS
:
(
'\r' '\n' { newLine; }
| '\r' { newLine; }
| '\n' { newLine; }
| '\t' { tab; }
| ' '
)
{
_ttype := TT_SKIP;
}
;
+92
View File
@@ -0,0 +1,92 @@
// ============================================================================
// Demo parser for four operator calculator
// ============================================================================
unit calcParser;
parser TcalcParser;
options
{
importVocab = calcLexer;
exportVocab = calcParser;
}
memberdecl
{
value : integer;
}
// ============================================================================
// calc
// ============================================================================
calc
: (expression SEMI {writeln(value);} )+
;
// ============================================================================
// expression
// ============================================================================
expression
: simpleExpression
;
// ============================================================================
// simpleExpression
// ============================================================================
simpleExpression
local
{
temp: integer;
}
: term { temp := value; }
(
PLUS term { temp := temp + value; }
| MINUS term { temp := temp - value; }
)* { value := temp; }
;
// ============================================================================
// term
// ============================================================================
term
local
{
temp: integer;
}
: factor { temp := value; }
(
STAR factor { temp := temp * value; }
| SLASH factor { temp := temp div value; }
)* { value := temp; }
;
// ============================================================================
// factor
// ============================================================================
factor
local
{
s: integer;
}
{
s := 1;
}
:
(
PLUS { s := 1; }
| MINUS { s := -1; }
)?
(
uInt
| LPAREN expression RPAREN
)
{
value := s * value;
}
;
// ============================================================================
// uInt
// ============================================================================
uInt
: x:INT { value := StrToInt( x.TokenText); }
;
+10
View File
@@ -0,0 +1,10 @@
To build demo project you must first compile the grammars.
1. Open calc.dpp in DPG
2. Press F9 to compile the grammars
After the compilation the project can be opened in delphi. Be sure that the dpg
runtime library is in the delphi library path. (In the project settings,
or in the environment settings).
Have fun...