Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Performance question
Performance question [message #1716043] Mon, 30 November 2015 19:20 Go to next message
Luis De Bello is currently offline Luis De BelloFriend
Messages: 95
Registered: January 2015
Member
Hi guys,

Based on one previous question about performance, I reduced my grammar to reduce the numbers of rules in order to analyse the performance.
I was able to simplified a lot and now I am trying to resolve the issue, maybe some of you can help me with some ideas on how to change my grammar to fully support the syntax that I need

Grammar:

grammar org.mule.tooling.dfl.DFL hidden(WS, EOL)

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate dFL "http://www.mule.org/tooling/dfl/DFL"

Document:
	content=Content;

Content:
	elements+=Expression;

SingleKeyValuePairObj:
	element=ObjectElement;

MultipleKeyValuePairObj:
	{MultipleKeyValuePairObj} "{" (elements+=ObjectElement ((-> "," elements+=ObjectElement) | ",")*)? "}";

ObjectElement:
	=> ConditionalKeyValuePair | => KeyValuePair | EnclosedExpression;

KeyValuePair:
	key=Key ":" (-> value=Expression?);

ConditionalKeyValuePair:
	"(" key=Key ":" value=Expression ")" "when" condition=Expression;

Key:
	value=KeyExpression;

KeyExpression:
	LiteralValue | EnclosedExpression;
	

Expression:
	OrExpression;

OrExpression returns Expression:
	AndExpression (-> ({Or.left=current} "or") right=AndExpression)*;

AndExpression returns Expression:
	Atomic (-> ({And.left=current} "and") right=Atomic)*;

Atomic returns Expression:
	=> SingleKeyValuePairObj | LiteralValue | MultipleKeyValuePairObj | EnclosedExpression;

EnclosedExpression returns Expression:
	"(" Expression ")";

LiteralValue:
	{QuotedStringLiteral} value=QuotedStringLiteral | {BooleanLiteral} value=BooleanLiteral | {NullLiteral} value=NullLiteral;

NullLiteral:
	"null";

BooleanLiteral returns ecore::EBoolean:
	"true" | "false";

QuotedStringLiteral:
	STRING;

terminal STRING:
	'"' ('\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\' | '"'))* '"'? |
	"'" ('\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\' | "'"))* "'"?;

terminal WS:
	(' ' | '\t')+;

terminal EOL:
	LINE_BREAK;

terminal fragment LINE_BREAK:
	('\r' | '\n');


This grammar support these expressions

{
	"SimpleField": "SimpleValue",
	"SimpleField": "SimpleField": "SimpleValue",
	"ComplexField": (((((((((true) and (false) and (true)) and (false))) and (false)))))),
	("DynamicField"): "SimpleValue",
	("ConditionalExpression"),
	("SimpleField": "SimpleValue") when true	
}


However it takes 3-4 seconds to parse this, but if I remove the line ["ComplexField": (((((((((true) and (false) and (true)) and (false))) and (false))))))] it runs in less that 300 ms.

I used some profiling tool and I noted that if I remove the "EnclosedExpression" from "KeyExpression" it runs ok but it means that I cannot support all the cases

Does someone have some idea about how to continue supporting all my cases in a decent time?

Thanks in advance

Regards,
Luis
Re: Performance question [message #1716046 is a reply to message #1716043] Mon, 30 November 2015 20:20 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You have multiple uses for "(" ... ")" consequently backtracking has
alternatives at Atomic and so it is quite possible that your expression
terms go exponential.

major solution: avoid "(" .. ")" ambiguity, or refactor to eliminate it.

minor solution: avoid nested complexity and precedence predicates

Expression:
Atomic ({Exp.left=current} BinaryOperator) right=Atomic)*;

BinaryOperator: "and" | "or";

and resolve precedence semantically.

Regards

Ed Willink

On 30/11/2015 19:21, Luis De Bello wrote:
> Hi guys,
>
> Based on one previous question about performance, I reduced my grammar
> to reduce the numbers of rules in order to analyse the performance. I
> was able to simplified a lot and now I am trying to resolve the issue,
> maybe some of you can help me with some ideas on how to change my
> grammar to fully support the syntax that I need
>
> Grammar:
>
>
> grammar org.mule.tooling.dfl.DFL hidden(WS, EOL)
>
> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
> generate dFL "http://www.mule.org/tooling/dfl/DFL"
>
> Document:
> content=Content;
>
> Content:
> elements+=Expression;
>
> SingleKeyValuePairObj:
> element=ObjectElement;
>
> MultipleKeyValuePairObj:
> {MultipleKeyValuePairObj} "{" (elements+=ObjectElement ((-> ","
> elements+=ObjectElement) | ",")*)? "}";
>
> ObjectElement:
> => ConditionalKeyValuePair | => KeyValuePair | EnclosedExpression;
>
> KeyValuePair:
> key=Key ":" (-> value=Expression?);
>
> ConditionalKeyValuePair:
> "(" key=Key ":" value=Expression ")" "when" condition=Expression;
>
> Key:
> value=KeyExpression;
>
> KeyExpression:
> LiteralValue | EnclosedExpression;
>
>
> Expression:
> OrExpression;
>
> OrExpression returns Expression:
> AndExpression (-> ({Or.left=current} "or") right=AndExpression)*;
>
> AndExpression returns Expression:
> Atomic (-> ({And.left=current} "and") right=Atomic)*;
>
> Atomic returns Expression:
> => SingleKeyValuePairObj | LiteralValue | MultipleKeyValuePairObj |
> EnclosedExpression;
>
> EnclosedExpression returns Expression:
> "(" Expression ")";
>
> LiteralValue:
> {QuotedStringLiteral} value=QuotedStringLiteral | {BooleanLiteral}
> value=BooleanLiteral | {NullLiteral} value=NullLiteral;
>
> NullLiteral:
> "null";
>
> BooleanLiteral returns ecore::EBoolean:
> "true" | "false";
>
> QuotedStringLiteral:
> STRING;
>
> terminal STRING:
> '"' ('\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\' |
> '"'))* '"'? |
> "'" ('\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\' |
> "'"))* "'"?;
>
> terminal WS:
> (' ' | '\t')+;
>
> terminal EOL:
> LINE_BREAK;
>
> terminal fragment LINE_BREAK:
> ('\r' | '\n');
>
>
> This grammar support these expressions
>
> {
> "SimpleField": "SimpleValue",
> "SimpleField": "SimpleField": "SimpleValue",
> "ComplexField": (((((((((true) and (false) and (true)) and
> (false))) and (false)))))),
> ("DynamicField"): "SimpleValue",
> ("ConditionalExpression"),
> ("SimpleField": "SimpleValue") when true
> }
>
>
> However it takes 3-4 seconds to parse this, but if I remove the line
> ["ComplexField": (((((((((true) and (false) and (true)) and (false)))
> and (false))))))] it runs in less that 300 ms.
>
> I used some profiling tool and I noted that if I remove the
> "EnclosedExpression" from "KeyExpression" it runs ok but it means that I
> cannot support all the cases
>
> Does someone have some idea about how to continue supporting all my
> cases in a decent time?
>
> Thanks in advance
>
> Regards,
> Luis
>
Re: Performance question [message #1716048 is a reply to message #1716046] Mon, 30 November 2015 20:58 Go to previous messageGo to next message
Luis De Bello is currently offline Luis De BelloFriend
Messages: 95
Registered: January 2015
Member
Hi guys,

I was able to solve my issue doing the following change , but to be honest I do not have idea why this is working.

Atomic returns Expression:
    => SingleKeyValuePairObj | LiteralValue | MultipleKeyValuePairObj | EnclosedExpression;

Atomic returns Expression:
    "(" Expression ")" | => SingleKeyValuePairObj | LiteralValue | MultipleKeyValuePairObj;


Do you have any idea the different between these two lines?

To be honest I believed that the order does not matter and that is the reason why we should use predicates to handle some specific cases.

Thanks in advance

Regards,
Luis
Re: Performance question [message #1716063 is a reply to message #1716048] Mon, 30 November 2015 22:18 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You are using LL technology. It is an ordered search with backtracking.
The change may exclude the alternative syntax.

IMHO you would do much better to develop your dangerously ambiguous
grammar with an LALR technology such as LPG/Bison/Yacc to ensure a clean
unambiguous parse, then re-implement with Xtext.

Regards

Ed Willink


On 30/11/2015 20:58, Luis De Bello wrote:
> Hi guys,
>
> I was able to solve my issue doing the following change , but to be
> honest I do not have idea why this is working.
>
>
> Atomic returns Expression:
> => SingleKeyValuePairObj | LiteralValue | MultipleKeyValuePairObj |
> EnclosedExpression;
>
> Atomic returns Expression:
> "(" Expression ")" | => SingleKeyValuePairObj | LiteralValue |
> MultipleKeyValuePairObj;
>
>
> Do you have any idea the different between these two lines?
>
> To be honest I believed that the order does not matter and that is the
> reason why we should use predicates to handle some specific cases.
>
> Thanks in advance
>
> Regards,
> Luis
Re: Performance question [message #1716124 is a reply to message #1716063] Tue, 01 December 2015 14:26 Go to previous message
Luis De Bello is currently offline Luis De BelloFriend
Messages: 95
Registered: January 2015
Member
Hi Ed,

Thanks for your answer, but I thought that the antlr parser does not use backtracking by default, besides I didn't activate that option so I am not sure how alternatives are handled because if alternatives are handled in order, ambiguities shouldn't be an issue because the first branch will be consumed.

Can some one explain me that or provide me a good link to read about this?

Regards,
Luis
Previous Topic:Help example java world
Next Topic:Pass strings from mwe2 workflow to an xtend generator
Goto Forum:
  


Current Time: Fri Apr 19 00:20:12 GMT 2024

Powered by FUDForum. Page generated in 0.01884 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top