|
Re: Problems with terminal rules... [message #634330 is a reply to message #634312] |
Thu, 21 October 2010 09:52 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
Your terminal rules overlap, e.g. "'1'" will match LGL as well as CHAR,
"0" will match INT as well ad DEC. As the internal Antlr lexer is
context-less this can yield very strange behaviour, as the first
matching terminal rule wins. It's generally a better idea to use a
minumum set of terminal rules and cover the rest in datatype rules,
which are evaluated by the parser thus dealing with lookahead etc, e.g.
Int returns ecore::EInt :
(-|+)? INT;
Dec:
(-|+)? INT '.' INT?
terminal INT:
('0'..'9')+;
Into the bargain, your character rule matches 'a as well as a' or 'a'.
You should rather use an alternative covering either quotes on both ends
or none, as we do in the STRING rule in
/org.eclipse.xtext/src/org/eclipse/xtext/common/Terminals.xt ext
Am 21.10.10 11:08, schrieb Fran Blanco:
> Hi,
>
> I want to create a DSL and i am having some problems...
>
> I have some kind of variables:
>
> INT --> +- 0...9
> DEC ---> +- 0...9 . 0...9
> LGL ---> '0' or '1'
> CHAR --> A....Z 0....9 eg. YESNO or '1234' or 'YESNO'
>
> I have the problem with CHAR variable. I can introduce this kind of
> variable of different ways:
>
> YESNO
> 'YESNO'
> 'ASDD1234'
> ASD1ASD
>
> between quotes or not...
>
> Here is my Code:
>
> Quote:
>> grammar org.xtext.prueba with org.eclipse.xtext.common.Terminals
>> generate prueba "http://www.xtext.org/prueba"
>> Model:
>> CLStatementInicial;
>>
>> terminal ENTEROS:
>> ('-')? ('0'..'9')+;
>> terminal DECIMAL:
>> (ENTEROS) ('.')? (ENTEROS)?;
>> terminal GENERAL:
>> ('A'..'Z') ('A'..'Z' | '0'..'9')*;
>> terminal PARAMETRO:
>> ('&') ('A'..'Z') ('A'..'Z' | '0'..'9')*;
>> terminal PROCEDURE:
>> ('A'..'Z' | '0'..'9' | '_')+;
>> terminal DIRECTORIO:
>> ('A'..'Z' | '0'..'9' | '/' | '_')+;
>> terminal CHAR: //Espacios da ERROR
>> "'"? ('A'..'Z' | '0'..'9' | '/' | '_' | '.')+ "'"?;
>>
>>
>>
>> enum LISTATIPOSPARAMETRO:
>> CHAR = '*CHAR' |
>> DEC = '*DEC' |
>> LGL = '*LGL' |
>> INTEGER = '*INT' |
>> UINT = '*UINT';
>>
>> //Definición estructura Valores...
>> NumeroINT:
>> Value=(ENTEROS);
>> NumeroDEC:
>> Value=(DECIMAL);
>> NumeroCHAR:
>> Value=(CHAR);
>> NumeroLGL:
>> Value=("'0'" | "'1'");
>> Valor:
>> NumeroINT |
>> NumeroDEC |
>> NumeroCHAR |
>> NumeroLGL;
>>
>> CLStatementInicial:
>> DCL: 'DCL' ('VAR'? '('? (parametro=PARAMETRO) ')'?)
>> ('TYPE'? '('? (listatipos=LISTATIPOSPARAMETRO) ')'?)
>> ('LEN'? '('? (length=ENTEROS) (length2=ENTEROS)? ')'?)?
>> (valor1=NumeroLGL |('VALUE' '(' (valor=Valor) ')'))?
>
>
> When I introduce a CHAR with quotes is fine, but if i dont do it, doesnt
> work....
>
>
> What could it be the correct structure of terminal rules?
> Im so lost....
>
> Thanks you..!
>
> B
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
Re: Problems with terminal rules... [message #634549 is a reply to message #634312] |
Fri, 22 October 2010 07:39 |
Fran Blanco Messages: 20 Registered: October 2010 |
Junior Member |
|
|
Hi Jan,
Thank you for ur reply! I think i have got the solution of INT and DEC problem... Here we go:
Model:
'PGM'
('PARM' '(' (parametros+=PARAMETRO)+ ')' )?
(OtrosStatements+=Statements)*
'ENDPGM';
terminal PARAMETRO:
('&') ('A'..'Z') ('A'..'Z' | '0'..'9')*;
DATAINT:
('-'|'+')? INT;
DATADEC:
('-'|'+')? INT '.' INT;
Valor:
DATAINT |
DATADEC;
enum LISTATIPOSPARAMETRO:
CHAR = '*CHAR' |
DEC = '*DEC' |
LGL = '*LGL' |
INTEGER = '*INT' |
UINT = '*UINT';
Statements:
'DCL'
('VAR'? '('? (parametro=PARAMETRO) ')'?)
('TYPE'? '('? (listatipos=LISTATIPOSPARAMETRO) ')'?)
('LEN'? '('? (INT) (INT)? ')'?)?
('VALUE' '(' (Valor) ')')?
What do you think? Its right?
After that, CHAR variable must have uppercases and numbers only. For example:
YESNO
YES123NO
'YESNO'
'YES123NO'
I m trying to put together INT rule with a uppercase letter rule, but RULE_INT fails... i have tried use a datatype too but I havent got it.. I dont know how to do it..!
Quotes issue i havent tried it yet... :S
But I have looked at terminal STRING and i dont understand the structure:
terminal STRING :
'"' ( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|'"') )* '"' |
"'" ( '\\' ('b'|'t'|'n'|'f'|'r'|'"'|"'"|'\\') | !('\\'|"'") )* "'";
But i could do a datatype like that:
DATACHAR2:
("'") DATACHAR ("'") |
DATACHAR;
Thank you so much again.
B
Edit:
I am trying right now and i cannot do it.
terminal UPPER:
"'" ('A'..'Z' | '0'..'9')+ "'" |
('A'..'Z' | '0'..'9')+;
The following token definitions are unreachable: RULE_INT..
[Updated on: Fri, 22 October 2010 08:18] Report message to a moderator
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04890 seconds