204 lines
4.2 KiB
ObjectPascal
204 lines
4.2 KiB
ObjectPascal
// ============================================================================
|
|
// This file is generated by the Delphi Parser Generator.
|
|
// ----------------------------------------------------------------------------
|
|
// DPG version: 1.0.1.0d
|
|
// Grammar: calcParser
|
|
// ============================================================================
|
|
unit calcParser;
|
|
|
|
interface
|
|
|
|
uses
|
|
calcParserTokens,
|
|
Classes,
|
|
Contnrs,
|
|
dpgLLkParser,
|
|
dpgToken,
|
|
dpgTypes,
|
|
SysUtils;
|
|
|
|
type
|
|
// =========================================================================
|
|
// Class TcalcParser declaration
|
|
// =========================================================================
|
|
TcalcParser = class( TdpgLLkParser)
|
|
|
|
public // Public grammar rules
|
|
procedure calc ;
|
|
procedure expression ;
|
|
procedure simpleExpression ;
|
|
procedure term ;
|
|
procedure factor ;
|
|
procedure uInt ;
|
|
|
|
end;
|
|
|
|
implementation
|
|
uses
|
|
dpgException,
|
|
dpgExceptionSemantic,
|
|
dpgExceptionMismatchedToken;
|
|
|
|
// ============================================================================
|
|
// calc
|
|
// ============================================================================
|
|
procedure TcalcParser.calc;
|
|
var
|
|
_cnt_16: integer;
|
|
v: integer;
|
|
|
|
begin
|
|
|
|
_cnt_16 := 0;
|
|
|
|
while(true) do
|
|
begin
|
|
if (( LA(1) in [TT_LPAREN,TT_PLUS..TT_MINUS,TT_INT])) then
|
|
begin
|
|
expression;
|
|
match(TT_SEMI);
|
|
writeln(v);
|
|
end
|
|
|
|
else
|
|
begin
|
|
if _cnt_16 >= 1 then
|
|
break
|
|
else
|
|
Raise EdpgMismatchedToken.Create( LT(1), [TT_LPAREN,TT_PLUS..TT_MINUS,TT_INT], FileName);
|
|
end;
|
|
|
|
INC(_cnt_16);
|
|
end;
|
|
end;
|
|
|
|
// ============================================================================
|
|
// expression
|
|
// ============================================================================
|
|
procedure TcalcParser.expression;
|
|
begin
|
|
|
|
simpleExpression;
|
|
end;
|
|
|
|
// ============================================================================
|
|
// simpleExpression
|
|
// ============================================================================
|
|
procedure TcalcParser.simpleExpression;
|
|
var
|
|
v : integer;
|
|
|
|
begin
|
|
|
|
term;
|
|
|
|
while(true) do
|
|
begin
|
|
if (( LA(1) in [TT_PLUS])) then
|
|
begin
|
|
match(TT_PLUS);
|
|
term;
|
|
result := result + v;
|
|
end
|
|
|
|
else if (( LA(1) in [TT_MINUS])) then
|
|
begin
|
|
match(TT_MINUS);
|
|
term;
|
|
result := result - v;
|
|
end
|
|
|
|
else
|
|
break;
|
|
end;
|
|
|
|
end;
|
|
|
|
// ============================================================================
|
|
// term
|
|
// ============================================================================
|
|
procedure TcalcParser.term;
|
|
var
|
|
v : integer;
|
|
|
|
begin
|
|
|
|
factor;
|
|
|
|
while(true) do
|
|
begin
|
|
if (( LA(1) in [TT_STAR])) then
|
|
begin
|
|
match(TT_STAR);
|
|
factor;
|
|
result := result * v;
|
|
end
|
|
|
|
else if (( LA(1) in [TT_SLASH])) then
|
|
begin
|
|
match(TT_SLASH);
|
|
factor;
|
|
result := result div v;
|
|
end
|
|
|
|
else
|
|
break;
|
|
end;
|
|
|
|
end;
|
|
|
|
// ============================================================================
|
|
// factor
|
|
// ============================================================================
|
|
procedure TcalcParser.factor;
|
|
var
|
|
s: integer;
|
|
|
|
begin
|
|
|
|
s := 1;
|
|
|
|
if (( LA(1) in [TT_PLUS])) then
|
|
begin
|
|
match(TT_PLUS);
|
|
s := 1;
|
|
end
|
|
|
|
else if (( LA(1) in [TT_MINUS])) then
|
|
begin
|
|
match(TT_MINUS);
|
|
s := -1;
|
|
end;
|
|
if (( LA(1) in [TT_INT])) then
|
|
begin
|
|
uInt;
|
|
end
|
|
|
|
else if (( LA(1) in [TT_LPAREN])) then
|
|
begin
|
|
match(TT_LPAREN);
|
|
expression;
|
|
match(TT_RPAREN);
|
|
end
|
|
|
|
else
|
|
Raise EdpgMismatchedToken.Create( LT(1), [TT_LPAREN,TT_INT], FileName);
|
|
result := s * result;
|
|
end;
|
|
|
|
// ============================================================================
|
|
// uInt
|
|
// ============================================================================
|
|
procedure TcalcParser.uInt;
|
|
var
|
|
x: IdpgToken;
|
|
|
|
begin
|
|
|
|
x := LT(1);
|
|
match(TT_INT);
|
|
result := StrToInt( x.TokenText);
|
|
end;
|
|
|
|
end.
|