Getting value of parser rules during runtime [message #1075283] |
Mon, 29 July 2013 07:14 |
Priya Sabut Messages: 41 Registered: September 2012 |
Member |
|
|
Hi All,
I am writing grammar to parse a specific type of command. Where I parse the command depending on some separator. My command has a header and body separated by 'header-boby separator'. Again the body part consists of one or more parameters separated with a 'parameter separator'. The parameter can be value-only or a name-value pair delimited with 'name-value delimiter'. For a specific element, the commands have different separators(they can be same also, to make it simple I am considering commands with unique separators).
For example:
The commands for element -1 is "Header1 : Param11=value11, value12, param13 = value13", where colon(: ) is header-body separator, comma (,) is parameter separator and equal(=) is name-value delimiter.
The commands for element -2 is "Header2, value21: param22 - value22: param23 - value23", where comma(,) is header-body separator, colon(: ) is parameter separator and dash(-) is name-value delimiter.
If both the rules - HEADERDELIMITER and PARAMSEPARATOR has same value (':' | ','), I get parser error while running xtext file. As they are conflicting with each other.
To parse commands for both the elements I have to create two xtext files with different separator values for both conflicting rules, which is not a good way!
Can I somehow get these separators during runtime while specifying the language. In my language if I specify
Language::
'headerBodySeparator :' //the value colon (: ) should be the value of rule HEADERDELIMITER.
Rule Definition ::
HEADERDELIMITER :
valueOf(headerBodySeparator) ;
My grammar below::
DomainModel:
(commands += Command+)
;
Command:
header = Header (blocks += Block*) tail=Tail
;
Block hidden(WS,ML_COMMENT,SL_COMMENT):
{Block}
(segSep=HEADERDELIMITER) (paramSet+=ParameterSet*)
;
ParameterSet:
{ParameterSet}
param = CommandParameter (paramSep = PARAMSEPARATOR?)
;
CommandParameter:
valueOnly = ValueOnly | nameValuePair = NameValuePair
;
ValueOnly:
name=ValidID
;
NameValuePair:
name=ValidID sep = Assign (variableName=Variable | literal=CommandLiteral)
;
Variable:
'<' name= IDORINT'>';
CommandLiteral:
name = IDORINTORSTRING
;
IDORINT:
(ValidID | INT)
;
IDORINTORSTRING:
(IDORINT | STRING)
;
Header:
name=ValidID
;
HEADERDELIMITER:
(':' | ',')
;
Tail:
(';')
;
PARAMSEPARATOR:
(':' | ',')
;
Assign:
('=' | '-')
;
Thank you in advance...
|
|
|
|
|
|
|
Re: Getting value of parser rules during runtime [message #1075833 is a reply to message #1075283] |
Tue, 30 July 2013 06:53 |
Priya Sabut Messages: 41 Registered: September 2012 |
Member |
|
|
Hi Christian,
Thank you for your reply.
The grammar is becoming ambiguous, If it is focusing to parse the commands that are targeted to multiple target elements. But for a single element It is not ambiguous as the separators will be different.
For example:
For Target Element-1 : header delimiter is colon(: ), parameter separator is comma(,), name-value delimiter is equal(=).
For Target Element-2 : header delimiter is comma(,), parameter separator is colon(: ), name-value delimiter is equal(-).
At a time, I will be processing commands for either Element-1 or Element-2 not both.
So my idea is to provide all the delimiters before putting the command, Somewhat similar to below statement :
For Element -1
//Define all the separators
define HeaderDelimiter :
define ParameterSeparator ,
define Name-ValueDelimiter =
//Put the command
Header:parm1=val1,parm2,parm3=val
For Element -2
//Define all the separators
define HeaderDelimiter ,
define ParameterSeparator :
define Name-ValueDelimiter -
//Put the command
Header,parm1=val1:parm2:parm3-val
So that I an use these delimiter values(provided in the language) in the grammar to specify the parser rule.
I hope, I conveyed my problem to you.
Is there any other way to solve the same problem described ? Please share...
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03604 seconds