3

I'm a newbie to use Gold Parser Engine and looking for a way to limit the length of a defined string but I'm not finding any way to do this. Please help de do that/. Here is my code

    ! Welcome to GOLD Parser Builder 
"Case Sensitive"='false'
"Start Symbol" =<start>
{String Char}={Printable}
    <start>::=<Value>
        !<Value>::=<>|a<Value>|b<Value>
<Value>::=<type>name<equal>number<symbol>|<type>name<symbol>
        <type>::=int|float|char|double|boolean|String
            name={Letter}{alphanumeric}+
                <symbol>::=';'
                         <equal>::='='
number={Digit}+[.]{Digit}+|{Digit}+|{Letter} 

Is there any way I could explain the max limit for a string. Thanks

Amessihel
  • 5,891
  • 3
  • 16
  • 40
JAMSHAID
  • 1,258
  • 9
  • 32

1 Answers1

2

Sounds like the parser wasn't designed to easily handle lexeme sizes with regular expressions. You should so check the String size in the skeleton program generated from your Grammar.

To illustrate, I tried this very trivial grammar example from the official website:

"Name"         = 'String Terminal Example'
"Author"       = 'Devin Cook'
"About"        = 'This is a simple example which defines a basic string'

"Start Symbol" = <Value>

! The following defines a set of characters for the string. It contains 
! all printable characters with the exceptionof the double-quotes used 
! for delimiters. The {Printable} set does not contain newlines.

{String Ch} = {Printable} - ["]

! This statement defines the string symbol

String     = '"' {String Ch}* '"'

<Value>   ::= String

String is both as a terminal token (String = '"' {String Ch}* '"') or in a rule (<Value> ::= String). You can check the token size at the terminal level.

I generated a C# one through Calitha Engine - Custom Parser class template, and I got a parser. Below I found the part in which you should check your String terminal token:

// [...]
private Object CreateObjectFromTerminal(TerminalToken token)
{
  switch (token.Symbol.Id)
    {
      // [...]

    case (int)SymbolConstants.SYMBOL_STRING :
      //String
      //todo: Create a new object that corresponds to the symbol
      return null;

      // [...]

    }
  throw new SymbolException("Unknown symbol");
}

According to the Calitha Parser Engine documentation, it's possible to retrieve the text from the token: TerminalToken.Text. So why not proceeding as below:

case (int)SymbolConstants.SYMBOL_STRING :
    // Check size (MAX_LENGTH could be a constant you defined)
    if (token.Text.Length > MAX_LENGTH)
    {
        // handle error here
        throw new SymbolException("String too long");
    }
    return token.Text;
Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • Will the C# part generate the required stuff for gold parser? As I'm not a C# developer so just need to clear that. – JAMSHAID Oct 07 '19 at 02:39
  • I was looking for an answer in GoldParser but found in C#. I'll try to do this in Java as I'm that much easy with C#. – JAMSHAID Oct 07 '19 at 10:10
  • OKAY. so for now, it is not possible limit the size using GOLD PARSER engine? – JAMSHAID Oct 07 '19 at 10:55
  • Yeah. I'm talking about meta language. Am I supposed to use engine using JAVA or C# to achieve what I am looking for? – JAMSHAID Oct 07 '19 at 11:45
  • I've read the documentation but didn't find anything relevant to my problem – JAMSHAID Oct 07 '19 at 14:05