1
votes

I'm new to Xtext and I'm trying to create a simple DSL for railway systems, here's my grammar :

grammar org.xtext.railway.RailWay with org.eclipse.xtext.common.Terminals

generate railWay "http://www.xtext.org/railway/RailWay"

Model:
    (trains+=Train)*
    | (paths+=Path)*
    | (sections+=Section)*
;

Train:
    'Train' name=ID ':'
    'Path'  path=[Path]
    'Speed' speed=INT
    'end'
;

Path:
    'Path'      name=ID ':'
    'Sections'  ('{' sections+=[Section] (',' sections+=[Section] )+ '}' ) | sections+=[Section]
    'end'
;

Section:
    'Section'   name=ID ':'
    'Start'     start=INT
    'End'       end=INT
    ('SpeedMax' speedMax=INT)?
    'end'
;

But when I put this code at the Eclipse instance :

Section brestStBrieux :
    Start 0
    End 5
end

Section StBrieuxLeMan :
    Start 5
    End 10
end

Section leManParis :
    Start 1
    End 12
end

Path brestParis :
    Sections  { brestStBrieux, StBrieuxLeMan, leManParis}
end

Train tgv :
    Path  brestParis
    Speed  23
end

I got this error three times:

mismatched input '0' expecting RULE_INT mismatched input '1' expecting RULE_INT mismatched input '5' expecting RULE_INT

I can't see where those errors come from, what can I do to fix them. Any idea?

2
are you sure you posted all relevant parts of the grammar? sure you do not have any keywords or terminal rules that overlap with the INT terminal rule? - Christian Dietrich
I have this terminal rule but I didn't used it yet : terminal FLOAT : '-'? INT ('.' INT)?; - cartman

2 Answers

0
votes

Christian is right, since the FLOAT terminal is no longer defined, the original problem is resolved. Anyway, a remaining issue is the rule

Path:
    'Path'      name=ID ':'
    'Sections'  ('{' sections+=[Section] (',' sections+=[Section] )+ '}' ) | sections+=[Section]
    'end'
;

which currently has this precedence:

Path:
    (
       'Path' name=ID ':' 'Sections'
       ('{' sections+=[Section] (',' sections+=[Section] )+ '}' )
    ) 
    |
    (sections+=[Section] 'end')
;

You may want to rewrite it to

Path:
    'Path'      name=ID ':'
    'Sections'  
    ( 
       ('{' sections+=[Section] (',' sections+=[Section] )+ '}' )
    |  sections+=[Section]
    ) 'end'
;
0
votes

lexing and parsing are different steps. thus no using does not matter. and your grammar gets ambigous (have a look at the warnings when generating the lang) you should turn that into a datatype rule (simply omit the terminal keyword)

=> change your grammar to

grammar org.xtext.example.mydsl2.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl2/MyDsl"

Model:
    (trains+=Train)*
    | (paths+=Path)*
    | (sections+=Section)*
;

Train:
    'Train' name=ID ':'
    'Path'  path=[Path]
    'Speed' speed=INT
    'end'
;

Path:
    'Path'      name=ID ':'
    'Sections'  ('{' sections+=[Section] (',' sections+=[Section] )+ '}' ) | sections+=[Section]
    'end'
;

Section:
    'Section'   name=ID ':'
    'Start'     start=INT
    'End'       end=INT
    ('SpeedMax' speedMax=INT)?
    'end'
;

FLOAT : '-'? INT ('.' INT)?;