MultiMatchSets and Match method

SyntaxEditor for Windows Forms Forum

Posted 16 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Version: 4.0.0271
Avatar
Hi

I'm having trouble to use the "Match" method in a RecursiveDescentSemanticParser class.
(it's always returning FALSE...)

I defined the followings token :

<ExplicitPatternGroup PatternValue="if" Key="IfPatternGroup" TokenKey="IfReservedWordToken" EndBracket="EndIfPatternGroup" Style="ReservedWordStyle" CaseSensitivity="Sensitive" />

<ExplicitPatternGroup PatternValue="endIf" Key="EndIfPatternGroup" TokenKey="EndIfReservedWordToken" StartBracket="IfPatternGroup" Style="ReservedWordStyle" CaseSensitivity="Sensitive" />

and in the semantic parser I call :
bool l_matchResult = this.Match(this.LookAheadToken.LexicalPatternGroup.EndBracket.TokenID)

What's wrong with this code ?


Also I don't really understand the goal of the MultiMatchSets property...
What's the goal of it and how to fill the array ?

At the moment I copy hte property of the SimpleSemanticParser...
private static bool[,] multiMatchSets = {
// 0: Identifier SemiColon Var Return OpenCurlyBrace
{n,n,n,n,n,n,n,n,n,Y,n,n,Y,Y,n,n,Y,n,n,n,n,Y,n,n,n,n,n,n,n,n,n},
// 1: Identifier SemiColon Var Return OpenCurlyBrace
{n,n,n,n,n,n,n,n,n,Y,n,n,Y,Y,n,n,Y,n,n,n,n,Y,n,n,n,n,n,n,n,n,n}
};

Thanks for your help

Stéphane

Comments (7)

Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
First, you are doing it the really hard way! :)

You should never edit the semantic parser class code like that. Instead you should be editing the XML grammar and have the Grammar Designer output it for you. It will take care of multi-match sets etc. They are just an optimization instead of making like 10 if conditions, it does a lookup in there.

But anyhow, look at the ActiproSoftware.Simple.Grammar.xml sample grammar file for an example of making an XML grammar. When you run it through the Grammar Designer, it outputs SimpleSemanticParser.


Actipro Software Support

Posted 16 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Avatar
I'd prefer to be able to use the Grammar designer....
...but I already looked at the ActiproSoftware.Simple.Grammar.xml and
didn't manage to write a very simple grammar that "only" matches
if..endIf
for..endFor
...

That's why I'm doing the semantic parser from scratch !!! :-(
(and it works fine but it takes a lot of time to develop)

I already asked the question but can you provide a light grammar definition sample ?
or.....
How to use the .Match method ? :-)

Stéphane
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
I really would strongly urge you not to code the semantic parser directly. It will work ok when you only have several non-terminals however once your language gets more complex it will turn into a nightmare and you will regret going down that path.

The parser generator is there so that you can worry about just specifying the grammar and it will handle all the match statements and generation of "if"'s for you. For instance in our C# language, there are many places where there could be an if statement that checks for any of 50 various tokens. Coding this by hand would be extremely error prone and impossible to maintain. By using the parser generator, you let SyntaxEditor handle the complexity for you.

I would go back and start with the Simple language grammar again. Make a copy of it for your language, remove all the non-terminal definitions except the CompilationUnit and another one. Then change the other one to be called IfStatement. To start, just have its definition be:
'If'
'EndIf'

Or whatever the related tokens are. Call it from CompilationUnit. Also remove the AST node generation stuff from the grammar, and change your Token definitions at the top to be tokens in your language like 'If' and 'EndIf'.

Once you run the parser generator on it, you will have C# or VB semantic parser class. If you have problems with it, then step through and debug that class like you would nay other code. You can see where your logic is wrong by doing that. Make any appropriate changes to the grammar to fix the problems, run the parser generator, and run your app again.

That is how we develop languages... make changes to grammar, run parser generator on it, test the changes, and if necessary go back to do more changes and repeat.

Hope that helps get you started.


Actipro Software Support

Posted 16 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Avatar
It's really better !!!!
I followed your instructions step by step and it's nearly working.

The problem is that I can't define my statement like this :

<NonTerminal Key="IfStatement">
<Production><![CDATA[
'if'
'endIf'
]]></Production>
</NonTerminal>

because the parser generator generate a class SimpleTokenID with the following
const definition :

/// <summary>
/// The If token ID.
/// </summary>
public const int if = 0;

which is invalid because 'if' is a C# keyword...

Anyway, THANKS A LOT for your help and for your answers

Stéphane
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Right, what we do is define all token IDs in Pascal casing. So define them as 'If' and 'EndIf' instead. This avoids the issue. Also if that still leads to a conflict (like if you were writing your code in VB), we sometimes put Keyword at the end of the ID. So it would be 'IfKeyword'.

Is this a dynamic language or a programmatic language? If it's a dynamic language then just redirect the patterns to use the revised token IDs.


Actipro Software Support

Posted 16 years ago by BLANC Stéphane - Staubli Robotics Suite Product Manager, STAUBLI
Avatar
You add 'Keyword' at the end in the grammar definition file
and then you override the GetTokenString method ?
Posted 16 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
I'm just saying in the case of conflicts, an alternate way to do things is to call your token ID 'ifKeyword' for instance. This means the generated value will be YourLanguageTokenID.ifKeyword and therefore won't have the conflict with C#'s if.

But yes as you noticed, you would need to make GetTokenString strip out the Keyword portion of its return value.


Actipro Software Support

The latest build of this product (v24.1.0) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.