Recent

Author Topic: Expression parser and AnsiString  (Read 896 times)

Uto

  • New Member
  • *
  • Posts: 30
Expression parser and AnsiString
« on: April 21, 2019, 05:31:46 pm »
Hi all,

I've been building the front end of a compiler for some time using fpc and plex, and at some point I had to modify ULexLib to support long strings, as it looked like it was made for old 255 character strings. I used AnsiString type for that and now my compiler supports long strings.

Now I want my compiler to support simple math expression ( in fact I just need addition and substractions). I found fpexprpars unit so I was trying to use TFPExpressionParser, I expected it to be easy but...

Sadly, the expressions I have to evaluate are stored into AnsiStrings, and when I assign the value to Parser.Expression it accepts it, but then when the expresion is evaluated I get exceptions like this:

Code: Pascal  [Select][+][-]
  1. EExprParser: Unknown identifier: 1+1

That doesn't happen if instead of assigning the value I got from my compiler parser ,I directly assing  a string between quotes like '1+1';

So this doesn't work:

Code: Pascal  [Select][+][-]
  1.         Parser := TFPExpressionParser.Create(nil);
  2.         Parser.BuiltIns := [bcMath];
  3.         WriteLn(CurrentText);
  4.         Parser.Expression := CurrentText;
  5.         parserResult := Parser.Evaluate; ;
  6.  

but this works:

Code: Pascal  [Select][+][-]
  1.         Parser := TFPExpressionParser.Create(nil);
  2.         Parser.BuiltIns := [bcMath];
  3.         WriteLn(CurrentText);
  4.         Parser.Expression := '1+1';
  5.         parserResult := Parser.Evaluate; ;
  6.  

Note:  CurrentText is a global variable of AnsiString type, holding the current text my compiler got, expressions are in between bracers so I get whole expression as single string.

In both cases I get "1+1" in the screen just before the evaluation, but the first one raises an exception.

I've tried to find how to convert strings, but didn't find a way. I tried to create a local variable with ShortString type, and assign CurrentText to that variable first, then assign to the parser, also tried to assing to a ShortStringVariable, then concatenate a single space string to force conversion, etc. No way.

Is there a simple way to make fpc convert an AnsiString to ShortString?

Note:  Due to other reasons, I'm using {$MODE OBJFPC}

Thanks in advance!
« Last Edit: April 21, 2019, 05:47:22 pm by Uto »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Expression parser and AnsiString
« Reply #1 on: April 21, 2019, 06:14:16 pm »
Is there a simple way to make fpc convert an AnsiString to ShortString?

AFAIK, assigning to a ShortString should do the trick nicely.
Code: [Select]
MyShortString := MyAnsiString;But try one thing: change your WriteLn to:
Code: [Select]
Writeln('"'+CurrentText+'"');just to make sure there is nothing straneous in the string like extra spaces or alike. Note: those are double-quotes in there, not four single quotes.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14358
  • Sensorship about opinions does not belong here.
Re: Expression parser and AnsiString
« Reply #2 on: April 21, 2019, 06:18:37 pm »
Edit:
Quote
Yes. Ansi and shortstring are assignment compatible.
With the restriction of sizeof(string255]) of course. Ansi strings can be many times larger than shortstring.
« Last Edit: April 22, 2019, 09:54:05 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Uto

  • New Member
  • *
  • Posts: 30
Re: Expression parser and AnsiString
« Reply #3 on: April 21, 2019, 11:23:31 pm »
You are both right, I hadn't noticed my expresion string included double quotes inside the string, at the first and last characters. The exception text didn't help either cause ir was not showing the quotes. But the WriteLn old-debug-style let me saw it. Thanks!

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Expression parser and AnsiString
« Reply #4 on: April 22, 2019, 12:20:48 am »
Glad it helped. Have luck!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018