Range Specification in Xtext - dsl

I am new to XText and want to define a language element for specifying ranges of values. Examples: [1-2] or ]0.1-0.3[
I have the following rule for this purpose:
Range returns Expression:
Atomic (leftBracket=('[' | ']') left=Atomic '-' right=Atomic rightBracket=('[' | ']'))*;
Atomic here refers basically to the primitive float and int types. I have two problems:
I get the warning "The assigned value of feature 'leftBracket' will possibly override itself because it is used inside of a loop" and the same for rightBracket. What does this mean in this context?
The expression works only in standalone manner (in one row), but not in connection with the rest of the language elements. E.g. in connection with the element right before:
Comparison returns Expression:
Range ({Comparison.left=current} op=(">="|"<="|">"|"<"|"=>"|" <=>"|"xor"|"=") right=Range)*;
This means, if such an operation is before the Range element in my input of the second Eclipse window, I get the error "No viable alternative at input".
Any ideas? Thanks for any hints and advices!
Some more information:
I took this example and changed it: https://github.com/LorenzoBettini/packtpub-xtext-book-examples/blob/master/org.example.expressions/src/org/example/expressions/Expressions.xtext
Full code:
grammar org.example.expressions.Expressions with org.eclipse.xtext.common.Terminals
generate expressions "http://www.example.org/expressions/Expressions"
ExpressionsModel:
expressions+=Expression*;
Expression: Or;
Or returns Expression:
And ({Or.left=current} "||" right=And)*
;
And returns Expression:
Equality ({And.left=current} "&&" right=Equality)*
;
Equality returns Expression:
Comparison (
{Equality.left=current} op=("==")
right=Comparison
)*
;
Comparison returns Expression:
Range ({Comparison.left=current} op=(">="|"<="|">"|"<"|"=>"|"<=>"|"xor"|"=") right=Range)*
;
Range returns Expression:
Primary (leftBracket=('[' | ']') left=Primary '-' right=Primary rightBracket=('[' | ']'))*
;
Primary returns Expression:
'(' Expression ')' |
{Not} "!" expression=Primary |
Atomic
;
Atomic returns Expression:
{IntConstant} value=INT |
{StringConstant} value=STRING |
{BoolConstant} value=('true'|'false')
;
Example where it fails: (1 = [1-2]) however [1-2] in a row works fine.

i cannot really follow you but your grammar looks strange to me
Model:
(expressions+=Comparison ";")*;
Comparison returns Expression:
Range ({Comparison.left=current} op=(">=" | "<=" | ">" | "<" | "=>" | "<=>" | "xor" | "=") right=Range)*;
Range:
(leftBracket=('[' | ']') left=Atomic '-' right=Atomic rightBracket=('[' | ']'))
|
Atomic;
Atomic:
value=INT;
works fine with
[1-2];
]3-5[;
[1-4[ < ]1-6];
6;
1 < 2;
so can you give some more context

Related

Cannot generate Xtext artifacts for grammar which uses parentheses and cross-references at once

I'm trying to generate DSL from this grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Program:
"print" expression=Expression "where" constant=Constant |
"print" expression=Expression;
Expression:
Add;
Add returns Expression:
Primary({Add.expression1=current} "+" expression2=Primary)*;
Primary returns Expression:
ExpressionParentheses | Number | ConstUsage;
Number returns Expression:
value=INT;
Constant:
name=ID "=" number=Number;
ConstUsage returns Expression:
name=[Constant];
ExpressionParentheses returns Expression:
"(" Add ")";
But generating Xtext artifacts in Eclipse always produces an error. It occurs always when I'm using ExpressionParentheses and also ConstUsage in Primary rule at once. When I put there either ConstUsage or ExpressionParentheses, everything works fine. What could be the problem with my grammar ?
thw following grammar works fine
Program:
"print" expression=Expression ("where" constant=Constant)?
;
Expression:
Add;
Add returns Expression:
Primary({Add.expression1=current} "+" expression2=Primary)*;
Primary returns Expression:
Number | ConstUsage | "("Add")";
Number returns Expression:
value=INT;
Constant:
name=ID "=" number=Number;
ConstUsage returns Expression:
name=[Constant];

Haskell Criterion - 'nf' is applied to too few arguments

I am a new guy to Haskell. I am working a benchmark(Criteriaon) on binary search algorithm. I keep getting error: 'nf' is applied to too few arguments what am I doing wrong.
Thanks
binSear array serNum lowInx highInx
| highInx < lowInx = -1
| array!!sred > serNum = binSear array serNum lowInx (mid-1)
| array!!sred < serNum = binSear array serNum (mid+1) highInx
| otherwise = mid
where
mid = lowInx + ((highInx - lowInx) `div` 2)
main = do
let arr = [1..10000000]
defaultMain [
bench "1" $ nf (binSear arr 54527 0 9999999)
]
The type of nf is (a->b)->a->b, so it expects two parameters: a function and an input to that function. The function should produce a Benchmarkable.
In your case, you are just passing one parameter to nf: the function itself, but that function is fully applied, so it's not expecting any additional parameter, nor are you passing that extra parameter. In this case, you should partially apply the function and pass that extra parameter to nf.
You may be forced to reorder the parameters of binSear or create a helper lambda to do so, to ensure that the currying happens in the last parameter, and you should pass that parameter to nf outside of the parenthesis.

Express a rule with ANTLR4

I must define a rule which expresses the following statement: {x in y | x > 0}.
For the first part of that comprehension "x in y", i have the subrule:
FIRSTPART: Name "in" Name
, whereas Name can be everything.
My problem is that I do not want a greedy behaviour. So, it should parse until the "|" sign and then stop. Since I am new in ANTLR4, I do not know how to achieve that.
best regards,
Normally, the lexer/parser rules should represent the allowable syntax of the source input stream.
The evaluation (and consequences) of how the source matches any rule or subrule is a matter of semantics -- whether the input matches a particular subrule and whether that should control how the rule is finally evaluated.
Normally, semantics are implemented as part of the tree-walker analysis. You can use alternate subrule lables (#inExpr, etc) to create easily distinguishable tree nodes for analysis purposes:
comprehension : LBrace expression property? RBrace ;
expression : ....
| Name In Name #inExpr
| Name BinOp Name #binExpr
| ....
;
property : Provided expression ;
BinOp : GT | LT | GTE | .... ;
Provided : '|' ;
In : 'in' ;

Cnnverting FSM to regular expression

I know how to convert regular expression into FSM but not exactly sure how to reverse it.
what would the regular expression for this example be?
Regular expression for your DFA will is (b + ab*a)*
The language description: Symbol b can appear in any fashion but restriction is a can to be for even number of times in language strings.
(b + ab*a)*
^ ^ ^
| | "* because loop on initial state"
| | "* on b because of self loop with label b on 2nd state"
|
|"+ because two outgoing edges, One is self loop other via 2nd state"
Here: + means Union, * means repetition for zero or more times
Note: Language string examples: {^, b, aa, bababb...}
(even as and any bs including null)

haskell own datatypes - using signs or numbers in the datatype definition

I have a question according to the definition of datatypes:
Is it possible to use signs or numbers in the definition?
For instance if I want to create a datatype for the lower and equal sign the following code works of course
data Signs = Lo | Eq
The constructor Lo stands for "<" and the constructor Eq is "=".
But I can't use the "real" signs. For instance the following codes won't work
data Signs = Lo "<" | Eq "="
type Signs = "<" | "="
type MyInt = '1' | '2'
data MyInt = One '1' | Two '2'
So I would like to know if there is a possibility to use "real" signs and numbers within the definition. And if there is one it would be nice if you could tell me how it works. ;)
Operator identifiers prefixed with : can be used in data constructors.
data Signs = (:<) | (:=)
if they are nullary then AFAIK you have to keep the parens:
[(:<), (:=)]
You can use numbers but the first character must be an uppercase letter.
data MyInt = N1 | N2
See http://www.haskell.org/onlinereport/lexemes.html

Resources