editing the ActiproSoftware.Simple.Grammar.xml file

SyntaxEditor for Windows Forms Forum

Posted 10 years ago by Einat
Version: 4.0.0282
Avatar
Hello!
I'm trying to create semantic parser for JavaScript language.
Does anybody made a grammar xml file for JavaScript?
I think I need to see an example of edited NonTerminal Production code (for "if/else", "for", "while", etc. statements) in order to understand how to do this by-my-own.

I've started to edit ActiproSoftware.Simple.Grammar.xml file, but right away I've got Error message from GrammarDesigner, that I couldn't understand.
I've changes "Statement" NonTermonal Production code in order to be able to make an assignment for var during it's declaration (like: var tmp = "test";).
I've added this code (just after the "Variable declaration statement" and before the "Assignment statement"):

| (
<%
' Variable statement with a value assignment
Dim variableName As Identifier = Nothing
Dim expression As Expression = Nothing
%>
'Var'
"Identifier<@ variableName @>"
'Assignment'
"Expression<@ expression @>"
'SemiColon<- ->'
<%
'statement = new AssignmentStatement(variableName, expression, new TextRange(startOffset, Me.Token.EndOffset))
%>
)

I've tried to run Parser Generator and got this Error message:
"A parser generator grammar exception occurred: r\nProduction 'Statement'contains an alternation that has multiple references to the terminal 'Var', either directly or via a non-terminal first set.

Does anybody knows what is this error means? Or more important: what is wrong with my code?

TX,
Einat



The full code looks like this:

<NonTerminal Key="Statement" Parameters="ByRef statement As Statement">
<Production>
<![CDATA[
<%
statement = Nothing
Dim startOffset As System.Int32 = Me.LookAheadToken.StartOffset
%>
"Block<@ statement @>"
| (
<%
' Empty statement
%>
'SemiColon'
<%
statement = new EmptyStatement(Me.Token.TextRange)
%>
)
| (
<%
' Variable declaration statement
Dim variableName As Identifier = Nothing
%>
'Var'
"Identifier<@ variableName @>"
'SemiColon<- ->'
<%
statement = new VariableDeclarationStatement(variableName, new TextRange(startOffset, Me.Token.EndOffset))
%>
)

| (
<%
' Variable statement with a value assignment
Dim variableName As Identifier = Nothing
Dim expression As Expression = Nothing
%>
'Var'
"Identifier<@ variableName @>"
'Assignment'
"Expression<@ expression @>"
'SemiColon<- ->'
<%
'statement = new AssignmentStatement(variableName, expression, new TextRange(startOffset, Me.Token.EndOffset))
%>
)

| (
<%
' Assignment statement
Dim variableName As Identifier = Nothing
Dim expression As Expression = Nothing
%>
"Identifier<@ variableName @>"
'Assignment'
"Expression<@ expression @>"
'SemiColon<- ->'
<%
statement = new AssignmentStatement(variableName, expression, new TextRange(startOffset, Me.Token.EndOffset))
%>
)
| (
<%
' Return statement
Dim expression As Expression = Nothing
%>
'Return'
"Expression<@ expression @>"
'SemiColon<- ->'
<%
statement = new ReturnStatement(expression, new TextRange(startOffset, Me.Token.EndOffset))
%>
)
]]></Production>
</NonTerminal>

Comments (3)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Einat,

Your error message was:
A parser generator grammar exception occurred: Production 'Statement'contains an alternation that has multiple references to the terminal 'Var', either directly or via a non-terminal first set.

So that is saying that in an alternation within the production for Statement, there are multiple paths that start with a 'Var' token. When I look at your code I see the alternation option with comment "Variable declaration statement" and the one with comment "Variable statement with a value assignment" both starting with a 'Var'.

That is not allowed since how would we know which path to take in that case? You'd want to combine the similar start parts, something like this:
| ( 
'Var' 
<% 
Dim variableName As Identifier = Nothing 
%> 
"Identifier<@ variableName @>" 

(
'SemiColon<- ->' 
<% 
statement = new VariableDeclarationStatement(variableName, new TextRange(startOffset, Me.Token.EndOffset)) 
%> 
) 
| ( 
'Assignment' 
<% 
Dim expression As Expression = Nothing 
%> 
"Expression<@ expression @>" 
'SemiColon<- ->' 
<% 
'statement = new AssignmentStatement(variableName, expression, new TextRange(startOffset, Me.Token.EndOffset)) 
%> 
) 
)
Hope that helps.


Actipro Software Support

Posted 10 years ago by Einat
Avatar
Hi,

Thanks a lot for a quick reply.
Unfortunately, your solution didn't work.
I still can't write this code:
var tmp = 3;
Semantic parser recognize only "var tmp" and then expects for semicolon.

I feel so lost with this grammar xml file :(
Do you have an edited grammar file for any language (ASP, JAVA, etc.)?
There are no examples in documentation and Simple language sample is too basic for me.

TX,
Einat
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Einat,

I didn't try the snippet I posted in the Grammar Designer (since I don't have a full grammar that I can paste it into to test) however I would believe it would work. It's the same technique we've done in our own grammars.

Basically you merge the common starting terminal/non-terminal patterns from the two alternations (OR's) and then have a child alternation for the remaining different parts. So I would expect that what I wrote would generate an 'if' statement in the output code that looks for a semi-colon and if that is not found, it would look for an assignment token instead.

If you still have a problem you can email over what you have and we can take a look. Unfortunately we don't have any other grammar samples other than what is in the sample project. Some people end up purchasing a Blueprint license for our add-ons to see how we do some of the more complex things.


Actipro Software Support

The latest build of this product (v2018.1 build 0341) was released 7 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.